Browse Source

Improve float and integer handling

Maurits van der Schee 7 years ago
parent
commit
8616b6795b
7 changed files with 174 additions and 82 deletions
  1. 39
    27
      api.php
  2. BIN
      data/blog.db
  3. 11
    0
      tests/blog_mysql.sql
  4. 27
    0
      tests/blog_postgresql.sql
  5. 9
    0
      tests/blog_sqlite.sql
  6. 26
    0
      tests/blog_sqlserver.sql
  7. 62
    55
      tests/tests.php

+ 39
- 27
api.php View File

@@ -5,8 +5,8 @@ interface DatabaseInterface {
5 5
 	public function getSql($name);
6 6
 	public function connect($hostname,$username,$password,$database,$port,$socket,$charset);
7 7
 	public function query($sql,$params=array());
8
-	public function fetchAssoc($result);
9
-	public function fetchRow($result);
8
+	public function fetchAssoc($result,$fields=false);
9
+	public function fetchRow($result,$fields=false);
10 10
 	public function insertId($result);
11 11
 	public function affectedRows($result);
12 12
 	public function close($result);
@@ -104,6 +104,9 @@ class MySQL implements DatabaseInterface {
104 104
 		if (!mysqli_query($db,'SET SESSION sql_mode = \'ANSI_QUOTES\';')) {
105 105
 			throw new \Exception('Error setting ANSI quotes. '.mysqli_error($db));
106 106
 		}
107
+		if (!mysqli_options($db,MYSQLI_OPT_INT_AND_FLOAT_NATIVE,true)) {
108
+			throw new \Exception('Error setting int and float native. '.mysqli_error($db));
109
+		}
107 110
 		$this->db = $db;
108 111
 	}
109 112
 
@@ -138,11 +141,11 @@ class MySQL implements DatabaseInterface {
138 141
 		return mysqli_query($db,$sql);
139 142
 	}
140 143
 
141
-	public function fetchAssoc($result) {
144
+	public function fetchAssoc($result,$fields=false) {
142 145
 		return mysqli_fetch_assoc($result);
143 146
 	}
144 147
 
145
-	public function fetchRow($result) {
148
+	public function fetchRow($result,$fields=false) {
146 149
 		return mysqli_fetch_row($result);
147 150
 	}
148 151
 
@@ -365,12 +368,29 @@ class PostgreSQL implements DatabaseInterface {
365 368
 		return @pg_query($db,$sql);
366 369
 	}
367 370
 
368
-	public function fetchAssoc($result) {
369
-		return pg_fetch_assoc($result);
371
+	protected function convertFloatAndInt($result,&$values, $fields) {
372
+		array_walk($values, function(&$v,$i) use ($result,$fields){
373
+			$t = $fields[$i]->type;
374
+			if (is_string($v) && in_array($t,array('int2','int4','int8','float4','float8'))) {
375
+				$v+=0;
376
+			}
377
+		});
370 378
 	}
371 379
 
372
-	public function fetchRow($result) {
373
-		return pg_fetch_row($result);
380
+	public function fetchAssoc($result,$fields=false) {
381
+		$values = pg_fetch_assoc($result);
382
+		if ($values && $fields) {
383
+			$this->convertFloatAndInt($result,$values,$fields);
384
+		}
385
+		return $values;
386
+	}
387
+
388
+	public function fetchRow($result,$fields=false) {
389
+		$values = pg_fetch_row($result);
390
+		if ($values && $fields) {
391
+			$this->convertFloatAndInt($result,$values,array_values($fields));
392
+		}
393
+		return $values;
374 394
 	}
375 395
 
376 396
 	public function insertId($result) {
@@ -591,16 +611,12 @@ class SQLServer implements DatabaseInterface {
591 611
 		return sqlsrv_query($db,$sql,$args)?:null;
592 612
 	}
593 613
 
594
-	public function fetchAssoc($result) {
595
-		$values = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
596
-		if ($values) $values = array_map(function($v){ return is_null($v)?null:(string)$v; },$values);
597
-		return $values;
614
+	public function fetchAssoc($result,$fields=false) {
615
+		return sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
598 616
 	}
599 617
 
600
-	public function fetchRow($result) {
601
-		$values = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC);
602
-		if ($values) $values = array_map(function($v){ return is_null($v)?null:(string)$v; },$values);
603
-		return $values;
618
+	public function fetchRow($result,$fields=false) {
619
+		return sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC);
604 620
 	}
605 621
 
606 622
 	public function insertId($result) {
@@ -832,16 +848,12 @@ class SQLite implements DatabaseInterface {
832 848
 		return $result;
833 849
 	}
834 850
 
835
-	public function fetchAssoc($result) {
836
-		$values = $result->fetchArray(SQLITE3_ASSOC);
837
-		if ($values) $values = array_map(function($v){ return is_null($v)?null:(string)$v; },$values);
838
-		return $values;
851
+	public function fetchAssoc($result,$fields=false) {
852
+		return $result->fetchArray(SQLITE3_ASSOC);
839 853
 	}
840 854
 
841
-	public function fetchRow($result) {
842
-		$values = $result->fetchArray(SQLITE3_NUM);
843
-		if ($values) $values = array_map(function($v){ return is_null($v)?null:(string)$v; },$values);
844
-		return $values;
855
+	public function fetchRow($result,$fields=false) {
856
+		return $result->fetchArray(SQLITE3_NUM);
845 857
 	}
846 858
 
847 859
 	public function insertId($result) {
@@ -1240,7 +1252,7 @@ class PHP_CRUD_API {
1240 1252
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
1241 1253
 		$object = null;
1242 1254
 		if ($result = $this->db->query($sql,$params)) {
1243
-			$object = $this->db->fetchAssoc($result);
1255
+			$object = $this->db->fetchAssoc($result,$fields[$table]);
1244 1256
 			$this->db->close($result);
1245 1257
 		}
1246 1258
 		return $object;
@@ -1625,7 +1637,7 @@ class PHP_CRUD_API {
1625 1637
 			$keys = array_flip($keys);
1626 1638
 			echo ',"records":[';
1627 1639
 			$first_row = true;
1628
-			while ($row = $this->db->fetchRow($result)) {
1640
+			while ($row = $this->db->fetchRow($result,$fields[$table])) {
1629 1641
 				if ($first_row) $first_row = false;
1630 1642
 				else echo ',';
1631 1643
 				if (isset($collect[$table])) {
@@ -1675,7 +1687,7 @@ class PHP_CRUD_API {
1675 1687
 				$keys = array_flip($keys);
1676 1688
 				echo ',"records":[';
1677 1689
 				$first_row = true;
1678
-				while ($row = $this->db->fetchRow($result)) {
1690
+				while ($row = $this->db->fetchRow($result,$fields[$table])) {
1679 1691
 					if ($first_row) $first_row = false;
1680 1692
 					else echo ',';
1681 1693
 					if (isset($collect[$table])) {

BIN
data/blog.db View File


+ 11
- 0
tests/blog_mysql.sql View File

@@ -118,4 +118,15 @@ INSERT INTO `events` (`id`, `name`, `datetime`) VALUES
118 118
 DROP VIEW IF EXISTS `tag_usage`;
119 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 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
+  PRIMARY KEY (`id`)
127
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
128
+
129
+INSERT INTO `products` (`id`, `name`, `price`) VALUES
130
+(1,	'Calculator', '23.01');
131
+
121 132
 -- 2016-11-05 13:11:47

+ 27
- 0
tests/blog_postgresql.sql View File

@@ -28,6 +28,8 @@ DROP TABLE IF EXISTS users CASCADE;
28 28
 DROP TABLE IF EXISTS countries CASCADE;
29 29
 DROP TABLE IF EXISTS events CASCADE;
30 30
 DROP VIEW IF EXISTS tag_usage;
31
+DROP TABLE IF EXISTS products CASCADE;
32
+
31 33
 --
32 34
 -- Name: categories; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
33 35
 --
@@ -120,6 +122,16 @@ CREATE TABLE events (
120 122
 
121 123
 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";
122 124
 
125
+--
126
+-- Name: products; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
127
+--
128
+
129
+CREATE TABLE products (
130
+    id serial NOT NULL,
131
+    name character varying(255) NOT NULL,
132
+    price decimal(10,2) NOT NULL
133
+);
134
+
123 135
 --
124 136
 -- Data for Name: categories; Type: TABLE DATA; Schema: public; Owner: postgres
125 137
 --
@@ -187,6 +199,13 @@ INSERT INTO "countries" ("name", "shape") VALUES
187 199
 INSERT INTO "events" ("name", "datetime") VALUES
188 200
 ('Launch',	'2016-01-01 13:01:01.111');
189 201
 
202
+--
203
+-- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres
204
+--
205
+
206
+INSERT INTO "products" ("name", "price") VALUES
207
+('Calculator',	'23.01');
208
+
190 209
 --
191 210
 -- Name: categories_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres; Tablespace:
192 211
 --
@@ -258,6 +277,14 @@ ALTER TABLE ONLY events
258 277
     ADD CONSTRAINT events_pkey PRIMARY KEY (id);
259 278
 
260 279
 
280
+--
281
+-- Name: products_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres; Tablespace:
282
+--
283
+
284
+ALTER TABLE ONLY products
285
+    ADD CONSTRAINT products_pkey PRIMARY KEY (id);
286
+
287
+
261 288
 --
262 289
 -- Name: comments_post_id_idx; Type: INDEX; Schema: public; Owner: postgres; Tablespace:
263 290
 --

+ 9
- 0
tests/blog_sqlite.sql View File

@@ -100,4 +100,13 @@ INSERT INTO `events` (`id`, `name`, `datetime`) VALUES (1,	'Launch',	'2016-01-01
100 100
 DROP VIEW IF EXISTS `tag_usage`;
101 101
 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`;
102 102
 
103
+DROP TABLE IF EXISTS `products`;
104
+CREATE TABLE `products` (
105
+  `id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
106
+  `name` text(255) NOT NULL,
107
+  `price` text(12) NOT NULL
108
+);
109
+
110
+INSERT INTO `products` (`id`, `name`, `price`) VALUES (1,	'Calculator', '23.01');
111
+
103 112
 --

+ 26
- 0
tests/blog_sqlserver.sql View File

@@ -23,6 +23,11 @@ BEGIN
23 23
 ALTER TABLE [comments] DROP CONSTRAINT [FK_comments_posts]
24 24
 END
25 25
 GO
26
+IF (OBJECT_ID('products', 'U') IS NOT NULL)
27
+BEGIN
28
+DROP TABLE [products]
29
+END
30
+GO
26 31
 IF (OBJECT_ID('events', 'U') IS NOT NULL)
27 32
 BEGIN
28 33
 DROP TABLE [events]
@@ -193,6 +198,21 @@ CREATE VIEW [tag_usage]
193 198
 AS
194 199
 SELECT top 100 PERCENT name, COUNT(name) AS [count] FROM tags, post_tags WHERE tags.id = post_tags.tag_id GROUP BY name ORDER BY [count] DESC, name
195 200
 
201
+GO
202
+SET ANSI_NULLS ON
203
+GO
204
+SET QUOTED_IDENTIFIER ON
205
+GO
206
+CREATE TABLE [products](
207
+	[id] [int] IDENTITY,
208
+	[name] [nvarchar](max) NOT NULL,
209
+	[price] [decimal](10,2) NOT NULL,
210
+ CONSTRAINT [PK_products] PRIMARY KEY CLUSTERED
211
+(
212
+	[id] ASC
213
+)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
214
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
215
+
196 216
 GO
197 217
 SET IDENTITY_INSERT [categories] ON
198 218
 GO
@@ -264,6 +284,12 @@ INSERT [events] ([id], [name], [datetime]) VALUES (1, N'Launch', N'2016-01-01 13
264 284
 GO
265 285
 SET IDENTITY_INSERT [events] OFF
266 286
 GO
287
+SET IDENTITY_INSERT [products] ON
288
+GO
289
+INSERT [products] ([id], [name], [price]) VALUES (1, N'Calculator', N'23.01')
290
+GO
291
+SET IDENTITY_INSERT [products] OFF
292
+GO
267 293
 ALTER TABLE [comments]  WITH CHECK ADD  CONSTRAINT [FK_comments_posts] FOREIGN KEY([post_id])
268 294
 REFERENCES [posts] ([id])
269 295
 GO

+ 62
- 55
tests/tests.php View File

@@ -192,58 +192,58 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
192 192
 	{
193 193
 		$test = new API($this);
194 194
 		$test->get('/posts');
195
-		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["1","1","1","blog started"],["2","1","2","It works!"]]}}');
195
+		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"],[2,1,2,"It works!"]]}}');
196 196
 	}
197 197
 
198 198
 	public function testListPostColumns()
199 199
 	{
200 200
 		$test = new API($this);
201 201
 		$test->get('/posts?columns=id,content');
202
-		$test->expect('{"posts":{"columns":["id","content"],"records":[["1","blog started"],["2","It works!"]]}}');
202
+		$test->expect('{"posts":{"columns":["id","content"],"records":[[1,"blog started"],[2,"It works!"]]}}');
203 203
 	}
204 204
 
205 205
 	public function testListPostsWithTransform()
206 206
 	{
207 207
 		$test = new API($this);
208 208
 		$test->get('/posts?transform=1');
209
-		$test->expect('{"posts":[{"id":"1","user_id":"1","category_id":"1","content":"blog started"},{"id":"2","user_id":"1","category_id":"2","content":"It works!"}]}');
209
+		$test->expect('{"posts":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":2,"user_id":1,"category_id":2,"content":"It works!"}]}');
210 210
 	}
211 211
 
212 212
 	public function testReadPost()
213 213
 	{
214 214
 		$test = new API($this);
215 215
 		$test->get('/posts/2');
216
-		$test->expect('{"id":"2","user_id":"1","category_id":"2","content":"It works!"}');
216
+		$test->expect('{"id":2,"user_id":1,"category_id":2,"content":"It works!"}');
217 217
 	}
218 218
 
219 219
 	public function testReadPosts()
220 220
 	{
221 221
 		$test = new API($this);
222 222
 		$test->get('/posts/1,2');
223
-		$test->expect('[{"id":"1","user_id":"1","category_id":"1","content":"blog started"},{"id":"2","user_id":"1","category_id":"2","content":"It works!"}]');
223
+		$test->expect('[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":2,"user_id":1,"category_id":2,"content":"It works!"}]');
224 224
 	}
225 225
 
226 226
 	public function testReadPostColumns()
227 227
 	{
228 228
 		$test = new API($this);
229 229
 		$test->get('/posts/2?columns=id,content');
230
-		$test->expect('{"id":"2","content":"It works!"}');
230
+		$test->expect('{"id":2,"content":"It works!"}');
231 231
 	}
232 232
 
233 233
 	public function testAddPost()
234 234
 	{
235 235
 		$test = new API($this);
236
-		$test->post('/posts','{"user_id":"1","category_id":"1","content":"test"}');
236
+		$test->post('/posts','{"user_id":1,"category_id":1,"content":"test"}');
237 237
 		$test->expect('3');
238 238
 	}
239 239
 
240 240
 	public function testEditPost()
241 241
 	{
242 242
 		$test = new API($this);
243
-		$test->put('/posts/3','{"user_id":"1","category_id":"1","content":"test (edited)"}');
243
+		$test->put('/posts/3','{"user_id":1,"category_id":1,"content":"test (edited)"}');
244 244
 		$test->expect('1');
245 245
 		$test->get('/posts/3');
246
-		$test->expect('{"id":"3","user_id":"1","category_id":"1","content":"test (edited)"}');
246
+		$test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited)"}');
247 247
 	}
248 248
 
249 249
 	public function testEditPostColumnsMissingField()
@@ -252,16 +252,16 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
252 252
 		$test->put('/posts/3?columns=id,content','{"content":"test (edited 2)"}');
253 253
 		$test->expect('1');
254 254
 		$test->get('/posts/3');
255
-		$test->expect('{"id":"3","user_id":"1","category_id":"1","content":"test (edited 2)"}');
255
+		$test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 2)"}');
256 256
 	}
257 257
 
258 258
     public function testEditPostColumnsExtraField()
259 259
 	{
260 260
 		$test = new API($this);
261
-		$test->put('/posts/3?columns=id,content','{"user_id":"2","content":"test (edited 3)"}');
261
+		$test->put('/posts/3?columns=id,content','{"user_id":2,"content":"test (edited 3)"}');
262 262
 		$test->expect('1');
263 263
 		$test->get('/posts/3');
264
-		$test->expect('{"id":"3","user_id":"1","category_id":"1","content":"test (edited 3)"}');
264
+		$test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 3)"}');
265 265
 	}
266 266
 
267 267
 	public function testEditPostWithUtf8Content()
@@ -271,7 +271,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
271 271
 		$test->put('/posts/2','{"content":'.$utf8.'}');
272 272
 		$test->expect('1');
273 273
 		$test->get('/posts/2');
274
-		$test->expect('{"id":"2","user_id":"1","category_id":"2","content":'.$utf8.'}');
274
+		$test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$utf8.'}');
275 275
 	}
276 276
 
277 277
 	public function testEditPostWithUtf8ContentWithPost()
@@ -283,7 +283,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
283 283
 		$test->put('/posts/2','content='.$url_encoded);
284 284
 		$test->expect('1');
285 285
 		$test->get('/posts/2');
286
-		$test->expect('{"id":"2","user_id":"1","category_id":"2","content":'.$json_encoded.'}');
286
+		$test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$json_encoded.'}');
287 287
 	}
288 288
 
289 289
 	public function testDeletePost()
@@ -308,7 +308,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
308 308
 		$test->put('/posts/4','user_id=1&category_id=1&content=test+(edited)');
309 309
 		$test->expect('1');
310 310
 		$test->get('/posts/4');
311
-		$test->expect('{"id":"4","user_id":"1","category_id":"1","content":"test (edited)"}');
311
+		$test->expect('{"id":4,"user_id":1,"category_id":1,"content":"test (edited)"}');
312 312
 	}
313 313
 
314 314
 	public function testDeletePostWithPost()
@@ -324,32 +324,32 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
324 324
 	{
325 325
 		$test = new API($this);
326 326
 		for ($i=1;$i<=10;$i++) {
327
-		  $test->post('/posts','{"user_id":"1","category_id":"1","content":"#'.$i.'"}');
327
+		  $test->post('/posts','{"user_id":1,"category_id":1,"content":"#'.$i.'"}');
328 328
 		  $test->expect(4+$i);
329 329
 		}
330 330
 		$test->get('/posts?page=2,2&order=id');
331
-		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["5","1","1","#1"],["6","1","1","#2"]],"results":11}}');
331
+		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[5,1,1,"#1"],[6,1,1,"#2"]],"results":11}}');
332 332
 	}
333 333
 
334 334
 	public function testListWithPaginateLastPage()
335 335
 	{
336 336
 		$test = new API($this);
337 337
 		$test->get('/posts?page=3,5&order=id');
338
-		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["14","1","1","#10"]],"results":11}}');
338
+		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[14,1,1,"#10"]],"results":11}}');
339 339
 	}
