Skip to content

Commit 4a517b9

Browse files
authored
Merge pull request #244 from karllhughes/master
Making mysql and postgres test suites adaptable
2 parents b58f4ed + 0529347 commit 4a517b9

11 files changed

+2496
-2
lines changed

tests/MysqlTest.php

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public function getCapabilities($db)
7979
public function seedDatabase($db, $capabilities)
8080
{
8181
$fixture = __DIR__.'/data/blog_mysql.sql';
82-
8382
$contents = file_get_contents($fixture);
8483

8584
if (!($capabilities & self::GIS)) {
@@ -99,4 +98,121 @@ public function seedDatabase($db, $capabilities)
9998
die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($db)."\n");
10099
}
101100
}
101+
102+
/**
103+
* Gets the path to the seed file based on the version of MySQL
104+
*
105+
* @return string
106+
*/
107+
protected static function getSeedFile()
108+
{
109+
if (static::$mysql_version >= self::MYSQL_57) {
110+
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_57.sql';
111+
} elseif (static::$mysql_version >= self::MYSQL_56) {
112+
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_56.sql';
113+
}
114+
return __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']).'_55.sql';
115+
}
116+
117+
public function testHidingPasswordColumn()
118+
{
119+
parent::testHidingPasswordColumn();
120+
}
121+
122+
public function testMissingIntermediateTable()
123+
{
124+
$test = new API($this, static::$config);
125+
$test->get('/users?include=posts,tags');
126+
$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"]]}}');
127+
}
128+
129+
public function testEditUserPassword()
130+
{
131+
parent::testEditUserPassword();
132+
}
133+
134+
public function testEditUserLocation()
135+
{
136+
parent::testEditUserLocation();
137+
}
138+
139+
public function testListUserLocations()
140+
{
141+
parent::testListUserLocations();
142+
}
143+
144+
public function testEditUserWithId()
145+
{
146+
parent::testEditUserWithId();
147+
}
148+
149+
public function testReadOtherUser()
150+
{
151+
parent::testReadOtherUser();
152+
}
153+
154+
public function testEditOtherUser()
155+
{
156+
parent::testEditOtherUser();
157+
}
158+
159+
public function testSpatialFilterWithin()
160+
{
161+
if (static::$mysql_version < self::MYSQL_56) {
162+
$this->markTestSkipped("MySQL < 5.6 does not support JSON fields.");
163+
}
164+
parent::testSpatialFilterWithin();
165+
}
166+
167+
168+
public function testListProductsProperties()
169+
{
170+
if (static::$mysql_version < self::MYSQL_57) {
171+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
172+
}
173+
parent::testListProductsProperties();
174+
}
175+
176+
public function testReadProductProperties()
177+
{
178+
if (static::$mysql_version < self::MYSQL_57) {
179+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
180+
}
181+
parent::testReadProductProperties();
182+
}
183+
184+
public function testWriteProductProperties()
185+
{
186+
if (static::$mysql_version < self::MYSQL_57) {
187+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
188+
}
189+
parent::testWriteProductProperties();
190+
}
191+
192+
public function testListProducts()
193+
{
194+
parent::testListProducts();
195+
}
196+
197+
public function testAddProducts()
198+
{
199+
if (static::$mysql_version < self::MYSQL_57) {
200+
$this->markTestSkipped("MySQL < 5.7 does not support JSON fields.");
201+
}
202+
parent::testAddProducts();
203+
}
204+
205+
public function testSoftDeleteProducts()
206+
{
207+
if (static::$mysql_version < self::MYSQL_57) {
208+
$test = new API($this, static::$config);
209+
$test->delete('/products/1');
210+
$test->expect('1');
211+
$test->get('/products?columns=id,deleted_at');
212+
$test->expect('{"products":{"columns":["id","deleted_at"],"records":[[1,"2013-12-11 11:10:09"]]}}');
213+
} else {
214+
parent::testSoftDeleteProducts();
215+
}
216+
}
217+
102218
}

