Browse Source

Prepare for #194

Maurits van der Schee 7 years ago
parent
commit
4d43f8f3d6
9 changed files with 286 additions and 118 deletions
  1. 1
    1
      .travis.yml
  2. 1
    1
      tests/Config.php.dist
  3. 1
    1
      tests/Config.php.travis
  4. 67
    27
      tests/MysqlTest.php
  5. 58
    24
      tests/PostgresqlTest.php
  6. 55
    25
      tests/SqlServerTest.php
  7. 54
    20
      tests/SqliteTest.php
  8. 46
    0
      tests/TestBase.php
  9. 3
    19
      tests/Tests.php

+ 1
- 1
.travis.yml View File

@@ -22,7 +22,7 @@ before_install:
22 22
   - psql -c 'create database testing;' -U postgres
23 23
   - psql -U postgres -c "create extension postgis"
24 24
   # - mysql -e 'CREATE DATABASE testing;'
25
-  - cp tests/config.php.travis tests/config.php
25
+  - cp tests/Config.php.travis tests/Config.php
26 26
 
27 27
 script:
28 28
   - curl https://phar.phpunit.de/phpunit-4.8.phar -L -o phpunit.phar && chmod +x phpunit.phar

tests/config.php.dist → tests/Config.php.dist View File

@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3
-class PHP_CRUD_API_Config
3
+class Config
4 4
 {
5 5
     /**
6 6
      * Configure one or more database connections as associative arrays.

tests/config.php.travis → tests/Config.php.travis View File

@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3
-class PHP_CRUD_API_Config
3
+class Config
4 4
 {
5 5
     /**
6 6
      * Configure one or more database connections as associative arrays.

+ 67
- 27
tests/MysqlTest.php View File

@@ -1,49 +1,89 @@
1 1
 <?php
2 2
 
3
-require_once(__DIR__ . '/tests.php');
3
+require_once(__DIR__ . '/Tests.php');
4 4
 
5
-class MysqlTest extends PHP_CRUD_API_Test
5
+class MysqlTest extends Tests
6 6
 {
7
-    public static function setUpBeforeClass()
7
+    /**
8
+     * Connects to the Database
9
+     *
10
+     * @return object Database connection
11
+     */
12
+    public function connect($config)
13
+    {
14
+        $db = mysqli_connect(
15
+            $config['hostname'],
16
+            $config['username'],
17
+            $config['password'],
18
+            $config['database']
19
+        );
20
+
21
+        if (mysqli_connect_errno()) {
22
+            die("Connect failed: ".mysqli_connect_error()."\n");
23
+        }
24
+
25
+        mysqli_set_charset($db,'utf8');
26
+
27
+        return $db;
28
+    }
29
+
30
+    /**
31
+     * Disconnects from the Database
32
+     *
33
+     * @return boolean Success
34
+     */
35
+    public function disconnect($db)
8 36
     {
9
-        self::setConfig('MySQL');
10
-        self::seedDatabase();
37
+        return mysqli_close($db);
11 38
     }
12 39
 
13 40
     /**
14
-     * Seeds the database for this connection
41
+     * Checks the version of the Database
15 42
      *
16 43
      * @return void
17 44
      */
18
-    public function seedDatabase()
45
+    public function checkVersion($db)
19 46
     {
20
-        if (static::$config['database']=='{{test_database}}') {
21
-            die("Configure database in 'config.php' before running tests.\n");
47
+        $major = 5;
48
+        $minor = 5;
49
+        $version = mysqli_get_server_version($db);
50
+        $v = array(floor($version/10000),floor($version/100));
51
+        if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
52
+            die("Detected MySQL $v[0].$v[1], but only $major.$minor and up are supported\n");
22 53
         }
54
+    }
23 55
 
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");
56
+    /**
57
+     * Gets the capabilities of the Database
58
+     *
59
+     * @return int Capabilites
60
+     */
61
+    public function getCapabilities($db)
62
+    {
63
+        $capabilities = 0;
64
+        $version = mysqli_get_server_version($db);
65
+        if ($version>50600) {
66
+            $capabilities |= self::GIS;
35 67
         }
68
+        return $capabilities;
69
+    }
36 70
 
37
-        mysqli_set_charset($link,'utf8');
71
+    /**
72
+     * Seeds the database for this connection
73
+     *
74
+     * @return void
75
+     */
76
+    public function seedDatabase($db, $capabilities)
77
+    {
78
+        $fixture = __DIR__.'/data/blog_mysql.sql';
38 79
 
39 80
         $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");
81
+        if (mysqli_multi_query($db, file_get_contents($fixture))) {
82
+            do { $i++; mysqli_next_result($db); } while (mysqli_more_results($db));
45 83
         }
46 84
 
47
-        mysqli_close($link);
85
+        if (mysqli_errno($db)) {
86
+            die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($db)."\n");
87
+        }
48 88
     }