340 340
 
341 341
 	public function testListExampleFromReadme()
342 342
 	{
343 343
 		$test = new API($this);
344 344
 		$test->get('/posts?include=categories,tags,comments&filter=id,eq,1');
345
-		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[["1","1","1","blog started"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[["1","1","1"],["2","1","2"]]},"categories":{"relations":{"id":"posts.category_id"},"columns":["id","name","icon"],"records":[["1","announcement",null]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[["1","funny"],["2","important"]]},"comments":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","message"],"records":[["1","1","great"],["2","1","fantastic"]]}}');
345
+		$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[[1,1,1],[2,1,2]]},"categories":{"relations":{"id":"posts.category_id"},"columns":["id","name","icon"],"records":[[1,"announcement",null]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[[1,"funny"],[2,"important"]]},"comments":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","message"],"records":[[1,1,"great"],[2,1,"fantastic"]]}}');
346 346
 	}
347 347
 
348 348
 	public function testListExampleFromReadmeWithTransform()
349 349
 	{
350 350
 		$test = new API($this);
351 351
 		$test->get('/posts?include=categories,tags,comments&filter=id,eq,1&transform=1');
352
-		$test->expect('{"posts":[{"id":"1","post_tags":[{"id":"1","post_id":"1","tag_id":"1","tags":[{"id":"1","name":"funny"}]},{"id":"2","post_id":"1","tag_id":"2","tags":[{"id":"2","name":"important"}]}],"comments":[{"id":"1","post_id":"1","message":"great"},{"id":"2","post_id":"1","message":"fantastic"}],"user_id":"1","category_id":"1","categories":[{"id":"1","name":"announcement","icon":null}],"content":"blog started"}]}');
352
+		$test->expect('{"posts":[{"id":1,"post_tags":[{"id":1,"post_id":1,"tag_id":1,"tags":[{"id":1,"name":"funny"}]},{"id":2,"post_id":1,"tag_id":2,"tags":[{"id":2,"name":"important"}]}],"comments":[{"id":1,"post_id":1,"message":"great"},{"id":2,"post_id":1,"message":"fantastic"}],"user_id":1,"category_id":1,"categories":[{"id":1,"name":"announcement","icon":null}],"content":"blog started"}]}');
353 353
 	}
354 354
 
355 355
 	public function testEditCategoryWithBinaryContent()
@@ -360,7 +360,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
360 360
 		$test->put('/categories/2','{"icon":"'.$base64url.'"}');
361 361
 		$test->expect('1');
362 362
 		$test->get('/categories/2');
363
-		$test->expect('{"id":"2","name":"article","icon":"'.$binary.'"}');
363
+		$test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}');
364 364
 	}
