Browse Source

Getting tests to configure themselves

Karl Hughes 7 years ago
parent
commit
ab83ff61f1
7 changed files with 201 additions and 105 deletions
  1. 4
    1
      phpunit.xml
  2. 38
    1
      tests/MysqlTest.php
  3. 40
    1
      tests/PostgresqlTest.php
  4. 45
    1
      tests/SqlServerTest.php
  5. 35
    1
      tests/SqliteTest.php
  6. 36
    5
      tests/config.php.dist
  7. 3
    95
      tests/tests.php

+ 4
- 1
phpunit.xml View File

@@ -1,5 +1,8 @@
1 1
 <phpunit>
2 2
     <php>
3
-         <ini name="display_errors" value="true"/>
3
+        <ini name="display_errors" value="true"/>
4 4
     </php>
5
+    <testsuite name="All Tests">
6
+        <directory suffix='.php'>./tests/</directory>
7
+    </testsuite>
5 8
 </phpunit>

+ 38
- 1
tests/MysqlTest.php View File

@@ -7,6 +7,43 @@ class MysqlTest extends PHP_CRUD_API_Test
7 7
     public static function setUpBeforeClass()
8 8
     {
9 9
         self::setConfig('MySQL');
10
-        parent::setUpBeforeClass();
10
+        self::seedDatabase();
11
+    }
12
+
13
+    /**
14
+     * Seeds the database for this connection
15
+     *
16
+     * @return void
17
+     */
18
+    public function seedDatabase()
19
+    {
20
+        if (static::$config['database']=='{{test_database}}') {
21
+            die("Configure database in 'config.php' before running tests.\n");
22
+        }
23
+
24
+        $fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
25
+
26
+        $link = mysqli_connect(
27
+            static::$config['hostname'],
28
+            static::$config['username'],
29
+            static::$config['password'],
30
+            static::$config['database']
31
+        );
32
+
33
+        if (mysqli_connect_errno()) {
34
+            die("Connect failed: ".mysqli_connect_error()."\n");
35
+        }
36
+
37
+        mysqli_set_charset($link,'utf8');
38
+
39
+        $i=0;
40
+        if (mysqli_multi_query($link, file_get_contents($fixture))) {
41
+            do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
42
+        }
43
+        if (mysqli_errno($link)) {
44
+            die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
45
+        }
46
+
47
+        mysqli_close($link);
11 48
     }
12 49
 }

+ 40
- 1
tests/PostgresqlTest.php View File

@@ -7,6 +7,45 @@ class PostgresqlTest extends PHP_CRUD_API_Test
7 7
     public static function setUpBeforeClass()
8 8
     {
9 9
         static::setConfig('PostgreSQL');
10
-        parent::setUpBeforeClass();
10
+        self::seedDatabase();
11
+    }
12
+
13
+    /**
14
+     * Seeds the database for this connection
15
+     *
16
+     * @return void
17
+     */
18
+    public function seedDatabase()
19
+    {
20
+        if (static::$config['database']=='{{test_database}}') {
21
+            die("Configure database in 'config.php' before running tests.\n");
22
+        }
23
+
24
+        $fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
25
+
26
+        $e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
27
+        $hostname = $e(static::$config['hostname']);
28
+        $database = $e(static::$config['database']);
29
+        $username = $e(static::$config['username']);
30
+        $password = $e(static::$config['password']);
31
+        $conn_string = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
32
+
33
+        $db = pg_connect($conn_string);
34
+
35
+        if (!$db) {
36
+            die("Connect failed: ". pg_last_error());
37
+        }
38
+
39
+        $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
40
+        array_pop($queries);
41
+
42
+        foreach ($queries as $i=>$query) {
43
+            if (!pg_query($db, $query.';')) {
44
+                $i++;
45
+                die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
46
+            }
47
+        }
48
+
49
+        pg_close($db);
11 50
     }
12 51
 }

+ 45
- 1
tests/SqlServerTest.php View File

@@ -7,6 +7,50 @@ class SqlServerTest extends PHP_CRUD_API_Test
7 7
     public static function setUpBeforeClass()
