Skip to content

Commit b75c5a0

Browse files
committed
Making mysql and postgres test suites adaptable
1 parent 94922d7 commit b75c5a0

11 files changed

+2518
-8
lines changed

tests/MysqlTest.php

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
class MysqlTest extends PHP_CRUD_API_Test
66
{
7+
const MYSQL_56 = 50600;
8+
const MYSQL_57 = 50700;
9+
public static $mysql_version;
10+
711
public static function setUpBeforeClass()
812
{
913
self::setConfig('MySQL');
@@ -21,8 +25,6 @@ public function seedDatabase()
2125
die("Configure database in 'config.php' before running tests.\n");
2226
}
2327

24-
$fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
25-
2628
$link = mysqli_connect(
2729
static::$config['hostname'],
2830
static::$config['username'],
@@ -34,16 +36,138 @@ public function seedDatabase()
3436
die("Connect failed: ".mysqli_connect_error()."\n");
3537
}
3638

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();
43+
3744
mysqli_set_charset($link,'utf8');
3845

3946
$i=0;
40-
if (mysqli_multi_query($link, file_get_contents($fixture))) {
47+
if (mysqli_multi_query($link, file_get_contents($seed_file))) {
4148
do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
4249
}
4350
if (mysqli_errno($link)) {
44-
die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
51+
die("Loading '$seed_file' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
4552
}
4653

4754
mysqli_close($link);
4855
}
56+
57+
/**
58+
* Gets the path to the seed file based on the version of MySQL
59+
*
60+
* @return string
61+
*/
62+
protected static function getSeedFile()
63+
{
64+
if (static::$mysql_version >= self::MYSQL_57) {
65+
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_57.sql';
66+
} elseif (static::$mysql_version >= self::MYSQL_56) {
67+
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_56.sql';
68+
}
69+
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_55.sql';
70+
}
71+
72+
public function testHidingPasswordColumn()
73+
{
74+
parent::testHidingPasswordColumn();
75+
}
76+
77+
public function testMissingIntermediateTable()
78+
{
79+
$test = new API($this, static::$config);
80+
$test->get('/users?include=posts,tags');
81+
$test->expect('{"users":{"columns":["id","username","location"],"records":[[1,"user1",null]]},"posts":{"relations":{"user_id":"users.id"},"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"],[2,1,2,"It works!"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[[1,1,1],[2,1,2],[3,2,1],[4,2,2]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[[1,"funny"],[2,"important"]]}}');
82+
}
83+
84+
public function testEditUserPassword()
85+
{
86+
parent::testEditUserPassword();
87+
}
88+
89+
public function testEditUserLocation()
90+
{
91+
parent::testEditUserLocation();
92+
}
93+
94+
public function testListUserLocations()
95+
{
96+
parent::testListUserLocations();
97+
}
98+
99+
public function testEditUserWithId()
100+
{
101+
parent::testEditUserWithId();
102+
}
103+
104+
public function testReadOtherUser()
105+
{
106+
parent::testReadOtherUser();
107+
}
108+
109+
public function testEditOtherUser()
110+
{
111+
parent::testEditOtherUser();
112+
}
113+
114+
public function testSpatialFilterWithin()
115+
{
116+
if (static::$mysql_version < self::MYSQL_56) {
117+
$this->markTestSkipped("MySQL < 5.6 does not support JSON fields.");
118+
}
119+
parent::testSpatialFilterWithin();
120+
}
121+
122+
123+
public function testListProductsProperties()
124+
{
125+
if (static::$mysql_version < self::MYSQL_57) {
126+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
127+
}
128+
parent::testListProductsProperties();
129+
}
130+
131+
public function testReadProductProperties()
132+
{
133+
if (static::$mysql_version < self::MYSQL_57) {
134+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
135+
}
136+
parent::testReadProductProperties();
137+
}
138+
139+
public function testWriteProductProperties()
140+
{
141+
if (static::$mysql_version < self::MYSQL_57) {
142+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
143+
}
144+
parent::testWriteProductProperties();
145+
}
146+
147+
public function testListProducts()
148+
{
149+
parent::testListProducts();
150+
}
151+
152+
public function testAddProducts()
153+
{
154+
if (static::$mysql_version < self::MYSQL_57) {
155+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
156+
}
157+
parent::testAddProducts();
158+
}
159+
160+
public function testSoftDeleteProducts()
161+
{
162+
if (static::$mysql_version < self::MYSQL_57) {
163+
$test = new API($this, static::$config);
164+
$test->delete('/products/1');
165+
$test->expect('1');
166+
$test->get('/products?columns=id,deleted_at');
167+
$test->expect('{"products":{"columns":["id","deleted_at"],"records":[[1,"2013-12-11 11:10:09"]]}}');
168+
} else {
169+
parent::testSoftDeleteProducts();
170+
}
171+
}
172+
49173
}

tests/PostgresqlTest.php

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class PostgresqlTest extends PHP_CRUD_API_Test
66
{
7+
public static $gis_installed;
8+
public static $pg_server_version;
9+
710
public static function setUpBeforeClass()
811
{
912
static::setConfig('PostgreSQL');
@@ -21,8 +24,6 @@ public function seedDatabase()
2124
die("Configure database in 'config.php' before running tests.\n");
2225
}
2326

24-
$fixture = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'.sql';
25-
2627
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
2728
$hostname = $e(static::$config['hostname']);
2829
$database = $e(static::$config['database']);
@@ -36,16 +37,123 @@ public function seedDatabase()
3637
die("Connect failed: ". pg_last_error());
3738
}
3839

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

4250
foreach ($queries as $i=>$query) {
4351
if (!pg_query($db, $query.';')) {
4452
$i++;
45-
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
53+
die("Loading '$seed_file' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
4654
}
4755
}
4856

4957
pg_close($db);
5058
}
59+
60+
/**
61+
* Gets the path to the seed file based on the version of Postgres and GIS extension
62+
*
63+
* @return string
64+
*/
65+
protected function getSeedFile()
66+
{
67+
$filepath = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']);
68+
69+
if (version_compare(static::$pg_server_version, '9.4.0') >= 0 ) {
70+
$filepath .= '_94';
71+
} elseif (version_compare(static::$pg_server_version, '9.2.0') >= 0 ) {
72+
$filepath .= '_92';
73+
} else {
74+
$filepath .= '_91';
75+
}
76+
if (static::$gis_installed) {
77+
$filepath .= '_gis';
78+
}
79+
return $filepath.'.sql';
80+
}
81+
82+
/**
83+
* Determines whether the GIS extension is installed or not based on array of extensions.
84+
*
85+
* @return boolean
86+
*/
87+
protected function isGisInstalled($extensions = [])
88+
{
89+
static::$gis_installed = false;
90+
if ($extensions) {
91+
foreach ($extensions as $extension) {
92+
if ($extension['extname'] === 'postgis') {
93+
static::$gis_installed = true;
94+
break;
95+
}
96+
}
97+
}
98+
return static::$gis_installed;
99+
}
100+
101+
public function testSpatialFilterWithin()
102+
{
103+
if (!static::$gis_installed) {
104+
$this->markTestSkipped("Postgis not installed");
105+
}
106+
parent::testSpatialFilterWithin();
107+
}
108+
109+
public function testListProductsProperties()
110+
{
111+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
112+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
113+
}
114+
parent::testListProductsProperties();
115+
}
116+
117+
public function testReadProductProperties()
118+
{
119+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
120+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
121+
}
122+
parent::testReadProductProperties();
123+
}
124+
125+
public function testWriteProductProperties()
126+
{
127+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
128+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
129+
}
130+
parent::testWriteProductProperties();
131+
}
132+
133+
public function testListProducts()
134+
{
135+
parent::testListProducts();
136+
}
137+
138+
public function testAddProducts()
139+
{
140+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
141+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
142+
}
143+
parent::testAddProducts();
144+
}
145+
146+
public function testSoftDeleteProducts()
147+
{
148+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
149+
$test = new API($this, static::$config);
150+
$test->delete('/products/1');
151+
$test->expect('1');
152+
$test->get('/products?columns=id,deleted_at');
153+
$test->expect('{"products":{"columns":["id","deleted_at"],"records":[[1,"2013-12-11 11:10:09"]]}}');
154+
} else {
155+
parent::testSoftDeleteProducts();
156+
}
157+
}
158+
51159
}

0 commit comments

Comments
 (0)