365 365
 
366 366
 	public function testEditCategoryWithNull()
@@ -369,7 +369,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
369 369
 		$test->put('/categories/2','{"icon":null}');
370 370
 		$test->expect('1');
371 371
 		$test->get('/categories/2');
372
-		$test->expect('{"id":"2","name":"article","icon":null}');
372
+		$test->expect('{"id":2,"name":"article","icon":null}');
373 373
 	}
374 374
 
375 375
 	public function testEditCategoryWithBinaryContentWithPost()
@@ -380,14 +380,14 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
380 380
 		$test->put('/categories/2','icon='.$base64url);
381 381
 		$test->expect('1');
382 382
 		$test->get('/categories/2');
383
-		$test->expect('{"id":"2","name":"article","icon":"'.$binary.'"}');
383
+		$test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}');
384 384
 	}
385 385
 
386 386
 	public function testListCategoriesWithBinaryContent()
387 387
 	{
388 388
 		$test = new API($this);
389 389
 		$test->get('/categories');
390
-		$test->expect('{"categories":{"columns":["id","name","icon"],"records":[["1","announcement",null],["2","article","4oKsIABhYmMACg1cYgA="]]}}');
390
+		$test->expect('{"categories":{"columns":["id","name","icon"],"records":[[1,"announcement",null],[2,"article","4oKsIABhYmMACg1cYgA="]]}}');
391 391
 	}
