Skip to content

Commit ab83ff6

Browse files
committed
Getting tests to configure themselves
1 parent 61a8c23 commit ab83ff6

File tree

7 files changed

+201
-105
lines changed

7 files changed

+201
-105
lines changed

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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,43 @@ class MysqlTest extends PHP_CRUD_API_Test
77
public static function setUpBeforeClass()
88
{
99
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);
1148
}
1249
}

tests/PostgresqlTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,45 @@ class PostgresqlTest extends PHP_CRUD_API_Test
77
public static function setUpBeforeClass()
88
{
99
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);
1150
}
1251
}

tests/SqlServerTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,50 @@ class SqlServerTest extends PHP_CRUD_API_Test
77
public static function setUpBeforeClass()
88
{
99
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);
1155
}
1256
}

tests/SqliteTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,40 @@ class SqliteTest extends PHP_CRUD_API_Test
77
public static function setUpBeforeClass()
88
{
99
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();
1145
}
1246
}

tests/config.php.dist

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,40 @@
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+
[
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+
];
1041
}

tests/tests.php

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -119,112 +119,20 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
119119
{
120120
public static $config;
121121

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();
214123

215-
public function setConfig($dbengine = '')
124+
protected function setConfig($dbengine = '')
216125
{
217126
foreach (PHP_CRUD_API_Config::$config as $database) {
218127
if ($database['dbengine'] == $dbengine) {
219128
static::$config = $database;
220129
return true;
221130
}
222131
}
223-
self::markTestSkipped();
132+
self::markTestSkipped("Configuration for {$dbengine} database not found.");
224133
return false;
225134
}
226135

227-
/** @group only */
228136
public function testListPosts()
229137
{
230138
$test = new API($this, static::$config);

0 commit comments

Comments
 (0)