tests/PostgresqlTest.php

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,108 @@ public function seedDatabase($db,$capabilities)
9595
foreach ($queries as $i=>$query) {
9696
if (!pg_query($db, $query.';')) {
9797
$i++;
98-
die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
98+
die("Loading '$seed_file' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
9999
}
100100
}
101101
}
102+
103+
/**
104+
* Gets the path to the seed file based on the version of Postgres and GIS extension
105+
*
106+
* @return string
107+
*/
108+
protected function getSeedFile()
109+
{
110+
$filepath = __DIR__.'/data/blog_'.strtolower(static::$config['dbengine']);
111+
112+
if (version_compare(static::$pg_server_version, '9.4.0') >= 0 ) {
113+
$filepath .= '_94';
114+
} elseif (version_compare(static::$pg_server_version, '9.2.0') >= 0 ) {
115+
$filepath .= '_92';
116+
} else {
117+
$filepath .= '_91';
118+
}
119+
if (static::$gis_installed) {
120+
$filepath .= '_gis';
121+
}
122+
return $filepath.'.sql';
123+
}
124+
125+
/**
126+
* Determines whether the GIS extension is installed or not based on array of extensions.
127+
*
128+
* @return boolean
129+
*/
130+
protected function isGisInstalled($extensions = [])
131+
{
132+
static::$gis_installed = false;
133+
if ($extensions) {
134+
foreach ($extensions as $extension) {
135+
if ($extension['extname'] === 'postgis') {
136+
static::$gis_installed = true;
137+
break;
138+
}
139+
}
140+
}
141+
return static::$gis_installed;
142+
}
143+
144+
public function testSpatialFilterWithin()
145+
{
146+
if (!static::$gis_installed) {
147+
$this->markTestSkipped("Postgis not installed");
148+
}
149+
parent::testSpatialFilterWithin();
150+
}
151+
152+
public function testListProductsProperties()
153+
{
154+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
155+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
156+
}
157+
parent::testListProductsProperties();
158+
}
159+
160+
public function testReadProductProperties()
161+
{
162+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
163+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
164+
}
165+
parent::testReadProductProperties();
166+
}
167+
168+
public function testWriteProductProperties()
169+
{
170+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
171+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
172+
}
173+
parent::testWriteProductProperties();
174+
}
175+
176+
public function testListProducts()
177+
{
178+
parent::testListProducts();
179+
}
180+
181+
public function testAddProducts()
182+
{
183+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
184+
$this->markTestSkipped("Postgres < 9.2.0 does not support JSON fields.");
185+
}
186+
parent::testAddProducts();
187+
}
188+
189+
public function testSoftDeleteProducts()
190+
{
191+
if (version_compare(static::$pg_server_version, '9.2.0') < 0) {
192+
$test = new API($this, static::$config);
193+
$test->delete('/products/1');
194+
$test->expect('1');
195+
$test->get('/products?columns=id,deleted_at');
196+
$test->expect('{"products":{"columns":["id","deleted_at"],"records":[[1,"2013-12-11 11:10:09"]]}}');
197+
} else {
198+
parent::testSoftDeleteProducts();
199+
}
200+
}
201+
102202
}