392 392
 
393 393
 	public function testEditCategoryWithNullWithPost()
@@ -396,13 +396,13 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
396 396
 		$test->put('/categories/2','icon__is_null');
397 397
 		$test->expect('1');
398 398
 		$test->get('/categories/2');
399
-		$test->expect('{"id":"2","name":"article","icon":null}');
399
+		$test->expect('{"id":2,"name":"article","icon":null}');
400 400
 	}
401 401
 
402 402
 	public function testAddPostFailure()
403 403
 	{
404 404
 		$test = new API($this);
405
-		$test->post('/posts','{"user_id":"a","category_id":"1","content":"tests"}');
405
+		$test->post('/posts','{"user_id":"a","category_id":1,"content":"tests"}');
406 406
 		$test->expect('null');
407 407
 	}
408 408
 
@@ -417,7 +417,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
417 417
 	{
418 418
 		$test = new API($this);
419 419
 		$test->get('/users?filter=id,eq,1&transform=1');
420
-		$test->expect('{"users":[{"id":"1","username":"user1","location":null}]}');
420
+		$test->expect('{"users":[{"id":1,"username":"user1","location":null}]}');
421 421
 	}
422 422
 
423 423
 	public function testValidatorErrorMessage()
@@ -433,7 +433,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
433 433
 		$test->put('/categories/2','{"name":"<script>alert();</script>"}');