8 8
     {
9 9
         self::setConfig('SQLServer');
10
-        parent::setUpBeforeClass();
10
+
11
+        if (static::$config['database']=='{{test_database}}') {
12
+            die("Configure database in 'config.php' before running tests.\n");
13
+        }
14
+
15
+        self::seedDatabase();
16
+    }
17
+
18
+    /**
19
+     * Seeds the database for this connection
20
+     *
21
+     * @return void
22
+     */
23
+    public function seedDatabase()
24
+    {
25
+        if (static::$config['database']=='{{test_database}}') {
26
+            die("Configure database in 'config.php' before running tests.\n");
27
+        }
28
+
29
+        $fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
30
+
31
+        $connectionInfo = [
32
+            'UID' => static::$config['username'],
33
+            'PWD' => static::$config['password'],
34
+            'Database' => static::$config['database'],
35
+            'CharacterSet' => static::$config['UTF-8'],
36
+        ];
37
+
38
+        $conn = sqlsrv_connect(static::$config['hostname'], $connectionInfo);
39
+
40
+        if (!$conn) {
41
+            die("Connect failed: ".print_r( sqlsrv_errors(), true));
42
+        }
43
+
44
+        $queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
45
+        array_pop($queries);
46
+
47
+        foreach ($queries as $i=>$query) {
48
+            if (!sqlsrv_query($conn, $query)) {
49
+                $i++;
50
+                die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
51
+            }
52
+        }
53
+
54
+        sqlsrv_close($conn);
11 55
     }
12 56
 }

+ 35
- 1
tests/SqliteTest.php View File

@@ -7,6 +7,40 @@ class SqliteTest extends PHP_CRUD_API_Test
7 7
     public static function setUpBeforeClass()
8 8
     {
9 9
         self::setConfig('SQLite');
10
-        parent::setUpBeforeClass();
10
+        self::seedDatabase();
11
+    }
12
+
13
+    /**
14
+     * Seeds the database for this connection
15
+     *
16
+     * @return void
17
+     */
18
+    public function seedDatabase()
19
+    {
20
+        if (static::$config['database']=='{{test_database}}') {
21
+            die("Configure database in 'config.php' before running tests.\n");
22
+        }
23
+
24
+        $database = static::$config['database'];
25
+
26
+        $fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
27
+
28
+        $db = new SQLite3($database);
29
+
30
+        if (!$db) {
31
+            die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
32
+        }
33
+
34
+        $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
35
+        array_pop($queries);
36
+
37
+        foreach ($queries as $i=>$query) {
38
+            if (!$db->query($query.';')) {
39
+                $i++;
40
+                die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
41
+            }
42
+        }
43
+
44
+        $db->close();
11 45
     }
12 46
 }

+ 36
- 5
tests/config.php.dist View File

@@ -2,9 +2,40 @@
2 2
 
3 3
 class PHP_CRUD_API_Config
4 4
 {
5
-	public static $dbengine='MySQL';             // 'MySQL', 'SQLServer', 'PostgreSQL' or 'SQLite'
6
-	public static $hostname='{{test_hostname}}'; // 'localhost' for MySQL, '(Local)' for SQLServer, empty for SQLite
7
-	public static $username='{{test_username}}'; // May be empty on SQLServer or SQLite
8
-	public static $password='{{test_password}}'; // May be empty on SQLServer or SQLite
9
-	public static $database='{{test_database}}'; // NB: Use an empty database, data will be LOST!
5
+    /**
6
+     * Configure one or more database connections as associative arrays.
7
+     * Tests will be run against any database connection specified here.
8
+     *
9
+     * @var array
10
+     */
11
+    public static $config = [
12
+        [
13
+            'dbengine' => 'MySQL',              // 'MySQL', 'SQLServer', 'PostgreSQL' or 'SQLite'
14
+            'hostname' => '{{test_hostname}}',  // 'localhost' for MySQL, '(Local)' for SQLServer, empty for SQLite
15
+            'username' => '{{test_username}}',  // May be empty on SQLServer or SQLite
16
+            'password' => '{{test_password}}',  // May be empty on SQLServer or SQLite
17
+            'database' => '{{test_database}}',  // NB: Use an empty database, data will be LOST!
18
+        ],
19
+        [
20
+            'dbengine' => 'PostgreSQL',
21
+            'hostname' => '',
22
+            'username' => '',
23
+            'password' => '',
24
+            'database' => '',
25
+        ],
26
+        [
27
+            'dbengine' => 'SQLite',
28
+            'hostname' => '',
29
+            'username' => '',
30
+            'password' => '',
31
+            'database' => '',
32
+        ],
33
+        [
34
+            'dbengine' => 'SQLServer',
35
+            'hostname' => '',
36
+            'username' => '',
37
+            'password' => '',
38
+            'database' => '',
39
+        ],
40
+    ];
10 41
 }

