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