434 434
 		$test->expect('1');
435 435
 		$test->get('/categories/2');
436
-		$test->expect('{"id":"2","name":"alert();","icon":null}');
436
+		$test->expect('{"id":2,"name":"alert();","icon":null}');
437 437
 	}
438 438
 
439 439
 	public function testErrorOnInvalidJson()
@@ -446,14 +446,14 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
446 446
 	public function testErrorOnDuplicatePrimaryKey()
447 447
 	{
448 448
 		$test = new API($this);
449
-		$test->post('/posts','{"id":"1","user_id":"1","category_id":"1","content":"blog started (duplicate)"}');
449
+		$test->post('/posts','{"id":1,"user_id":1,"category_id":1,"content":"blog started (duplicate)"}');
450 450
 		$test->expect('null');
451 451
 	}
452 452
 
453 453
 	public function testErrorOnFailingForeignKeyConstraint()
454 454
 	{
455 455
 		$test = new API($this);
456
-		$test->post('/posts','{"user_id":"3","category_id":"1","content":"fk constraint"}');
456
+		$test->post('/posts','{"user_id":3,"category_id":1,"content":"fk constraint"}');
457 457
 		$test->expect('null');
458 458
 	}
459 459
 
@@ -461,7 +461,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
461 461
 	{
462 462
 		$test = new API($this);
463 463
 		$test->get('/users?include=posts,tags');
464
-		$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","\u20ac Hello world, \u039a\u03b1\u03bb\u03b7\u03bc\u1f73\u03c1\u03b1 \u03ba\u1f79\u03c3\u03bc\u03b5, \u30b3\u30f3\u30cb\u30c1\u30cf"],["5","1","1","#1"],["6","1","1","#2"],["7","1","1","#3"],["8","1","1","#4"],["9","1","1","#5"],["10","1","1","#6"],["11","1","1","#7"],["12","1","1","#8"],["14","1","1","#10"]]},"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"]]}}');
464
+		$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,"\u20ac Hello world, \u039a\u03b1\u03bb\u03b7\u03bc\u1f73\u03c1\u03b1 \u03ba\u1f79\u03c3\u03bc\u03b5, \u30b3\u30f3\u30cb\u30c1\u30cf"],[5,1,1,"#1"],[6,1,1,"#2"],[7,1,1,"#3"],[8,1,1,"#4"],[9,1,1,"#5"],[10,1,1,"#6"],[11,1,1,"#7"],[12,1,1,"#8"],[14,1,1,"#10"]]},"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"]]}}');
465 465
 	}
