Skip to content

Commit 4d43f8f

Browse files
committed
Prepare for #194
1 parent 94922d7 commit 4d43f8f

File tree

9 files changed

+286
-118
lines changed

9 files changed

+286
-118
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ before_install:
2222
- psql -c 'create database testing;' -U postgres
2323
- psql -U postgres -c "create extension postgis"
2424
# - mysql -e 'CREATE DATABASE testing;'
25-
- cp tests/config.php.travis tests/config.php
25+
- cp tests/Config.php.travis tests/Config.php
2626

2727
script:
2828
- curl https://phar.phpunit.de/phpunit-4.8.phar -L -o phpunit.phar && chmod +x phpunit.phar

tests/config.php.dist renamed to tests/Config.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class PHP_CRUD_API_Config
3+
class Config
44
{
55
/**
66
* Configure one or more database connections as associative arrays.

tests/config.php.travis renamed to tests/Config.php.travis

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class PHP_CRUD_API_Config
3+
class Config
44
{
55
/**
66
* Configure one or more database connections as associative arrays.

tests/MysqlTest.php

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,89 @@
11
<?php
22

3-
require_once(__DIR__ . '/tests.php');
3+
require_once(__DIR__ . '/Tests.php');
44

5-
class MysqlTest extends PHP_CRUD_API_Test
5+
class MysqlTest extends Tests
66
{
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)
836
{
9-
self::setConfig('MySQL');
10-
self::seedDatabase();
37+
return mysqli_close($db);
1138
}
1239

1340
/**
14-
* Seeds the database for this connection
41+
* Checks the version of the Database
1542
*
1643
* @return void
1744
*/
18-
public function seedDatabase()
45+
public function checkVersion($db)
1946
{
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");
2253
}
54+
}
2355

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;
3567
}
68+
return $capabilities;
69+
}
3670

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';
3879

3980
$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));
4583
}
4684

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+
}
4888
}
4989
}

tests/PostgresqlTest.php

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,77 @@
11
<?php
22

3-
require_once(__DIR__ . '/tests.php');
3+
require_once(__DIR__ . '/Tests.php');
44

5-
class PostgresqlTest extends PHP_CRUD_API_Test
5+
class PostgresqlTest extends Tests
66
{
7-
public static function setUpBeforeClass()
7+
/**
8+
* Connects to the Database
9+
*
10+
* @return object Database connection
11+
*/
12+
public function connect($config)
813
{
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);
1122
}
1223

1324
/**
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
1536
*
1637
* @return void
1738
*/
18-
public function seedDatabase()
39+
public function checkVersion($db)
1940
{
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");
2247
}
48+
}
2349

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+
}
3763
}
64+
return $capabilities;
65+
}
3866

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';
3975
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
4076
array_pop($queries);
4177

@@ -45,7 +81,5 @@ public function seedDatabase()
4581
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
4682
}
4783
}
48-
49-
pg_close($db);
5084
}
5185
}

tests/SqlServerTest.php

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,77 @@
11
<?php
22

3-
require_once(__DIR__ . '/tests.php');
3+
require_once(__DIR__ . '/Tests.php');
44

5-
class SqlServerTest extends PHP_CRUD_API_Test
5+
class SqlServerTest extends Tests
66
{
7-
public static function setUpBeforeClass()
7+
/**
8+
* Connects to the Database
9+
*
10+
* @return object Database connection
11+
*/
12+
public function connect($config)
813
{
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);
1022

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));
1325
}
1426

15-
self::seedDatabase();
27+
return $db;
1628
}
1729

1830
/**
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
2042
*
2143
* @return void
2244
*/
23-
public function seedDatabase()
45+
public function checkVersion($db)
2446
{
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");
2753
}
54+
}
2855

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+
}
3765

38-
$conn = sqlsrv_connect(static::$config['hostname'], $connectionInfo);
3966

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';
4375

4476
$queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
4577
array_pop($queries);
@@ -50,7 +82,5 @@ public function seedDatabase()
5082
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
5183
}
5284
}
53-
54-
sqlsrv_close($conn);
5585
}
5686
}

0 commit comments

Comments
 (0)