|
@@ -1,105 +0,0 @@
|
1
|
|
-<?php
|
2
|
|
-require "api.php";
|
3
|
|
-
|
4
|
|
-class MySQL_CRUD_API_Test extends PHPUnit_Framework_TestCase
|
5
|
|
-{
|
6
|
|
- private function expectQueries($queries)
|
7
|
|
- {
|
8
|
|
- $mysqli = $this->getMockBuilder('mysqli')
|
9
|
|
- ->setMethods(array('query','real_escape_string'))
|
10
|
|
- ->getMock();
|
11
|
|
- $mysqli->expects($this->any())
|
12
|
|
- ->method('real_escape_string')
|
13
|
|
- ->will($this->returnArgument(0));
|
14
|
|
- $mysqli->expects($this->any())
|
15
|
|
- ->method('query')
|
16
|
|
- ->will($this->returnCallback(function() use ($queries) {
|
17
|
|
- static $i = 0;
|
18
|
|
- $args = func_get_args();
|
19
|
|
- list($query,$fields,$results) = $queries[$i++];
|
20
|
|
- $fields = array_map(function($v){ return (object)array('name'=>$v); },$fields);
|
21
|
|
- $this->assertEquals($query,$args[0]);
|
22
|
|
- $mysqli_result = $this->getMockBuilder('mysqli_result')
|
23
|
|
- ->setMethods(array('fetch_fields','fetch_row','close'))
|
24
|
|
- ->disableOriginalConstructor()
|
25
|
|
- ->getMock();
|
26
|
|
- $mysqli_result->expects($this->any())
|
27
|
|
- ->method('fetch_fields')
|
28
|
|
- ->willReturn($fields);
|
29
|
|
- $mysqli_result->expects($this->any())
|
30
|
|
- ->method('fetch_row')
|
31
|
|
- ->will($this->returnCallback(function() use ($results) {
|
32
|
|
- static $r = 0;
|
33
|
|
- return isset($results[$r])?$results[$r++]:false;
|
34
|
|
- }));
|
35
|
|
- return $mysqli_result;
|
36
|
|
- }));
|
37
|
|
-
|
38
|
|
- return $mysqli;
|
39
|
|
- }
|
40
|
|
-
|
41
|
|
- private function expect($method,$url,$queries,$output)
|
42
|
|
- {
|
43
|
|
- $url = explode('?',$url,2);
|
44
|
|
- $request = $url[0];
|
45
|
|
- $get = array();
|
46
|
|
- if (isset($url[1])) {
|
47
|
|
- parse_str($url[1],$get);
|
48
|
|
- }
|
49
|
|
- $mysqli = $this->expectQueries($queries);
|
50
|
|
- $config = array(
|
51
|
|
- 'method' =>$method,
|
52
|
|
- 'request' =>$request,
|
53
|
|
- 'get' =>$get,
|
54
|
|
- 'database'=>'database',
|
55
|
|
- 'mysqli'=>$mysqli,
|
56
|
|
- );
|
57
|
|
- $api = new MySQL_CRUD_API($config);
|
58
|
|
- $this->expectOutputString($output);
|
59
|
|
- $api->executeCommand();
|
60
|
|
- }
|
61
|
|
-
|
62
|
|
- public function testList()
|
63
|
|
- {
|
64
|
|
- $this->expect(
|
65
|
|
- "GET",
|
66
|
|
- "/table",
|
67
|
|
- array(
|
68
|
|
- array(
|
69
|
|
- "SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE 'table' AND `TABLE_SCHEMA` = 'database'",
|
70
|
|
- array('table_name'),
|
71
|
|
- array(array('table')),
|
72
|
|
- ),array(
|
73
|
|
- "SELECT * FROM `table`",
|
74
|
|
- array('id','name'),
|
75
|
|
- array(array('1','value1'),array('2','value2'),array('3','value3'),array('4','value4'),array('5','value5')),
|
76
|
|
- )
|
77
|
|
- ),
|
78
|
|
- '{"table":{"columns":["id","name"],"records":[["1","value1"],["2","value2"],["3","value3"],["4","value4"],["5","value5"]]}}'
|
79
|
|
- );
|
80
|
|
- }
|
81
|
|
-
|
82
|
|
- public function testListPageFilterMatchOrder()
|
83
|
|
- {
|
84
|
|
- $this->expect(
|
85
|
|
- "GET",
|
86
|
|
- "/table?page=2,2&filter=id:3&match=from&order=id,desc",
|
87
|
|
- array(
|
88
|
|
- array(
|
89
|
|
- "SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE 'table' AND `TABLE_SCHEMA` = 'database'",
|
90
|
|
- array('table_name'),
|
91
|
|
- array(array('table')),
|
92
|
|
- ),array(
|
93
|
|
- "SELECT COUNT(*) FROM `table` WHERE `id` >= '3'",
|
94
|
|
- array('count'),
|
95
|
|
- array(array('3')),
|
96
|
|
- ),array(
|
97
|
|
- "SELECT * FROM `table` WHERE `id` >= '3' ORDER BY `id` DESC LIMIT 2 OFFSET 2",
|
98
|
|
- array('id','name'),
|
99
|
|
- array(array('3','value3')),
|
100
|
|
- )
|
101
|
|
- ),
|
102
|
|
- '{"table":{"columns":["id","name"],"records":[["3","value3"]],"results":3}}'
|
103
|
|
- );
|
104
|
|
- }
|
105
|
|
-}
|