10 * A facade for managing the test environment.
17 private static $singletons = array();
20 * Get the data source used for testing.
22 * @param string|NULL $part
23 * One of NULL, 'hostspec', 'port', 'username', 'password', 'database'.
24 * @return string|array|NULL
25 * If $part is omitted, return full DSN array.
26 * If $part is a string, return that part of the DSN.
28 public static function dsn($part = NULL) {
29 if (!isset(self
::$singletons['dsn'])) {
30 require_once "DB.php";
31 self
::$singletons['dsn'] = \DB
::parseDSN(CIVICRM_DSN
);
35 return self
::$singletons['dsn'];
38 if (isset(self
::$singletons['dsn'][$part])) {
39 return self
::$singletons['dsn'][$part];
46 * Get a connection to the test database.
50 public static function pdo() {
51 if (!isset(self
::$singletons['pdo'])) {
52 $dsninfo = self
::dsn();
53 $host = $dsninfo['hostspec'];
54 $port = @$dsninfo['port'];
56 self
::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ?
";port=$port" : ""),
57 $dsninfo['username'], $dsninfo['password'],
58 array(PDO
::MYSQL_ATTR_USE_BUFFERED_QUERY
=> TRUE)
61 catch (PDOException
$e) {
62 echo "Can't connect to MySQL server:" . PHP_EOL
. $e->getMessage() . PHP_EOL
;
66 return self
::$singletons['pdo'];
70 * Create a builder for the headless environment.
72 * @return \Civi\Test\CiviEnvBuilder
75 * \Civi\Test::headless()->apply();
76 * \Civi\Test::headless()->sqlFile('ex.sql')->apply();
79 public static function headless() {
80 $civiRoot = dirname(__DIR__
);
81 $builder = new \Civi\Test\
CiviEnvBuilder('CiviEnvBuilder');
83 ->callback(function ($ctx) {
84 if (CIVICRM_UF
!== 'UnitTests') {
85 throw new \
RuntimeException("\\Civi\\Test::headless() requires CIVICRM_UF=UnitTests");
87 $dbName = \Civi\Test
::dsn('database');
88 echo "Installing {$dbName} schema\n";
89 \Civi\Test
::schema()->dropAll();
91 ->sqlFile($civiRoot . "/sql/civicrm.mysql")
92 ->sql("DELETE FROM civicrm_extension")
93 ->callback(function ($ctx) {
94 \Civi\Test
::data()->populate();
100 * Create a builder for end-to-end testing on the live environment.
102 * @return \Civi\Test\CiviEnvBuilder
105 * \Civi\Test::e2e()->apply();
106 * \Civi\Test::e2e()->install('foo.bar')->apply();
109 public static function e2e() {
110 $builder = new \Civi\Test\
CiviEnvBuilder('CiviEnvBuilder');
112 ->callback(function ($ctx) {
113 if (CIVICRM_UF
=== 'UnitTests') {
114 throw new \
RuntimeException("\\Civi\\Test::e2e() requires a real CMS. Found CIVICRM_UF=UnitTests.");
121 * @return \Civi\Test\Schema
123 public static function schema() {
124 if (!isset(self
::$singletons['schema'])) {
125 self
::$singletons['schema'] = new \Civi\Test\
Schema();
127 return self
::$singletons['schema'];
132 * @return \Civi\Test\Data
134 public static function data() {
135 if (!isset(self
::$singletons['data'])) {
136 self
::$singletons['data'] = new \Civi\Test\
Data('CiviTesterData');
138 return self
::$singletons['data'];
142 * Prepare and execute a batch of SQL statements.
144 * @param string $query
147 public static function execute($query) {
148 $pdo = \Civi\Test
::pdo();
150 $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
151 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
153 $queries = preg_split('/;\s*$/m', $string);
154 foreach ($queries as $query) {
155 $query = trim($query);
156 if (!empty($query)) {
157 $result = $pdo->query($query);
158 if ($pdo->errorCode() == 0) {
163 var_dump($pdo->errorInfo());
164 // die( "Cannot execute $query: " . $pdo->errorInfo() );