tests/data/blog_mysql_55.sql

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
-- Adminer 4.2.4 MySQL dump
2+
3+
SET NAMES utf8;
4+
SET time_zone = '+00:00';
5+
SET foreign_key_checks = 0;
6+
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
7+
8+
DROP TABLE IF EXISTS `categories`;
9+
CREATE TABLE `categories` (
10+
`id` int(11) NOT NULL AUTO_INCREMENT,
11+
`name` varchar(255) NOT NULL,
12+
`icon` blob NULL,
13+
PRIMARY KEY (`id`)
14+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
15+
16+
INSERT INTO `categories` (`id`, `name`, `icon`) VALUES
17+
(1, 'announcement', NULL),
18+
(2, 'article', NULL);
19+
20+
DROP TABLE IF EXISTS `comments`;
21+
CREATE TABLE `comments` (
22+
`id` int(11) NOT NULL AUTO_INCREMENT,
23+
`post_id` int(11) NOT NULL,
24+
`message` varchar(255) NOT NULL,
25+
PRIMARY KEY (`id`),
26+
KEY `post_id` (`post_id`),
27+
CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
28+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
29+
30+
INSERT INTO `comments` (`id`, `post_id`, `message`) VALUES
31+
(1, 1, 'great'),
32+
(2, 1, 'fantastic'),
33+
(3, 2, 'thank you'),
34+
(4, 2, 'awesome');
35+
36+
DROP TABLE IF EXISTS `posts`;
37+
CREATE TABLE `posts` (
38+
`id` int(11) NOT NULL AUTO_INCREMENT,
39+
`user_id` int(11) NOT NULL,
40+
`category_id` int(11) NOT NULL,
41+
`content` varchar(255) NOT NULL,
42+
PRIMARY KEY (`id`),
43+
KEY `category_id` (`category_id`),
44+
KEY `user_id` (`user_id`),
45+
CONSTRAINT `posts_ibfk_3` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`),
46+
CONSTRAINT `posts_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
47+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
48+
49+
INSERT INTO `posts` (`id`, `user_id`, `category_id`, `content`) VALUES
50+
(1, 1, 1, 'blog started'),
51+
(2, 1, 2, 'It works!');
52+
53+
DROP TABLE IF EXISTS `post_tags`;
54+
CREATE TABLE `post_tags` (
55+
`id` int(11) NOT NULL AUTO_INCREMENT,
56+
`post_id` int(11) NOT NULL,
57+
`tag_id` int(11) NOT NULL,
58+
PRIMARY KEY (`id`),
59+
KEY `post_id` (`post_id`),
60+
KEY `tag_id` (`tag_id`),
61+
CONSTRAINT `post_tags_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
62+
CONSTRAINT `post_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
63+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
64+
65+
INSERT INTO `post_tags` (`id`, `post_id`, `tag_id`) VALUES
66+
(1, 1, 1),
67+
(2, 1, 2),
68+
(3, 2, 1),
69+
(4, 2, 2);
70+
71+
DROP TABLE IF EXISTS `tags`;
72+
CREATE TABLE `tags` (
73+
`id` int(11) NOT NULL AUTO_INCREMENT,
74+
`name` varchar(255) NOT NULL,
75+
PRIMARY KEY (`id`)
76+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
77+
78+
INSERT INTO `tags` (`id`, `name`) VALUES
79+
(1, 'funny'),
80+
(2, 'important');
81+
82+
DROP TABLE IF EXISTS `users`;
83+
CREATE TABLE `users` (
84+
`id` int(11) NOT NULL AUTO_INCREMENT,
85+
`username` varchar(255) NOT NULL,
86+
`password` varchar(255) NOT NULL,
87+
`location` text NULL,
88+
PRIMARY KEY (`id`)
89+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
90+
91+
INSERT INTO `users` (`id`, `username`, `password`, `location`) VALUES
92+
(1, 'user1', 'pass1', null),
93+
(2, 'user2', 'pass2', null);
94+
95+
DROP TABLE IF EXISTS `countries`;
96+
CREATE TABLE `countries` (
97+
`id` int(11) NOT NULL AUTO_INCREMENT,
98+
`name` varchar(255) NOT NULL,
99+
PRIMARY KEY (`id`)
100+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
101+
102+
INSERT INTO `countries` (`id`, `name`) VALUES
103+
(1, 'Left'),
104+
(2, 'Right');
105+
106+
DROP TABLE IF EXISTS `events`;
107+
CREATE TABLE `events` (
108+
`id` int(11) NOT NULL AUTO_INCREMENT,
109+
`name` varchar(255) NOT NULL,
110+
`datetime` datetime NOT NULL,
111+
`visitors` int(11) NOT NULL,
112+
PRIMARY KEY (`id`)
113+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
114+
115+
INSERT INTO `events` (`id`, `name`, `datetime`, `visitors`) VALUES
116+
(1, 'Launch', '2016-01-01 13:01:01', 0);
117+
118+
DROP VIEW IF EXISTS `tag_usage`;
119+
CREATE VIEW `tag_usage` AS select `name`, count(`name`) AS `count` from `tags`, `post_tags` where `tags`.`id` = `post_tags`.`tag_id` group by `name` order by `count` desc, `name`;
120+
121+
DROP TABLE IF EXISTS `products`;
122+
CREATE TABLE `products` (
123+
`id` int(11) NOT NULL AUTO_INCREMENT,
124+
`name` varchar(255) NOT NULL,
125+
`price` decimal(10,2) NOT NULL,
126+
`properties` text NOT NULL,
127+
`created_at` datetime NOT NULL,
128+
`deleted_at` datetime NULL,
129+
PRIMARY KEY (`id`)
130+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
131+
132+
INSERT INTO `products` (`id`, `name`, `price`, `properties`, `created_at`) VALUES
133+
(1, 'Calculator', '23.01', '{"depth":false,"model":"TRX-120","width":100,"height":null}', '1970-01-01 01:01:01');
134+
135+
DROP TABLE IF EXISTS `barcodes`;
136+
CREATE TABLE `barcodes` (
137+
`id` int(11) NOT NULL AUTO_INCREMENT,
138+
`product_id` int(11) NOT NULL,
139+
`hex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
140+
`bin` varbinary(255) NOT NULL,
141+
PRIMARY KEY (`id`),
142+
CONSTRAINT `barcodes_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
143+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
144+
145+
INSERT INTO `barcodes` (`id`, `product_id`, `hex`, `bin`) VALUES
146+
(1, 1, '00ff01', UNHEX('00ff01'));
147+
148+
-- 2016-11-05 13:11:47

0 commit comments

Comments
 (0)