Skip to content

Commit fb75a38

Browse files
committed
Another shot at #194
1 parent c8c8a64 commit fb75a38

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

tests/MysqlTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ public function getCapabilities($db)
6262
{
6363
$capabilities = 0;
6464
$version = mysqli_get_server_version($db);
65-
if ($version>50600) {
65+
if ($version>=50600) {
6666
$capabilities |= self::GIS;
6767
}
68+
if ($version>=50700) {
69+
$capabilities |= self::JSON;
70+
}
6871
return $capabilities;
6972
}
7073

@@ -77,8 +80,18 @@ public function seedDatabase($db, $capabilities)
7780
{
7881
$fixture = __DIR__.'/data/blog_mysql.sql';
7982

83+
$contents = file_get_contents($fixture);
84+
85+
if (!($capabilities & self::GIS)) {
86+
$contents = preg_replace('/(POINT|POLYGON) NOT NULL/i','text NOT NULL',$contents);
87+
$contents = preg_replace('/ST_GeomFromText/i','concat',$contents);
88+
}
89+
if (!($capabilities & self::JSON)) {
90+
$contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents);
91+
}
92+
8093
$i=0;
81-
if (mysqli_multi_query($db, file_get_contents($fixture))) {
94+
if (mysqli_multi_query($db, $contents)) {
8295
do { $i++; mysqli_next_result($db); } while (mysqli_more_results($db));
8396
}
8497

tests/PostgresqlTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public function checkVersion($db)
5555
public function getCapabilities($db)
5656
{
5757
$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+
}
5865
$extensions = pg_fetch_all(pg_query($db, "SELECT * FROM pg_extension;"));
5966
foreach ($extensions as $extension) {
6067
if ($extension['extname'] === 'postgis') {
@@ -72,7 +79,17 @@ public function getCapabilities($db)
7279
public function seedDatabase($db,$capabilities)
7380
{
7481
$fixture = __DIR__.'/data/blog_postgresql.sql';
75-
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
82+
$contents = file_get_contents($fixture);
83+
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);
90+
}
91+
92+
$queries = preg_split('/;\s*\n/', $contents);
7693
array_pop($queries);
7794

7895
foreach ($queries as $i=>$query) {

tests/SqlServerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function checkVersion($db)
6161
*/
6262
public function getCapabilities($db)
6363
{
64-
return self::GIS;
64+
$capabilities = 0;
65+
$capabilities |= self::GIS;
66+
return $capabilities;
6567
}
6668

6769

@@ -73,8 +75,9 @@ public function getCapabilities($db)
7375
public function seedDatabase($db, $capabilities)
7476
{
7577
$fixture = __DIR__.'/data/blog_sqlserver.sql';
78+
$contents = file_get_contents($fixture);
7679

77-
$queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
80+
$queries = preg_split('/\n\s*GO\s*\n/', $contents);
7881
array_pop($queries);
7982

8083
foreach ($queries as $i=>$query) {

tests/SqliteTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function checkVersion($db)
5454
public function getCapabilities($db)
5555
{
5656
$capabilities = 0;
57-
57+
$capabilities |= self::JSON;
5858
return $capabilities;
5959
}
6060

@@ -66,8 +66,9 @@ public function getCapabilities($db)
6666
public function seedDatabase($db, $capabilities)
6767
{
6868
$fixture = __DIR__.'/data/blog_sqlite.sql';
69+
$contents = file_get_contents($fixture);
6970

70-
$queries = preg_split('/;\s*\n/', file_get_contents($fixture));
71+
$queries = preg_split('/;\s*\n/', $contents);
7172
array_pop($queries);
7273

7374
foreach ($queries as $i=>$query) {

tests/Tests.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ public function testColumnsOnImplicitJoin()
568568

569569
public function testSpatialFilterWithin()
570570
{
571-
if (static::$config['dbengine']!='SQLite') {
571+
if (static::$capabilities & self::GIS) {
572572
$test = new API($this, static::$config);
573573
$test->get('/users?columns=id,username&filter=location,swi,POINT(30 20)');
574574
$test->expect('{"users":{"columns":["id","username"],"records":[[1,"user1"]]}}');
@@ -683,32 +683,52 @@ public function testListProductsProperties()
683683
{
684684
$test = new API($this, static::$config);
685685
$test->get('/products?columns=id,properties&transform=1');
686-
$test->expect('{"products":[{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}]}');
686+
if (static::$capabilities & self::JSON) {
687+
$test->expect('{"products":[{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}]}');
688+
} else {
689+
$test->expect('{"products":[{"id":1,"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":null}"}]}');
690+
}
687691
}
688692

689693
public function testReadProductProperties()
690694
{
691695
$test = new API($this, static::$config);
692696
$test->get('/products/1?columns=id,properties');
693-
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}');
697+
if (static::$capabilities & self::JSON) {
698+
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":null}}');
699+
} else {
700+
$test->expect('{"id":1,"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":null}"}');
701+
}
694702
}
695703

696704
public function testWriteProductProperties()
697705
{
698706
$test = new API($this, static::$config);
699-
$test->put('/products/1','{"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
707+
if (static::$capabilities & self::JSON) {
708+
$test->put('/products/1','{"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
709+
} else {
710+
$test->put('/products/1','{"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":123}"}');
711+
}
700712
$test->expect('1');
701713
$test->get('/products/1?columns=id,properties');
702-
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
714+
if (static::$capabilities & self::JSON) {
715+
$test->expect('{"id":1,"properties":{"depth":false,"model":"TRX-120","width":100,"height":123}}');
716+
} else {
717+
$test->expect('{"id":1,"properties":"{\"depth\":false,\"model\":\"TRX-120\",\"width\":100,\"height\":123}"}');
718+
}
703719
}
704720

705721
public function testAddProducts()
706722
{
707723
$test = new API($this, static::$config);
708-
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":{}}');
724+
if (static::$capabilities & self::JSON) {
725+
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":{}}');
726+
} else {
727+
$test->post('/products','{"name":"Laptop","price":"1299.99","properties":"{}"}');
728+
}
709729
$test->expect('2');
710-
$test->get('/products/2');
711-
$test->expect('{"id":2,"name":"Laptop","price":"1299.99","properties":{},"created_at":"2013-12-11 10:09:08","deleted_at":null}');
730+
$test->get('/products/2?columns=id,created_at,deleted_at');
731+
$test->expect('{"id":2,"created_at":"2013-12-11 10:09:08","deleted_at":null}');
712732
}
713733

714734
public function testSoftDeleteProducts()

0 commit comments

Comments
 (0)