1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Generator\Tests; |
5
|
|
|
|
6
|
|
|
use Faker\Factory; |
7
|
|
|
use SlayerBirden\DFCodeGeneration\Util\Lexer; |
8
|
|
|
use Zend\Code\Generator\ClassGenerator; |
9
|
|
|
use Zend\Code\Generator\FileGenerator; |
10
|
|
|
use Zend\Code\Generator\MethodGenerator; |
11
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
12
|
|
|
|
13
|
|
|
class Gets extends AbstractTest |
14
|
|
|
{ |
15
|
3 |
|
public function generate(): string |
16
|
|
|
{ |
17
|
3 |
|
$class = new ClassGenerator($this->getClassName()); |
18
|
|
|
|
19
|
3 |
|
$class->addMethodFromGenerator( |
20
|
3 |
|
(new MethodGenerator('_before')) |
21
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
22
|
3 |
|
->setBody($this->generateHaveInRepo(11)) |
23
|
|
|
); |
24
|
|
|
|
25
|
3 |
|
$class->addMethodFromGenerator( |
26
|
3 |
|
(new MethodGenerator('getAll')) |
27
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
28
|
3 |
|
->setBody($this->getAll()) |
29
|
|
|
); |
30
|
|
|
|
31
|
3 |
|
$class->addMethodFromGenerator( |
32
|
3 |
|
(new MethodGenerator('getSecondPage')) |
33
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
34
|
3 |
|
->setBody($this->getSecondPage()) |
35
|
|
|
); |
36
|
|
|
|
37
|
3 |
|
$class->addMethodFromGenerator( |
38
|
3 |
|
(new MethodGenerator('getFiltered')) |
39
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
40
|
3 |
|
->setBody($this->getFiltered()) |
41
|
|
|
); |
42
|
|
|
|
43
|
3 |
|
$class->addMethodFromGenerator( |
44
|
3 |
|
(new MethodGenerator('getSorted')) |
45
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
46
|
3 |
|
->setBody($this->getSorted()) |
47
|
|
|
); |
48
|
|
|
|
49
|
3 |
|
$class->addMethodFromGenerator( |
50
|
3 |
|
(new MethodGenerator('getNoResultsFilter')) |
51
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
52
|
3 |
|
->setBody($this->getNoResultsFilter()) |
53
|
|
|
); |
54
|
|
|
|
55
|
3 |
|
$class->addMethodFromGenerator( |
56
|
3 |
|
(new MethodGenerator('getInvalidFilter')) |
57
|
3 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
58
|
3 |
|
->setBody($this->getInvalidFilter()) |
59
|
|
|
); |
60
|
|
|
|
61
|
3 |
|
return (new FileGenerator()) |
62
|
3 |
|
->setClass($class) |
63
|
3 |
|
->generate(); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
private function getAll(): string |
67
|
|
|
{ |
68
|
|
|
$body = <<<'BODY' |
69
|
3 |
|
$I->wantTo('get all entities'); |
70
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
71
|
|
|
$I->sendGET('/%1$ss'); |
72
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
73
|
|
|
$I->seeResponseContainsJson([ |
74
|
|
|
'success' => true, |
75
|
|
|
'data' => [ |
76
|
|
|
'%1$ss' => %2$s, |
77
|
|
|
'count' => 11, |
78
|
|
|
] |
79
|
|
|
]); |
80
|
|
|
BODY; |
81
|
|
|
|
82
|
3 |
|
$allParams = $this->getAllParams(); |
83
|
|
|
|
84
|
3 |
|
return sprintf($body, $this->getLatestProvider()->getShortName(), var_export($allParams, true)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getAllParams(): array |
88
|
|
|
{ |
89
|
3 |
|
return array_map(function (EntityProviderInterface $provider) { |
90
|
3 |
|
return $provider->getPostParams(); |
91
|
3 |
|
}, $this->providers); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
private function getSecondPage(): string |
95
|
|
|
{ |
96
|
|
|
$body = <<<'BODY' |
97
|
3 |
|
$I->wantTo('get second page'); |
98
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
99
|
|
|
$I->sendGET('/%1$ss?p=2'); |
100
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
101
|
|
|
$I->seeResponseContainsJson([ |
102
|
|
|
'success' => true, |
103
|
|
|
'data' => [ |
104
|
|
|
'%1$ss' => %2$s, |
105
|
|
|
'count' => 11, |
106
|
|
|
] |
107
|
|
|
]); |
108
|
|
|
BODY; |
109
|
|
|
|
110
|
|
|
$params = [ |
111
|
3 |
|
$this->getHaveInRepoParams(10) |
112
|
|
|
]; |
113
|
|
|
|
114
|
3 |
|
return sprintf($body, $this->getLatestProvider()->getBaseName(), var_export($params, true)); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
private function getFiltered(): string |
118
|
|
|
{ |
119
|
|
|
$body = <<<'BODY' |
120
|
3 |
|
$I->wantTo('get filtered %1$ss'); |
121
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
122
|
|
|
$I->sendGET('/%1$ss?%2$s'); |
123
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
124
|
|
|
$I->seeResponseContainsJson([ |
125
|
|
|
'success' => true, |
126
|
|
|
'data' => [ |
127
|
|
|
'%1$ss' => %3$s, |
128
|
|
|
'count' => %4$d, |
129
|
|
|
] |
130
|
|
|
]); |
131
|
|
|
BODY; |
132
|
|
|
|
133
|
3 |
|
$chosen = $this->getHaveInRepoParams(5); |
134
|
|
|
|
135
|
3 |
|
$key = $this->getKey($chosen); |
136
|
3 |
|
if (empty($key)) { |
137
|
|
|
return '//TODO add filter case'; |
138
|
|
|
} |
139
|
3 |
|
$value = $chosen[$key]; |
140
|
|
|
|
141
|
3 |
|
$found = []; |
142
|
3 |
|
$all = $this->getAllParams(); |
143
|
3 |
|
foreach ($all as $item) { |
144
|
3 |
|
if ($item[$key] === $value) { |
145
|
3 |
|
$found[] = $item; |
146
|
|
|
} |
147
|
|
|
} |
148
|
3 |
|
$filterString = "f[$key]=$value"; |
149
|
|
|
|
150
|
3 |
|
return sprintf($body, $this->getLatestProvider()->getShortName(), $filterString, var_export($found, true), count($found)); |
151
|
|
|
} |
152
|
|
|
|
153
|
3 |
|
private function getKey(array $item): string |
154
|
|
|
{ |
155
|
3 |
|
$key = ''; |
156
|
3 |
|
foreach ($item as $key => $value) { |
157
|
3 |
|
if ($key === $this->getLatestProvider()->getIdName()) { |
158
|
2 |
|
continue; |
159
|
|
|
} |
160
|
3 |
|
break; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
return $key; |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
private function getSorted(): string |
167
|
|
|
{ |
168
|
|
|
$body = <<<'BODY' |
169
|
3 |
|
$I->wantTo('get %1$ss sorted by %2$s asc'); |
170
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
171
|
|
|
$I->sendGET('/%1$ss?s[%2$s]=asc'); |
172
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
173
|
|
|
$I->seeResponseContainsJson([ |
174
|
|
|
'success' => true, |
175
|
|
|
'data' => [ |
176
|
|
|
'%1$ss' => %3$s, |
177
|
|
|
'count' => 11, |
178
|
|
|
] |
179
|
|
|
]); |
180
|
|
|
BODY; |
181
|
3 |
|
$chosen = $this->getHaveInRepoParams(5); |
182
|
|
|
|
183
|
3 |
|
$key = $this->getKey($chosen); |
184
|
|
|
|
185
|
3 |
|
if (empty($key)) { |
186
|
|
|
return '//TODO add sorted case'; |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
$all = $this->getAllParams(); |
190
|
3 |
|
usort($all, function (array $a, array $b) use ($key) { |
191
|
3 |
|
return strcmp($a[$key], $b[$key]); |
192
|
3 |
|
}); |
193
|
|
|
|
194
|
3 |
|
return sprintf($body, $this->getLatestProvider()->getShortName(), $key, var_export($all, true)); |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
private function getNoResultsFilter(): string |
198
|
|
|
{ |
199
|
|
|
$body = <<<'BODY' |
200
|
3 |
|
$I->wantTo('attempt to get %1$ss filtered by non existing value'); |
201
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
202
|
|
|
$I->sendGET('/%1$ss?%2$s'); |
203
|
|
|
$I->seeResponseCodeIs(HttpCode::NOT_FOUND); |
204
|
|
|
$I->seeResponseContainsJson([ |
205
|
|
|
'success' => false, |
206
|
|
|
'data' => [ |
207
|
|
|
'%1$ss' => [], |
208
|
|
|
'count' => 0, |
209
|
|
|
] |
210
|
|
|
]); |
211
|
|
|
BODY; |
212
|
|
|
|
213
|
3 |
|
$chosen = $this->getHaveInRepoParams(5); |
214
|
|
|
|
215
|
|
|
|
216
|
3 |
|
$key = $this->getKey($chosen); |
217
|
3 |
|
if (empty($key)) { |
218
|
|
|
return '//TODO add filter case'; |
219
|
|
|
} |
220
|
3 |
|
$value = $this->getWrongValue($key); |
221
|
3 |
|
$filterString = "f[$key]=$value"; |
222
|
|
|
|
223
|
3 |
|
return sprintf($body, $this->getLatestProvider()->getShortName(), $filterString); |
224
|
|
|
} |
225
|
|
|
|
226
|
3 |
|
private function getWrongValue(string $key): string |
227
|
|
|
{ |
228
|
3 |
|
$faker = Factory::create(); |
229
|
3 |
|
$all = $this->haveInRepoParams[$this->entityClassName] ?? []; |
|
|
|
|
230
|
3 |
|
$values = array_map(function (array $item) use ($key) { |
231
|
|
|
return $item[$key] ?? null; |
232
|
3 |
|
}, $all); |
233
|
|
|
do { |
234
|
3 |
|
$wrongValue = $faker->word; |
235
|
3 |
|
} while (in_array($wrongValue, $values, true)); |
236
|
|
|
|
237
|
3 |
|
return $wrongValue; |
238
|
|
|
} |
239
|
|
|
|
240
|
3 |
|
private function getWrongKey(): string |
241
|
|
|
{ |
242
|
3 |
|
$faker = Factory::create(); |
243
|
3 |
|
$item = $this->getHaveInRepoParams(); |
244
|
3 |
|
$keys = array_keys($item); |
245
|
|
|
do { |
246
|
3 |
|
$wrongKey = $faker->word; |
247
|
3 |
|
} while (in_array($wrongKey, $keys, true)); |
248
|
|
|
|
249
|
3 |
|
return $wrongKey; |
250
|
|
|
} |
251
|
|
|
|
252
|
3 |
|
private function getInvalidFilter(): string |
253
|
|
|
{ |
254
|
|
|
$body = <<<'BODY' |
255
|
3 |
|
$I->wantTo('attempt to get %1$ss with wrong filters'); |
256
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
257
|
|
|
$I->sendGET('/%1$ss?%2$s'); |
258
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
259
|
|
|
$I->seeResponseContainsJson([ |
260
|
|
|
'success' => false, |
261
|
|
|
'data' => [ |
262
|
|
|
'%1$ss' => [], |
263
|
|
|
'count' => 0, |
264
|
|
|
] |
265
|
|
|
]); |
266
|
|
|
BODY; |
267
|
3 |
|
$key = $this->getWrongKey(); |
268
|
3 |
|
$filter = "f[$key]={$this->getWrongValue($key)}"; |
269
|
|
|
|
270
|
3 |
|
return sprintf($body, $this->getLatestProvider()->getShortName(), $filter); |
271
|
|
|
} |
272
|
|
|
|
273
|
3 |
|
public function getClassName(): string |
274
|
|
|
{ |
275
|
3 |
|
return 'Get' . Lexer::getPluralForm($this->getLatestProvider()->getBaseName()) . 'Cest'; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|