/sites/all/modules/contrib/civicrm/Civi/Test.php

https://gitlab.com/virtualrealms/d7civicrm · PHP · 170 lines · 140 code · 8 blank · 22 comment · 4 complexity · de134c313b1d69f8ad9bc0303387420d MD5 · raw file

  1. <?php
  2. namespace Civi;
  3. use PDO;
  4. use PDOException;
  5. /**
  6. * Class Test
  7. *
  8. * A facade for managing the test environment.
  9. */
  10. class Test {
  11. /**
  12. * @var array
  13. */
  14. private static $singletons = [];
  15. /**
  16. * Get the data source used for testing.
  17. *
  18. * @param string|NULL $part
  19. * One of NULL, 'hostspec', 'port', 'username', 'password', 'database'.
  20. * @return string|array|NULL
  21. * If $part is omitted, return full DSN array.
  22. * If $part is a string, return that part of the DSN.
  23. */
  24. public static function dsn($part = NULL) {
  25. if (!isset(self::$singletons['dsn'])) {
  26. require_once "DB.php";
  27. self::$singletons['dsn'] = \DB::parseDSN(CIVICRM_DSN);
  28. }
  29. if ($part === NULL) {
  30. return self::$singletons['dsn'];
  31. }
  32. if (isset(self::$singletons['dsn'][$part])) {
  33. return self::$singletons['dsn'][$part];
  34. }
  35. return NULL;
  36. }
  37. /**
  38. * Get a connection to the test database.
  39. *
  40. * @return \PDO
  41. */
  42. public static function pdo() {
  43. if (!isset(self::$singletons['pdo'])) {
  44. $dsninfo = self::dsn();
  45. $host = $dsninfo['hostspec'];
  46. $port = @$dsninfo['port'];
  47. try {
  48. self::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""),
  49. $dsninfo['username'], $dsninfo['password'],
  50. [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE]
  51. );
  52. }
  53. catch (PDOException $e) {
  54. echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL;
  55. exit(1);
  56. }
  57. }
  58. return self::$singletons['pdo'];
  59. }
  60. /**
  61. * Create a builder for the headless environment.
  62. *
  63. * @return \Civi\Test\CiviEnvBuilder
  64. *
  65. * @code
  66. * \Civi\Test::headless()->apply();
  67. * \Civi\Test::headless()->sqlFile('ex.sql')->apply();
  68. * @endCode
  69. */
  70. public static function headless() {
  71. $civiRoot = dirname(__DIR__);
  72. $builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder');
  73. $builder
  74. ->callback(function ($ctx) {
  75. if (CIVICRM_UF !== 'UnitTests') {
  76. throw new \RuntimeException("\\Civi\\Test::headless() requires CIVICRM_UF=UnitTests");
  77. }
  78. $dbName = \Civi\Test::dsn('database');
  79. echo "Installing {$dbName} schema\n";
  80. \Civi\Test::schema()->dropAll();
  81. }, 'headless-drop')
  82. ->sqlFile($civiRoot . "/sql/civicrm.mysql")
  83. ->sql("DELETE FROM civicrm_extension")
  84. ->callback(function ($ctx) {
  85. \Civi\Test::data()->populate();
  86. }, 'populate');
  87. return $builder;
  88. }
  89. /**
  90. * Create a builder for end-to-end testing on the live environment.
  91. *
  92. * @return \Civi\Test\CiviEnvBuilder
  93. *
  94. * @code
  95. * \Civi\Test::e2e()->apply();
  96. * \Civi\Test::e2e()->install('foo.bar')->apply();
  97. * @endCode
  98. */
  99. public static function e2e() {
  100. $builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder');
  101. $builder
  102. ->callback(function ($ctx) {
  103. if (CIVICRM_UF === 'UnitTests') {
  104. throw new \RuntimeException("\\Civi\\Test::e2e() requires a real CMS. Found CIVICRM_UF=UnitTests.");
  105. }
  106. }, 'e2e-check');
  107. return $builder;
  108. }
  109. /**
  110. * @return \Civi\Test\Schema
  111. */
  112. public static function schema() {
  113. if (!isset(self::$singletons['schema'])) {
  114. self::$singletons['schema'] = new \Civi\Test\Schema();
  115. }
  116. return self::$singletons['schema'];
  117. }
  118. /**
  119. * @return \Civi\Test\Data
  120. */
  121. public static function data() {
  122. if (!isset(self::$singletons['data'])) {
  123. self::$singletons['data'] = new \Civi\Test\Data('CiviTesterData');
  124. }
  125. return self::$singletons['data'];
  126. }
  127. /**
  128. * Prepare and execute a batch of SQL statements.
  129. *
  130. * @param string $query
  131. * @return bool
  132. */
  133. public static function execute($query) {
  134. $pdo = \Civi\Test::pdo();
  135. $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
  136. $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
  137. $queries = preg_split('/;\s*$/m', $string);
  138. foreach ($queries as $query) {
  139. $query = trim($query);
  140. if (!empty($query)) {
  141. $result = $pdo->query($query);
  142. if ($pdo->errorCode() == 0) {
  143. continue;
  144. }
  145. else {
  146. var_dump($result);
  147. var_dump($pdo->errorInfo());
  148. // die( "Cannot execute $query: " . $pdo->errorInfo() );
  149. }
  150. }
  151. }
  152. return TRUE;
  153. }
  154. }