Skip to content

Commit 3da50cc

Browse files
committed
update 一些测试用例 && 测试过程中的调整
1 parent f202d81 commit 3da50cc

File tree

7 files changed

+308
-73
lines changed

7 files changed

+308
-73
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ https://github.com/Tencent/APIJSON/commits/master
3737

3838
1. ✅最基本CRUD 包括批量写入 批量更新 批量删除
3939
2. ✅支持@column @having @order @group
40-
3. ✅支持运算符 {}, <>, }{@, $, %, ~
40+
3. ✅支持运算符 {}, }{@, $, %, ~
4141
4. ✅支持多表查询、一对一查询、一对多查询、数组内多对多查询
4242
5. ✅支持数组内count/page
4343
6. ✅支持debug标签可返回语句执行

app/ApiJson/Method/AbstractMethod.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ abstract class AbstractMethod
1616
/** @var bool $isQueryMany */
1717
protected bool $isQueryMany = false;
1818

19+
/** @var bool $arrayQuery */
20+
protected bool $arrayQuery = false;
21+
1922
public function __construct(protected TableEntity $tableEntity, protected string $method = 'GET')
2023
{
2124
$this->buildQuery();
@@ -52,9 +55,14 @@ public function setQueryMany(bool $isQueryMany = false)
5255
$this->isQueryMany = $isQueryMany;
5356
}
5457

58+
public function setArrayQuery(bool $arrayQuery = false)
59+
{
60+
$this->arrayQuery = $arrayQuery;
61+
}
62+
5563
protected function isQueryMany(): bool
5664
{
57-
return $this->isQueryMany;
65+
return $this->isQueryMany; //可能有不止一个因素影响
5866
}
5967

6068
abstract protected function validateCondition(): bool;

app/ApiJson/Method/GetMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function process()
2121
$result = $this->query->all();
2222
if ($queryMany) {
2323
foreach ($result as $key => $item) {
24-
$result[$key] = [$this->tableEntity->getTableName() => $item];
24+
$result[$key] = $this->arrayQuery ? [$this->tableEntity->getTableName() => $item] : $item;
2525
}
2626
} else {
2727
$result = current($result);

app/ApiJson/Parse/Parse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array
6464
/** @var AbstractMethod $method */
6565
$method = new $methodClass($this->tableEntities[$tableName], $this->method);
6666
$method->setQueryMany($isQueryMany);
67+
$method->setArrayQuery(false);
6768
$response = $method->handle();
6869
if (!is_null($response)) {
6970
$result[$tableName] = $response;
@@ -74,7 +75,7 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array
7475
return $this->resultExtendHandle($result);
7576
}
7677

77-
protected function resultExtendHandle(array $result)
78+
protected function resultExtendHandle(array $result): array
7879
{
7980
if ($this->tagColumn['debug']) {
8081
$result['debug'] = (new Context())->get('querySql');
@@ -105,6 +106,7 @@ protected function handleArray(array $jsonData, array $extendData = []): array
105106
$this->tableEntities['[]'][$tableName] = new TableEntity($tableName, $jsonData, $this->getGlobalArgs(), array_merge($result, $extendData));
106107
$method = new GetMethod($this->tableEntities['[]'][$tableName], $this->method);
107108
$method->setQueryMany($result == [[]]);
109+
$method->setArrayQuery(true);
108110
$response = $method->handle();
109111
if (!is_null($response)) {
110112
if ($result == [[]]) {

test/Cases/ExampleTest.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

test/Cases/GetTest.php

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Cases;
13+
14+
use App\ApiJson\Parse\Parse;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @internal
19+
* @coversNothing
20+
*/
21+
class GetTest extends TestCase
22+
{
23+
protected string $method = 'GET';
24+
25+
public function testColumn()
26+
{
27+
$json = [
28+
'User' => [
29+
'id' => 1,
30+
'@column' => 'id'
31+
]
32+
];
33+
$parse = new Parse($json, $this->method, '');
34+
$result = $parse->handle();
35+
36+
$this->assertSame([
37+
'User' => [
38+
'id' => 1
39+
]
40+
], $result);
41+
}
42+
43+
public function testWhere()
44+
{
45+
$json = [
46+
'User' => [
47+
'id' => 1
48+
]
49+
];
50+
$parse = new Parse($json, $this->method, '');
51+
$result = $parse->handle();
52+
53+
$this->assertSame([
54+
'User' => [
55+
'id' => 1,
56+
'username' => 'abigeater',
57+
'email' => 'abigeater@163.com',
58+
'password' => '1',
59+
'create_time' => '2021-11-02 06:57:56'
60+
]
61+
], $result);
62+
}
63+
64+
public function testWhereIn()
65+
{
66+
$json = [
67+
'User[]' => [
68+
'id{}' => [1,2]
69+
]
70+
];
71+
$parse = new Parse($json, $this->method, '');
72+
$result = $parse->handle();
73+
74+
$this->assertSame([
75+
'User[]' => [
76+
[
77+
'id' => 1,
78+
'username' => 'abigeater',
79+
'email' => 'abigeater@163.com',
80+
'password' => '1',
81+
'create_time' => '2021-11-02 06:57:56'
82+
]
83+
]
84+
], $result);
85+
}
86+
87+
public function testWhereQueryMany()
88+
{
89+
$json = [
90+
'User[]' => [
91+
]
92+
];
93+
$parse = new Parse($json, $this->method, '');
94+
$result = $parse->handle();
95+
96+
$this->assertSame([
97+
'User[]' => [
98+
[
99+
'id' => 1,
100+
'username' => 'abigeater',
101+
'email' => 'abigeater@163.com',
102+
'password' => '1',
103+
'create_time' => '2021-11-02 06:57:56'
104+
]
105+
]
106+
], $result);
107+
}
108+
109+
public function testWhereCondition()
110+
{
111+
$json = [
112+
'User' => [
113+
'id{}' => '>0,<=1'
114+
]
115+
];
116+
$parse = new Parse($json, $this->method, '');
117+
$result = $parse->handle();
118+
119+
$this->assertSame([
120+
'User' => [
121+
'id' => 1,
122+
'username' => 'abigeater',
123+
'email' => 'abigeater@163.com',
124+
'password' => '1',
125+
'create_time' => '2021-11-02 06:57:56'
126+
]
127+
], $result);
128+
129+
$json = [
130+
'User' => [
131+
'id{}' => '>1,<1'
132+
]
133+
];
134+
$parse = new Parse($json, $this->method, '');
135+
$result = $parse->handle();
136+
137+
$this->assertSame([
138+
'User' => []
139+
], $result);
140+
}
141+
142+
public function testWhereArray()
143+
{
144+
$json = [
145+
'[]' => [
146+
'count'=> 10,
147+
'page' => 1,
148+
'User' => [
149+
'username' => 'abigeater'
150+
]
151+
]
152+
];
153+
$parse = new Parse($json, $this->method, '');
154+
$result = $parse->handle();
155+
156+
$this->assertSame([
157+
'[]' => [
158+
[
159+
'User' => [
160+
'id' => 1,
161+
'username' => 'abigeater',
162+
'email' => 'abigeater@163.com',
163+
'password' => '1',
164+
'create_time' => '2021-11-02 06:57:56'
165+
]
166+
]
167+
]
168+
], $result);
169+
}
170+
171+
public function testQueryManyTable()
172+
{
173+
$json = [
174+
'User' => [
175+
'username' => 'abigeater'
176+
],
177+
'Message' => [
178+
],
179+
];
180+
$parse = new Parse($json, $this->method, '');
181+
$result = $parse->handle();
182+
183+
$this->assertSame([
184+
'User' => [
185+
'id' => 1,
186+
'username' => 'abigeater',
187+
'email' => 'abigeater@163.com',
188+
'password' => '1',
189+
'create_time' => '2021-11-02 06:57:56'
190+
],
191+
'Message' => [
192+
'id' => 12,
193+
'user_id' => 1,
194+
'watched_message_id' => '2'
195+
]
196+
], $result);
197+
}
198+
199+
public function testQueryOneOne()
200+
{
201+
$json = [
202+
'Message' => [
203+
],
204+
'User' => [
205+
'id@' => 'Message/user_id'
206+
],
207+
];
208+
$parse = new Parse($json, $this->method, '');
209+
$result = $parse->handle();
210+
211+
$this->assertSame([
212+
'Message' => [
213+
'id' => 12,
214+
'user_id' => 1,
215+
'watched_message_id' => '2'
216+
],
217+
'User' => [
218+
'id' => 1,
219+
'username' => 'abigeater',
220+
'email' => 'abigeater@163.com',
221+
'password' => '1',
222+
'create_time' => '2021-11-02 06:57:56'
223+
]
224+
], $result);
225+
}
226+
227+
public function testQueryOneMany()
228+
{
229+
$json = [
230+
'User' => [
231+
'id' => 1
232+
],
233+
'[]' => [
234+
'Message' => [
235+
"user_id@" => "User/id"
236+
],
237+
]
238+
];
239+
$parse = new Parse($json, $this->method, '');
240+
$result = $parse->handle();
241+
242+
$this->assertSame([
243+
'User' => [
244+
'id' => 1,
245+
'username' => 'abigeater',
246+
'email' => 'abigeater@163.com',
247+
'password' => '1',
248+
'create_time' => '2021-11-02 06:57:56'
249+
],
250+
'[]' => [
251+
[
252+
'Message' => [
253+
'id' => 12,
254+
'user_id' => 1,
255+
'watched_message_id' => '2'
256+
],
257+
]
258+
]
259+
], $result);
260+
}
261+
262+
public function testQueryArrayOneOne()
263+
{
264+
$json = [
265+
'[]' => [
266+
'Message' => [],
267+
'User' => [
268+
"id@" => "[]/Message/user_id"
269+
],
270+
]
271+
];
272+
$parse = new Parse($json, $this->method, '');
273+
$result = $parse->handle();
274+
275+
$this->assertSame([
276+
'[]' => [
277+
[
278+
'Message' => [
279+
'id' => 12,
280+
'user_id' => 1,
281+
'watched_message_id' => '2'
282+
],
283+
'User' => [
284+
'id' => 1,
285+
'username' => 'abigeater',
286+
'email' => 'abigeater@163.com',
287+
'password' => '1',
288+
'create_time' => '2021-11-02 06:57:56'
289+
]
290+
]
291+
]
292+
], $result);
293+
}
294+
}

0 commit comments

Comments
 (0)