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