Skip to content

Commit 0529347

Browse files
authored
Merge branch 'master' into master
2 parents b75c5a0 + b58f4ed commit 0529347

File tree

9 files changed

+352
-139
lines changed

9 files changed

+352
-139
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: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,102 @@
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-
const MYSQL_56 = 50600;
8-
const MYSQL_57 = 50700;
9-
public static $mysql_version;
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');
1026

11-
public static function setUpBeforeClass()
27+
return $db;
28+
}
29+
30+
/**
31+
* Disconnects from the Database
32+
*
33+
* @return boolean Success
34+
*/
35+
public function disconnect($db)
1236
{
13-
self::setConfig('MySQL');
14-
self::seedDatabase();
37+
return mysqli_close($db);
1538
}
1639

1740
/**
18-
* Seeds the database for this connection
41+
* Checks the version of the Database
1942
*
2043
* @return void
2144
*/
22-
public function seedDatabase()
23-
{
24-
if (static::$config['database']=='{{test_database}}') {
25-
die("Configure database in 'config.php' before running tests.\n");
45+
public function checkVersion($db)
46+
{
47+
$major = 5;
48+
$minor = 5;
49+
$version = mysqli_get_server_version($db);
50+
$v = array(floor($version/10000),floor(($version%10000)/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");
2653
}
54+
}
2755

28-
$link = mysqli_connect(
29-
static::$config['hostname'],
30-
static::$config['username'],
31-
static::$config['password'],
32-
static::$config['database']
33-
);
34-
35-
if (mysqli_connect_errno()) {
36-
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;
67+
}
68+
if ($version>=50700) {
69+
$capabilities |= self::JSON;
3770
}
71+
return $capabilities;
72+
}
3873

39-
// Note: For some reason this version is formatted:
40-
// $mysql_version = main_version * 10000 + minor_version * 100 + sub_version
41-
static::$mysql_version = mysqli_get_server_version($link);
42-
$seed_file = self::getSeedFile();
74+
/**
75+
* Seeds the database for this connection
76+
*
77+
* @return void
78+
*/
79+
public function seedDatabase($db, $capabilities)
80+
{
81+
$fixture = __DIR__.'/data/blog_mysql.sql';
82+
$contents = file_get_contents($fixture);
4383

44-
mysqli_set_charset($link,'utf8');
84+
if (!($capabilities & self::GIS)) {
85+
$contents = preg_replace('/(POINT|POLYGON) NOT NULL/i','text NOT NULL',$contents);
86+
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents);
87+
}
88+
if (!($capabilities & self::JSON)) {
89+
$contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents);
90+
}
4591

4692
$i=0;
47-
if (mysqli_multi_query($link, file_get_contents($seed_file))) {
48-
do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
49-
}
50-
if (mysqli_errno($link)) {
51-
die("Loading '$seed_file' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
93+
if (mysqli_multi_query($db, $contents)) {
94+
do { $i++; mysqli_next_result($db); } while (mysqli_more_results($db));
5295
}
5396

54-
mysqli_close($link);
97+
if (mysqli_errno($db)) {
98+
die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($db)."\n");
99+
}
55100
}
56101

57102
/**

tests/PostgresqlTest.php

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,95 @@
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 $gis_installed;
8-
public static $pg_server_version;
7+
/**
8+
* Connects to the Database
9+
*
10+
* @return object Database connection
11+
*/
12+
public function connect($config)
13+
{
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);
22+
}
923

10-
public static function setUpBeforeClass()
24+
/**
25+
* Disconnects from the Database
26+
*
27+
* @return boolean Success
28+
*/
29+
public function disconnect($db)
1130
{
12-
static::setConfig('PostgreSQL');
13-
self::seedDatabase();
31+
return pg_close($db);
1432
}
1533

1634
/**
17-
* Seeds the database for this connection
35+
* Checks the version of the Database
1836
*
1937
* @return void
2038
*/
21-
public function seedDatabase()
39+
public function checkVersion($db)
2240
{
23-
if (static::$config['database']=='{{test_database}}') {
24-
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");
2547
}
48+
}
2649

27-
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
28-
$hostname = $e(static::$config['hostname']);
29-
$database = $e(static::$config['database']);
30-
$username = $e(static::$config['username']);
31-
$password = $e(static::$config['password']);
32-
$conn_string = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
50+
/**
51+
* Gets the capabilities of the Database
52+
*
53+
* @return int Capabilites
54+
*/
55+
public function getCapabilities($db)
56+
{
57+
$capabilities = 0;
58+
$major = 9;
59+
$minor = 4;
60+
$version = pg_version();
61+
$v = explode('.',$version['server']);
62+
if ($v[0]>$major || ($v[0]==$major && $v[1]>=$minor)) {
63+
$capabilities |= self::JSON;
64+
}
65+
$extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
66+
foreach ($extensions as $extension) {
67+
if ($extension['extname'] === 'postgis') {
68+
$capabilities |= self::GIS;
69+
}
70+
}
71+
return $capabilities;
72+
}
3373

34-
$db = pg_connect($conn_string);
74+
/**
75+
* Seeds the database for this connection
76+
*
77+
* @return void
78+
*/
79+
public function seedDatabase($db,$capabilities)
80+
{
81+
$fixture = __DIR__.'/data/blog_postgresql.sql';
82+
$contents = file_get_contents($fixture);
3583

36-
if (!$db) {
37-
die("Connect failed: ". pg_last_error());
84+
if (!($capabilities & self::GIS)) {
85+
$contents = preg_replace('/(geometry) NOT NULL/i','text NOT NULL',$contents);
86+
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents);
87+
}
88+
if (!($capabilities & self::JSON)) {
89+
$contents = preg_replace('/JSONB? NOT NULL/i','text NOT NULL',$contents);
3890
}
3991

40-
static::$pg_server_version = pg_version()['server'];
41-
$gisInstalled = self::isGisInstalled(
42-
pg_fetch_all(
43-
pg_query($db, "SELECT * FROM pg_extension;")
44-
)
45-
);
46-
$seed_file = self::getSeedFile();
47-
$queries = preg_split('/;\s*\n/', file_get_contents($seed_file));
92+
$queries = preg_split('/;\s*\n/', $contents);
4893
array_pop($queries);
4994

5095
foreach ($queries as $i=>$query) {
@@ -53,8 +98,6 @@ public function seedDatabase()
5398
die("Loading '$seed_file' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
5499
}
55100
}
56-
57-
pg_close($db);
58101
}
59102

60103
/**

0 commit comments

Comments
 (0)