466 466
 
467 467
 	public function testEditUserPassword()
@@ -478,9 +478,9 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
478 478
 		$test->expect('1');
479 479
 		$test->get('/users/1?columns=id,location');
480 480
 		if (PHP_CRUD_API_Config::$dbengine=='SQLServer') {
481
-			$test->expect('{"id":"1","location":"POINT (30 20)"}');
481
+			$test->expect('{"id":1,"location":"POINT (30 20)"}');
482 482
 		} else {
483
-			$test->expect('{"id":"1","location":"POINT(30 20)"}');
483
+			$test->expect('{"id":1,"location":"POINT(30 20)"}');
484 484
 		}
485 485
 	}
486 486
 
@@ -489,9 +489,9 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
489 489
 		$test = new API($this);
490 490
 		$test->get('/users?columns=id,location');
491 491
 		if (PHP_CRUD_API_Config::$dbengine=='SQLServer') {
492
-			$test->expect('{"users":{"columns":["id","location"],"records":[["1","POINT (30 20)"]]}}');
492
+			$test->expect('{"users":{"columns":["id","location"],"records":[[1,"POINT (30 20)"]]}}');
493 493
 		} else {
494
-			$test->expect('{"users":{"columns":["id","location"],"records":[["1","POINT(30 20)"]]}}');
494
+			$test->expect('{"users":{"columns":["id","location"],"records":[[1,"POINT(30 20)"]]}}');
495 495
 		}
496 496
 	}
497 497
 
@@ -499,10 +499,10 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
499 499
 	{
500 500
 		if (PHP_CRUD_API_Config::$dbengine!='SQLServer') {
501 501
 			$test = new API($this);
502
-			$test->put('/users/1','{"id":"2","password":"testtest2"}');
502
+			$test->put('/users/1','{"id":2,"password":"testtest2"}');
503 503
 			$test->expect('1');
504 504
 			$test->get('/users/1?columns=id,username,password');
505
-			$test->expect('{"id":"1","username":"user1","password":"testtest2"}');
505
+			$test->expect('{"id":1,"username":"user1","password":"testtest2"}');
506 506
 		}
507 507
 	}
508 508
 
@@ -524,7 +524,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
524 524
 	{
525 525
 		$test = new API($this);
526 526
 		$test->get('/categories?filter[]=icon,is,null&transform=1');
527
-		$test->expect('{"categories":[{"id":"1","name":"announcement","icon":null},{"id":"2","name":"alert();","icon":null}]}');
527
+		$test->expect('{"categories":[{"id":1,"name":"announcement","icon":null},{"id":2,"name":"alert();","icon":null}]}');
528 528
 	}
529 529
 
530 530
 	public function testFilterCategoryOnNotNullIcon()