+ 3
- 95
tests/tests.php View File

@@ -119,100 +119,9 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
119 119
 {
120 120
     public static $config;
121 121
 
122
-    public static function setUpBeforeClass()
123
-    {
124
-        if (static::$config['database']=='{{test_database}}') {
125
-            die("Configure database in 'config.php' before running tests.\n");
126
-        }
127
-
128
-        $dbengine = static::$config['dbengine'];
129
-        $hostname = static::$config['hostname'];
130
-        $username = static::$config['username'];
131
-        $password = static::$config['password'];
132
-        $database = static::$config['database'];
133
-
134
-        $fixture = __DIR__.'/data/blog_'.strtolower($dbengine).'.sql';
135
-
136
-        if ($dbengine == 'MySQL') {
137
-
138
-            $link = mysqli_connect($hostname, $username, $password, $database);
139
-            if (mysqli_connect_errno()) {
140
-                die("Connect failed: ".mysqli_connect_error()."\n");
141
-            }
142
-            mysqli_set_charset($link,'utf8');
143
-
144
-            $i=0;
145
-            if (mysqli_multi_query($link, file_get_contents($fixture))) {
146
-                do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
147
-            }
148
-            if (mysqli_errno($link)) {
149
-                die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
150
-            }
151
-
152
-            mysqli_close($link);
153
-
154
-        } elseif ($dbengine == 'SQLServer') {
155
-
156
-            $connectionInfo = array();
157
-            $connectionInfo['UID']=$username;
158
-            $connectionInfo['PWD']=$password;
159
-            $connectionInfo['Database']=$database;
160
-            $connectionInfo['CharacterSet']='UTF-8';
161
-            $conn = sqlsrv_connect( $hostname, $connectionInfo);
162
-            if (!$conn) {
163
-                die("Connect failed: ".print_r( sqlsrv_errors(), true));
164
-            }
165
-            $queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
166
-            array_pop($queries);
167
-            foreach ($queries as $i=>$query) {
168
-                if (!sqlsrv_query($conn, $query)) {
169
-                    $i++;
170
-                    die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
171
-                }
172
-            }
173
-            sqlsrv_close($conn);
174
-
175
-        } elseif ($dbengine == 'PostgreSQL') {
176
-
177
-            $e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
178
-            $hostname = $e($hostname);
179
-            $database = $e($database);
180
-            $username = $e($username);
181
-            $password = $e($password);
182
-            $conn_string = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
183
-            $db = pg_connect($conn_string);
184
-            if (!$db) {
185
-                die("Connect failed: ". pg_last_error());
186
-            }
187
-            $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
188
-            array_pop($queries);
189
-            foreach ($queries as $i=>$query) {
190
-                if (!pg_query($db, $query.';')) {
191
-                    $i++;
192
-                    die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
193
-                }
194
-            }
195
-            pg_close($db);
196
-
197
-        } elseif ($dbengine == 'SQLite') {
198
-
199
-            $db = new SQLite3($database);
200
-            if (!$db) {
201
-                die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
202
-            }
203
-            $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
204
-            array_pop($queries);
205
-            foreach ($queries as $i=>$query) {
206
-                if (!$db->query($query.';')) {
207
-                    $i++;
208
-                    die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
209
-                }
210
-            }
211
-            $db->close();
212
-        }
213
-    }
122
+    public abstract function seedDatabase();
214 123
 
215
-    public function setConfig($dbengine = '')
124
+    protected function setConfig($dbengine = '')
216 125
     {
217 126
         foreach (PHP_CRUD_API_Config::$config as $database) {
218 127
             if ($database['dbengine'] == $dbengine) {
@@ -220,11 +129,10 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
220 129
                 return true;
221 130
             }
222 131
         }
223
-        self::markTestSkipped();
132
+        self::markTestSkipped("Configuration for {$dbengine} database not found.");
224 133
         return false;
225 134
     }
226 135
 
227
-    /** @group only */
228 136
     public function testListPosts()
229 137
     {
230 138
         $test = new API($this, static::$config);

Loading…
Cancel
Save