1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Generator\Tests; |
5
|
|
|
|
6
|
|
|
use Zend\Code\Generator\ClassGenerator; |
7
|
|
|
use Zend\Code\Generator\FileGenerator; |
8
|
|
|
use Zend\Code\Generator\MethodGenerator; |
9
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
10
|
|
|
|
11
|
|
|
class Update extends AbstractTest |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return string |
15
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
16
|
|
|
* @throws \ReflectionException |
17
|
|
|
*/ |
18
|
|
|
public function generate(): string |
19
|
|
|
{ |
20
|
|
|
$className = 'Update' . $this->getBaseName($this->entityClassName) . 'Cest'; |
21
|
|
|
$baseName = $this->getBaseName($this->entityClassName); |
22
|
|
|
$class = new ClassGenerator($className); |
23
|
|
|
|
24
|
|
|
$class->addMethodFromGenerator( |
25
|
|
|
(new MethodGenerator('_before')) |
26
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
27
|
|
|
->setBody($this->getBefore(2)) |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$class->addMethodFromGenerator( |
31
|
|
|
(new MethodGenerator('update' . $baseName)) |
32
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
33
|
|
|
->setBody($this->getSuccessCase()) |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$class->addMethodFromGenerator( |
37
|
|
|
(new MethodGenerator('updateNonExisting' . $baseName)) |
38
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
39
|
|
|
->setBody($this->getNonExistingCase()) |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$class->addMethodFromGenerator( |
43
|
|
|
(new MethodGenerator('updateSetId' . $baseName)) |
44
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
45
|
|
|
->setBody($this->getUpdateSetIdCase()) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$class->addMethodFromGenerator( |
49
|
|
|
(new MethodGenerator('updateInvalidInput' . $baseName)) |
50
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
51
|
|
|
->setBody($this->getInvalidInputCase()) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$class->addMethodFromGenerator( |
55
|
|
|
(new MethodGenerator('updateFailedConstraint' . $baseName)) |
56
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
57
|
|
|
->setBody($this->getUniqueConstraintCase()) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return (new FileGenerator()) |
61
|
|
|
->setClass($class) |
62
|
|
|
->generate(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
68
|
|
|
* @throws \ReflectionException |
69
|
|
|
*/ |
70
|
|
|
private function getSuccessCase(): string |
71
|
|
|
{ |
72
|
|
|
$body = <<<'BODY' |
73
|
|
|
$I->wantTo('update %1$s'); |
74
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
75
|
|
|
$I->sendPUT('/%1$s/%2$d', %3$s); |
76
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
77
|
|
|
$I->seeResponseContainsJson([ |
78
|
|
|
'success' => true, |
79
|
|
|
'data' => [ |
80
|
|
|
'%1$s' => %3$s |
81
|
|
|
] |
82
|
|
|
]); |
83
|
|
|
BODY; |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
return sprintf($body, $this->shortName, $this->getId($this->entityClassName), |
87
|
|
|
var_export($this->getPostParams(), true)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
93
|
|
|
* @throws \ReflectionException |
94
|
|
|
*/ |
95
|
|
|
private function getNonExistingCase(): string |
96
|
|
|
{ |
97
|
|
|
$body = <<<'BODY' |
98
|
|
|
$I->wantTo('update non existing %1$s'); |
99
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
100
|
|
|
$I->sendPUT('/%1$s/0', %2$s); |
101
|
|
|
$I->seeResponseCodeIs(HttpCode::NOT_FOUND); |
102
|
|
|
$I->seeResponseContainsJson([ |
103
|
|
|
'success' => false, |
104
|
|
|
'data' => [ |
105
|
|
|
'%1$s' => null |
106
|
|
|
] |
107
|
|
|
]); |
108
|
|
|
BODY; |
109
|
|
|
|
110
|
|
|
return sprintf($body, $this->shortName, var_export($this->getPostParams(), true)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
116
|
|
|
* @throws \ReflectionException |
117
|
|
|
*/ |
118
|
|
|
private function getUpdateSetIdCase(): string |
119
|
|
|
{ |
120
|
|
|
$body = <<<'BODY' |
121
|
|
|
$I->wantTo('update %1$s and attempt to set id'); |
122
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
123
|
|
|
$I->sendPUT('/%1$s/1', %2$s); |
124
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
125
|
|
|
$I->seeResponseContainsJson([ |
126
|
|
|
'success' => true, |
127
|
|
|
'data' => [ |
128
|
|
|
'%1$s' => %3$s |
129
|
|
|
] |
130
|
|
|
]); |
131
|
|
|
BODY; |
132
|
|
|
$params = $this->getPostParams(); |
133
|
|
|
$paramsWithId = $params; |
134
|
|
|
$paramsWithId['id'] = 2; |
135
|
|
|
|
136
|
|
|
$expected = $params; |
137
|
|
|
$expected['id'] = 1; |
138
|
|
|
|
139
|
|
|
return sprintf($body, $this->shortName, var_export($paramsWithId, true), var_export($expected, true)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
145
|
|
|
* @throws \ReflectionException |
146
|
|
|
*/ |
147
|
|
|
private function getInvalidInputCase(): string |
148
|
|
|
{ |
149
|
|
|
$params = $this->getPostParams(); |
150
|
|
|
if (count($params) > 0) { |
151
|
|
|
$body = <<<'BODY' |
152
|
|
|
$I->wantTo('update %1$s set invalid input'); |
153
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
154
|
|
|
$I->sendPUT('/%1$s/1', %2$s); |
155
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
156
|
|
|
$I->seeResponseContainsJson([ |
157
|
|
|
'success' => false, |
158
|
|
|
'data' => [ |
159
|
|
|
'validation' => %3$s |
160
|
|
|
] |
161
|
|
|
]); |
162
|
|
|
BODY; |
163
|
|
|
|
164
|
|
|
$validation = []; |
165
|
|
|
foreach ($params as $key => $val) { |
166
|
|
|
$validation[] = [ |
167
|
|
|
'field' => $key |
168
|
|
|
]; |
169
|
|
|
unset($params[$key]); |
170
|
|
|
break; |
171
|
|
|
} |
172
|
|
|
return sprintf($body, $this->shortName, var_export($params, true), var_export($validation, true)); |
173
|
|
|
} else { |
174
|
|
|
return '//TODO add validation case'; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function getUniqueConstraintCase(): string |
179
|
|
|
{ |
180
|
|
|
if (empty($this->unique)) { |
181
|
|
|
return '//TODO add unique case'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$params = $this->getHaveInRepoParams($this->entityClassName); |
185
|
|
|
foreach ($params as $key => $param) { |
186
|
|
|
if ($key === 'id') { |
187
|
|
|
unset($params[$key]); |
188
|
|
|
} |
189
|
|
|
if (is_object($param) && method_exists($param, 'getId')) { |
190
|
|
|
$params[$key] = $param->getId(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$body = <<<'BODY' |
195
|
|
|
$I->wantTo('update %1$s, fail constraint'); |
196
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
197
|
|
|
$I->sendPUT('/%1$s/2', %2$s); |
198
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
199
|
|
|
$I->seeResponseContainsJson([ |
200
|
|
|
'success' => false, |
201
|
|
|
]); |
202
|
|
|
BODY; |
203
|
|
|
|
204
|
|
|
return sprintf($body, $this->shortName, var_export($params, true)); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|