@@ -538,21 +538,21 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
538 538
 	{
539 539
 		$test = new API($this);
540 540
 		$test->get('/posts?filter[]=id,nin,1,2,3,4,7,8,9,10,11,12,13,14&transform=1');
541
-		$test->expect('{"posts":[{"id":"5","user_id":"1","category_id":"1","content":"#1"},{"id":"6","user_id":"1","category_id":"1","content":"#2"}]}');
541
+		$test->expect('{"posts":[{"id":5,"user_id":1,"category_id":1,"content":"#1"},{"id":6,"user_id":1,"category_id":1,"content":"#2"}]}');
542 542
 	}
543 543
 
544 544
 	public function testFilterPostsBetween()
545 545
 	{
546 546
 		$test = new API($this);
547 547
 		$test->get('/posts?filter[]=id,bt,5,6&transform=1');
548
-		$test->expect('{"posts":[{"id":"5","user_id":"1","category_id":"1","content":"#1"},{"id":"6","user_id":"1","category_id":"1","content":"#2"}]}');
548
+		$test->expect('{"posts":[{"id":5,"user_id":1,"category_id":1,"content":"#1"},{"id":6,"user_id":1,"category_id":1,"content":"#2"}]}');
549 549
 	}
550 550
 
551 551
 	public function testFilterPostsNotBetween()
552 552
 	{
553 553
 		$test = new API($this);
554 554
 		$test->get('/posts?filter[]=id,nbt,2,13&transform=1');
555
-		$test->expect('{"posts":[{"id":"1","user_id":"1","category_id":"1","content":"blog started"},{"id":"14","user_id":"1","category_id":"1","content":"#10"}]}');
555
+		$test->expect('{"posts":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":14,"user_id":1,"category_id":1,"content":"#10"}]}');
556 556
 	}
557 557
 
558 558
 	public function testColumnsWithTable()
@@ -566,42 +566,42 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
566 566
 	{
567 567
 		$test = new API($this);
568 568
 		$test->get('/posts?columns=posts.*&filter=id,eq,1&transform=1');
569
-		$test->expect('{"posts":[{"id":"1","user_id":"1","category_id":"1","content":"blog started"}]}');
569
+		$test->expect('{"posts":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"}]}');
570 570
 	}
571 571
 
572 572
 	public function testColumnsOnInclude()
573 573
 	{
574 574
 		$test = new API($this);
575 575
 		$test->get('/posts?include=categories&columns=categories.name&filter=id,eq,1&transform=1');
576
-		$test->expect('{"posts":[{"category_id":"1","categories":[{"id":"1","name":"announcement"}]}]}');
576
+		$test->expect('{"posts":[{"category_id":1,"categories":[{"id":1,"name":"announcement"}]}]}');
577 577
 	}
578 578
 
579 579
 	public function testFilterOnRelationAnd()
580 580
 	{
581 581
 		$test = new API($this);
582 582
 		$test->get('/categories?include=posts&filter[]=id,ge,1&filter[]=id,le,1&filter[]=id,le,2&filter[]=posts.id,lt,8&filter[]=posts.id,gt,4');
583
-		$test->expect('{"categories":{"columns":["id","name","icon"],"records":[["1","announcement",null]]},"posts":{"relations":{"category_id":"categories.id"},"columns":["id","user_id","category_id","content"],"records":[["5","1","1","#1"],["6","1","1","#2"],["7","1","1","#3"]]}}');
583
+		$test->expect('{"categories":{"columns":["id","name","icon"],"records":[[1,"announcement",null]]},"posts":{"relations":{"category_id":"categories.id"},"columns":["id","user_id","category_id","content"],"records":[[5,1,1,"#1"],[6,1,1,"#2"],[7,1,1,"#3"]]}}');
584 584
 	}
585 585
 
586 586
 	public function testFilterOnRelationOr()
587 587
 	{
588 588
 		$test = new API($this);
589 589
 		$test->get('/categories?include=posts&filter[]=id,ge,1&filter[]=id,le,1&filter[]=posts.id,eq,5&filter[]=posts.id,eq,6&filter[]=posts.id,eq,7&satisfy=all,posts.any');
590
-		$test->expect('{"categories":{"columns":["id","name","icon"],"records":[["1","announcement",null]]},"posts":{"relations":{"category_id":"categories.id"},"columns":["id","user_id","category_id","content"],"records":[["5","1","1","#1"],["6","1","1","#2"],["7","1","1","#3"]]}}');
590
+		$test->expect('{"categories":{"columns":["id","name","icon"],"records":[[1,"announcement",null]]},"posts":{"relations":{"category_id":"categories.id"},"columns":["id","user_id","category_id","content"],"records":[[5,1,1,"#1"],[6,1,1,"#2"],[7,1,1,"#3"]]}}');
591 591
 	}
592 592
 
593 593
 	public function testColumnsOnWrongInclude()
594 594
 	{
595 595
 		$test = new API($this);
596 596
 		$test->get('/posts?include=categories&columns=categories&filter=id,eq,1&transform=1');
597
-		$test->expect('{"posts":[{"category_id":"1","categories":[{"id":"1"}]}]}');
597
+		$test->expect('{"posts":[{"category_id":1,"categories":[{"id":1}]}]}');
598 598
 	}