49 89
 }

+ 58
- 24
tests/PostgresqlTest.php View File

@@ -1,41 +1,77 @@
1 1
 <?php
2 2
 
3
-require_once(__DIR__ . '/tests.php');
3
+require_once(__DIR__ . '/Tests.php');
4 4
 
5
-class PostgresqlTest extends PHP_CRUD_API_Test
5
+class PostgresqlTest extends Tests
6 6
 {
7
-    public static function setUpBeforeClass()
7
+    /**
8
+     * Connects to the Database
9
+     *
10
+     * @return object Database connection
11
+     */
12
+    public function connect($config)
8 13
     {
9
-        static::setConfig('PostgreSQL');
10
-        self::seedDatabase();
14
+        $e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
15
+        $hostname = $e($config['hostname']);
16
+        $database = $e($config['database']);
17
+        $username = $e($config['username']);
18
+        $password = $e($config['password']);
19
+        $connectionString = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
20
+
21
+        return pg_connect($connectionString);
11 22
     }
12 23
 
13 24
     /**
14
-     * Seeds the database for this connection
25
+     * Disconnects from the Database
26
+     *
27
+     * @return boolean Success
28
+     */
29
+    public function disconnect($db)
30
+    {
31
+        return pg_close($db);
32
+    }
33
+
34
+    /**
35
+     * Checks the version of the Database
15 36
      *
16 37
      * @return void
17 38
      */
18
-    public function seedDatabase()
39
+    public function checkVersion($db)
19 40
     {
20
-        if (static::$config['database']=='{{test_database}}') {
21
-            die("Configure database in 'config.php' before running tests.\n");
41
+        $major = 9;
42
+        $minor = 1;
43
+        $version = pg_version();
44
+        $v = explode('.',$version['server']);
45
+        if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
46
+            die("Detected PostgreSQL $v[0].$v[1], but only $major.$minor and up are supported\n");
22 47
         }
48
+    }
23 49
 
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());
50
+    /**
51
+     * Gets the capabilities of the Database
52
+     *
53
+     * @return int Capabilites
54
+     */
55
+    public function getCapabilities($db)
56
+    {
57
+        $capabilities = 0;
58
+        $extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
59
+        foreach ($extensions as $extension) {
60
+          if ($extension['extname'] === 'postgis') {
61
+            $capabilities |= self::GIS;
62
+          }
37 63
         }
64
+        return $capabilities;
65
+    }
38 66
 
67
+    /**
68
+     * Seeds the database for this connection
69
+     *
70
+     * @return void
71
+     */
72
+    public function seedDatabase($db,$capabilities)
73
+    {
74
+        $fixture = __DIR__.'/data/blog_postgresql.sql';
39 75
         $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
40 76
         array_pop($queries);
41 77
 
@@ -45,7 +81,5 @@ class PostgresqlTest extends PHP_CRUD_API_Test
45 81
                 die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
46 82
             }
47 83
         }
48
-
49
-        pg_close($db);
50 84
     }
51 85
 }

+ 55
- 25
tests/SqlServerTest.php View File

@@ -1,45 +1,77 @@
1 1
 <?php
2 2
 
3
-require_once(__DIR__ . '/tests.php');
3
+require_once(__DIR__ . '/Tests.php');
4 4
 
