Skip to content

Commit f8bc3a7

Browse files
authored
Merge pull request #235 from karllhughes/master
Running tests over multiple DB connections
2 parents 3073411 + bc8ffe6 commit f8bc3a7

12 files changed

+1006
-842
lines changed

README.md

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -832,75 +832,63 @@ I am testing mainly on Ubuntu and I have the following test setups:
832832

833833
This should cover most environments, but please notify me of failing tests and report your environment.
834834

835-
### MySQL on Linux
835+
### MySQL, PostgreSQL, and SQLite on Linux
836836

837-
There are PHPUnit tests in the file 'tests.php'. You need to configure your test database connection in this file. After that run:
837+
The tests are held in the `tests.php` file, but first you should copy the `config.php.dist` file to `config.php` and add your database credentials. You can add credentials for one or all the databases supported.
838+
839+
After configuring the database connections, use PHPUnit to run all the tests:
838840

839841
```
840842
$ wget https://phar.phpunit.de/phpunit.phar
841-
$ php phpunit.phar tests/tests.php
842-
PHPUnit 5.3.2 by Sebastian Bergmann and contributors.
843+
$ php phpunit.phar
844+
PHPUnit 5.7.17 by Sebastian Bergmann and contributors.
843845
844-
.................................................. 50 / 50 (100%)
846+
............................................................... 63 / 304 ( 20%)
847+
............................................................... 126 / 304 ( 41%)
848+
..........................SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 189 / 304 ( 62%)
849+
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS........................ 252 / 304 ( 82%)
850+
.................................................... 304 / 304 (100%)
845851
846-
Time: 495 ms, Memory: 10.00Mb
852+
Time: 11.16 seconds, Memory: 12.00MB
847853
848-
OK (50 tests, 75 assertions)
854+
OK, but incomplete, skipped, or risky tests!
855+
Tests: 6004, Assertions: 338, Skipped: 76.
849856
$
850857
```
851858

852-
NB: You MUST use an empty database as a desctructive database fixture ('blog_mysql.sql') is loaded.
853-
854-
### SQL server on Windows:
855-
856-
```
857-
C:\php-crud-api>"C:\PHP\php.exe" phpunit.phar tests\tests.php
858-
PHPUnit 5.3.2 by Sebastian Bergmann and contributors.
859-
860-
.................................................. 50 / 50 (100%)
861-
862-
Time: 1.47 seconds, Memory: 6.75Mb
863-
864-
OK (50 tests, 73 assertions)
859+
You can also run tests for only one database at a time if you'd like. For example to run MySQL tests, specify the `MysqlTest.php` file:
865860

866-
C:\php-crud-api>
867861
```
862+
$ php phpunit.phar tests/MysqlTest.php
863+
PHPUnit 5.7.17 by Sebastian Bergmann and contributors.
868864
869-
NB: You MUST use an empty database as a desctructive database fixture ('blog_sqlserver.sql') is loaded.
865+
................................................................. 65 / 76 ( 85%)
866+
........... 76 / 76 (100%)
870867
871-
### PostgreSQL on Linux
868+
Time: 3.54 seconds, Memory: 10.00MB
872869
873-
```
874-
$ wget https://phar.phpunit.de/phpunit.phar
875-
$ php phpunit.phar tests/tests.php
876-
PHPUnit 5.3.2 by Sebastian Bergmann and contributors.
877-
878-
.................................................. 50 / 50 (100%)
879-
880-
Time: 1.09 seconds, Memory: 8.00Mb
881-
882-
OK (50 tests, 75 assertions)
870+
OK (76 tests, 113 assertions)
883871
$
884872
```
885873

886-
NB: You MUST use an empty database as a desctructive database fixture ('blog_postgresql.sql') is loaded.
874+
NB: You MUST use an empty database as a destructive database fixture is loaded.
887875

888-
### SQLite on Linux
876+
### SQL server on Windows:
889877

890878
```
891-
$ wget https://phar.phpunit.de/phpunit.phar
892-
$ php phpunit.phar tests/tests.php
879+
C:\php-crud-api>"C:\PHP\php.exe" phpunit.phar
893880
PHPUnit 5.3.2 by Sebastian Bergmann and contributors.
894881
895882
.................................................. 50 / 50 (100%)
896883
897-
Time: 1.96 seconds, Memory: 8.00Mb
884+
Time: 1.47 seconds, Memory: 6.75Mb
898885
899-
OK (50 tests, 74 assertions)
900-
$
886+
OK (50 tests, 73 assertions)
887+
888+
C:\php-crud-api>
901889
```
902890

903-
NB: You MUST use an empty database as a desctructive database fixture ('blog_sqlite.sql') is loaded.
891+
NB: You MUST use an empty database as a desctructive database fixture ('blog_sqlserver.sql') is loaded.
904892

905893
## Installing MySQL on Ubuntu Linux
906894

phpunit.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<phpunit>
22
<php>
3-
<ini name="display_errors" value="true"/>
3+
<ini name="display_errors" value="true"/>
44
</php>
5+
<testsuite name="All Tests">
6+
<directory suffix='.php'>./tests/</directory>
7+
</testsuite>
58
</phpunit>

tests/MysqlTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/tests.php');
4+
5+
class MysqlTest extends PHP_CRUD_API_Test
6+
{
7+
public static function setUpBeforeClass()
8+
{
9+
self::setConfig('MySQL');
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);
48+
}
49+
}

tests/PostgresqlTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/tests.php');
4+
5+
class PostgresqlTest extends PHP_CRUD_API_Test
6+
{
7+
public static function setUpBeforeClass()
8+
{
9+
static::setConfig('PostgreSQL');
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);
50+
}
51+
}

tests/SqlServerTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/tests.php');
4+
5+
class SqlServerTest extends PHP_CRUD_API_Test
6+
{
7+
public static function setUpBeforeClass()
8+
{
9+
self::setConfig('SQLServer');
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);
55+
}
56+
}

tests/SqliteTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/tests.php');
4+
5+
class SqliteTest extends PHP_CRUD_API_Test
6+
{
7+
public static function setUpBeforeClass()
8+
{
9+
self::setConfig('SQLite');
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();
45+
}
46+
}

tests/config.php.dist

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,42 @@
22

33
class PHP_CRUD_API_Config
44
{
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+
/* Uncomment and update for any databases you want to use.
20+
[
21+
'dbengine' => 'PostgreSQL',
22+
'hostname' => '',
23+
'username' => '',
24+
'password' => '',
25+
'database' => '',
26+
],
27+
[
28+
'dbengine' => 'SQLite',
29+
'hostname' => '',
30+
'username' => '',
31+
'password' => '',
32+
'database' => '',
33+
],
34+
[
35+
'dbengine' => 'SQLServer',
36+
'hostname' => '',
37+
'username' => '',
38+
'password' => '',
39+
'database' => '',
40+
],
41+
*/
42+
];
1043
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)