599 599
 
600 600
 	public function testColumnsOnImplicitJoin()
601 601
 	{
602 602
 		$test = new API($this);
603 603
 		$test->get('/posts?include=tags&columns=posts.id,tags.name&filter=id,eq,1&transform=1');
604
-		$test->expect('{"posts":[{"id":"1","post_tags":[{"post_id":"1","tag_id":"1","tags":[{"id":"1","name":"funny"}]},{"post_id":"1","tag_id":"2","tags":[{"id":"2","name":"important"}]}]}]}');
604
+		$test->expect('{"posts":[{"id":1,"post_tags":[{"post_id":1,"tag_id":1,"tags":[{"id":1,"name":"funny"}]},{"post_id":1,"tag_id":2,"tags":[{"id":2,"name":"important"}]}]}]}');
605 605
 	}
606 606
 
607 607
 	public function testSpatialFilterWithin()
@@ -609,14 +609,14 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
609 609
 		if (PHP_CRUD_API_Config::$dbengine!='SQLite') {
610 610
 			$test = new API($this);
611 611
 			$test->get('/users?columns=id,username&filter=location,swi,POINT(30 20)');
612
-			$test->expect('{"users":{"columns":["id","username"],"records":[["1","user1"]]}}');
612
+			$test->expect('{"users":{"columns":["id","username"],"records":[[1,"user1"]]}}');
613 613
 		}
614 614
 	}
615 615
 
616 616
 	public function testAddPostsWithNonExistingCategory()
617 617
 	{ 
618 618
 		$test = new API($this);
619
-		$test->post('/posts','[{"user_id":"1","category_id":"1","content":"tests"},{"user_id":"1","category_id":"15","content":"tests"}]');
619
+		$test->post('/posts','[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":15,"content":"tests"}]');
620 620
 		$test->expect('null');
621 621
 		$test->get('/posts?columns=content&filter=content,eq,tests');
622 622
 		$test->expect('{"posts":{"columns":["content"],"records":[]}}');
@@ -625,7 +625,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
625 625
 	public function testAddPosts()
626 626
 	{
627 627
 		$test = new API($this);
628
-		$test->post('/posts','[{"user_id":"1","category_id":"1","content":"tests"},{"user_id":"1","category_id":"1","content":"tests"}]');
628
+		$test->post('/posts','[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":1,"content":"tests"}]');
629 629
 		$test->expectAny();
630 630
 		$test->get('/posts?columns=content&filter=content,eq,tests');
631 631
 		$test->expect('{"posts":{"columns":["content"],"records":[["tests"],["tests"]]}}');
@@ -642,14 +642,14 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
642 642
 	{
643 643
 		$test = new API($this);
644 644
 		$test->get('/tag_usage');
645
-		$test->expect('{"tag_usage":{"columns":["name","count"],"records":[["funny","2"],["important","2"]]}}');
645
+		$test->expect('{"tag_usage":{"columns":["name","count"],"records":[["funny",2],["important",2]]}}');
646 646
 	}
647 647
 
648 648
 	public function testUpdateMultipleTags()
649 649
 	{
650 650
 		$test = new API($this);
651 651
 		$test->get('/tags?transform=1');
652
-		$test->expect('{"tags":[{"id":"1","name":"funny"},{"id":"2","name":"important"}]}');
652
+		$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
653 653
 		$test->put('/tags/1,2','[{"name":"funny"},{"name":"important"}]');
654 654
 		$test->expect('[1,1]');
655 655
 	}
@@ -660,7 +660,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
660 660
 		$test->put('/tags/1,2,3','[{"name":"funny!!!"},{"name":"important"}]');
661 661
 		$test->expect(false,'Not found (subject)');
662 662
 		$test->get('/tags?transform=1');
663
-		$test->expect('{"tags":[{"id":"1","name":"funny"},{"id":"2","name":"important"}]}');
663
+		$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
664 664
 	}
665 665
 
666 666
 	public function testUpdateMultipleTagsWithoutFields()
@@ -669,7 +669,7 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
669 669
 		$test->put('/tags/1,2','[{"name":"funny!!!"},{}]');
670 670
 		$test->expect('null');
671 671
 		$test->get('/tags?transform=1');
672
-		$test->expect('{"tags":[{"id":"1","name":"funny"},{"id":"2","name":"important"}]}');
672
+		$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
673 673
 	}
674 674
 
675 675
 	public function testDeleteMultipleTags()
@@ -680,6 +680,13 @@ class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
680 680
 		$test->delete('/tags/3,4');
681 681
 		$test->expect('[1,1]');
682 682
 		$test->get('/tags?transform=1');
683
-		$test->expect('{"tags":[{"id":"1","name":"funny"},{"id":"2","name":"important"}]}');
683
+		$test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
684
+	}
685
+
686
+	public function testListProducts()
687
+	{
688
+		$test = new API($this);
689
+		$test->get('/products?transform=1');
690
+		$test->expect('{"products":[{"id":1,"name":"Calculator","price":"23.01"}]}');
684 691
 	}
685 692
 }

Loading…
Cancel
Save