5
-class SqlServerTest extends PHP_CRUD_API_Test
5
+class SqlServerTest extends Tests
6 6
 {
7
-    public static function setUpBeforeClass()
7
+    /**
8
+     * Connects to the Database
9
+     *
10
+     * @return object Database connection
11
+     */
12
+    public function connect($config)
8 13
     {
9
-        self::setConfig('SQLServer');
14
+        $connectionInfo = array(
15
+            'UID' => $config['username'],
16
+            'PWD' => $config['password'],
17
+            'Database' => $config['database'],
18
+            'CharacterSet' => 'UTF-8',
19
+        );
20
+
21
+        $db = sqlsrv_connect($config['hostname'], $dbectionInfo);
10 22
 
11
-        if (static::$config['database']=='{{test_database}}') {
12
-            die("Configure database in 'config.php' before running tests.\n");
23
+        if (!$db) {
24
+            die("Connect failed: ".print_r( sqlsrv_errors(), true));
13 25
         }
14 26
 
15
-        self::seedDatabase();
27
+        return $db;
16 28
     }
17 29
 
18 30
     /**
19
-     * Seeds the database for this connection
31
+     * Disconnects from the Database
32
+     *
33
+     * @return boolean Success
34
+     */
35
+    public function disconnect($db)
36
+    {
37
+        return sqlsrv_close($db);
38
+    }
39
+
40
+    /**
41
+     * Checks the version of the Database
20 42
      *
21 43
      * @return void
22 44
      */
23
-    public function seedDatabase()
45
+    public function checkVersion($db)
24 46
     {
25
-        if (static::$config['database']=='{{test_database}}') {
26
-            die("Configure database in 'config.php' before running tests.\n");
47
+        $major = 5;
48
+        $minor = 5;
49
+        $version = sqlsrv_server_info($db);
50
+        $v = explode('.',$version['SQLServerVersion']);
51
+        if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
52
+            die("Detected MySQL $v[0].$v[1], but only $major.$minor and up are supported\n");
27 53
         }
54
+    }
28 55
 
29
-        $fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
30
-
31
-        $connectionInfo = array(
32
-            'UID' => static::$config['username'],
33
-            'PWD' => static::$config['password'],
34
-            'Database' => static::$config['database'],
35
-            'CharacterSet' => 'UTF-8',
36
-        );
56
+    /**
57
+     * Gets the capabilities of the Database
58
+     *
59
+     * @return int Capabilites
60
+     */
61
+    public function getCapabilities($db)
62
+    {
63
+        return self::GIS;
64
+    }
37 65
 
38
-        $conn = sqlsrv_connect(static::$config['hostname'], $connectionInfo);
39 66
 
40
-        if (!$conn) {
41
-            die("Connect failed: ".print_r( sqlsrv_errors(), true));
42
-        }
67
+    /**
68
+     * Seeds the database for this connection
69
+     *
70
+     * @return void
71
+     */
72
+    public function seedDatabase($db, $capabilities)
73
+    {
74
+        $fixture = __DIR__.'/data/blog_sqlserver.sql';
43 75
 
44 76
         $queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
45 77
         array_pop($queries);
@@ -50,7 +82,5 @@ class SqlServerTest extends PHP_CRUD_API_Test
50 82
                 die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
51 83
             }
52 84
         }
53
-
54
-        sqlsrv_close($conn);
55 85
     }
56 86
 }

+ 54
- 20
tests/SqliteTest.php View File

@@ -1,35 +1,71 @@
1 1
 <?php
2 2
 
3
-require_once(__DIR__ . '/tests.php');
3
+require_once(__DIR__ . '/Tests.php');
4 4
 
5
-class SqliteTest extends PHP_CRUD_API_Test
5
+class SqliteTest extends Tests
6 6
 {
7
-    public static function setUpBeforeClass()
7
+    /**
8
+     * Connects to the Database
9
+     *
10
+     * @return object Database connection
11
+     */
12
+    public function connect($config)
8 13
     {
9
-        self::setConfig('SQLite');
10
-        self::seedDatabase();
14
+        $db = new SQLite3($config['database']);
15
+
16
+        if (!$db) {
17
+            die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
18
+        }
19
+
20
+        return $db;
11 21
     }
12 22
 
13 23
     /**
14
-     * Seeds the database for this connection
24
+     * Disconnects from the Database
25
+     *
26
+     * @return boolean Success
27
+     */
28
+    public function disconnect($db)
29
+    {
30
+        return $db->close();
31
+    }
32
+
33
+    /**
34
+     * Checks the version of the Database
15 35
      *
16 36
      * @return void
17 37
      */
18
-    public function seedDatabase()
38
+    public function checkVersion($db)
19 39
     {
20
-        if (static::$config['database']=='{{test_database}}') {
21
-            die("Configure database in 'config.php' before running tests.\n");
40
+        $major = 3;
41
+        $minor = 0;
42
+        $version = SQLite3::version();
43
+        $v = explode('.',$version['versionString']);
44
+        if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
45
+            die("Detected SQLite $v[0].$v[1], but only $major.$minor and up are supported\n");
22 46
         }
47
+    }
23 48
 
24
-        $database = static::$config['database'];
25
-
26
-        $fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
27
-
28
-        $db = new SQLite3($database);
49
+    /**
50
+     * Gets the capabilities of the Database
51
+     *
52
+     * @return int Capabilites
53
+     */
54
+    public function getCapabilities($db)
55
+    {
56
+        $capabilities = 0;
57
+        
58
+        return $capabilities;
59
+    }
29 60
 
30
-        if (!$db) {
31
-            die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
32
-        }
61
+    /**
62
+     * Seeds the database for this connection
63
+     *
64
+     * @return void
65
+     */
66
+    public function seedDatabase($db, $capabilities)
67
+    {
68
+        $fixture = __DIR__.'/data/blog_sqlite.sql';
33 69
 
34 70
         $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
35 71
         array_pop($queries);
@@ -40,7 +76,5 @@ class SqliteTest extends PHP_CRUD_API_Test
40 76
                 die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
41 77
             }
42 78
         }
43
-
44
-        $db->close();
45 79
     }
46
-}
80
+}

+ 46
- 0
tests/TestBase.php View File

@@ -0,0 +1,46 @@
1
+<?php
2
+
3
+require_once(__DIR__ . '/Config.php');
4
+
5
+abstract class TestBase extends PHPUnit_Framework_TestCase
6
+{
7
+    protected function getConfig()
8
+    {
9
+        $dbengine = strtolower(substr(get_called_class(),0,-4));
10
+        foreach (Config::$config as $database) {
11
+            if (strtolower($database['dbengine']) == $dbengine) {
12
+                if ($database['database']!='{{test_database}}') {
13
+                    return $database;
14
+                }
15
+            }
16
+        }
17
+        self::markTestSkipped("Configuration for '{$dbengine}' in 'Config.php' not found.");
18
+        return false;
19
+    }
20
+
21
+    public static function setUpBeforeClass()
22
+    {
23
+        $config = self::getConfig();
24
+        $db = static::connect($config);
25
+        static::checkVersion($db);
26
+        $capabilities = static::getCapabilities($db);
27
+        static::seedDatabase($db,$capabilities);
28
+        static::disconnect($db);
29
+        // set params for test
30
+        static::$config = $config;
31
+        static::$capabilities = $capabilities;
32
+    }
33
+
34
+    public static $config;
35
+    public static $capabilities;
36
+
37
+    const GIS = 1;
38
+    const JSON = 2;
39
+
40
+    public abstract function checkVersion($db);
41
+
42
+    public abstract function getCapabilities($db);
43
+
44
+    public abstract function seedDatabase($db,$capabilities);
45
+
46
+}

tests/tests.php → tests/Tests.php View File

@@ -1,7 +1,7 @@
1 1
 <?php
2 2
 
3
-require __DIR__.'/config.php';
4
-require __DIR__.'/../api.php';
3
+require_once(__DIR__ . '/TestBase.php');
4
+require_once(__DIR__ . '/../api.php');
5 5
 
6 6
 class API
7 7
 {
@@ -113,24 +113,8 @@ class API
113 113
     }
114 114
 }
115 115
 
116
-abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
116
+abstract class Tests extends TestBase
117 117
 {
118
-    public static $config;
119
-
120
-    public abstract function seedDatabase();
121
-
122
-    protected function setConfig($dbengine = '')
123
-    {
124
-        foreach (PHP_CRUD_API_Config::$config as $database) {
125
-            if ($database['dbengine'] == $dbengine) {
126
-                static::$config = $database;
127
-                return true;
128
-            }
129
-        }
130
-        self::markTestSkipped("Configuration for {$dbengine} database not found.");
131
-        return false;
132
-    }
133
-
134 118
     public function testListPosts()
135 119
     {
136 120
         $test = new API($this, static::$config);

Loading…
Cancel
Save