|
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
|
1 |
|
public function generate(): string |
|
14
|
|
|
{ |
|
15
|
1 |
|
$class = new ClassGenerator($this->getClassName()); |
|
16
|
|
|
|
|
17
|
1 |
|
$class->addMethodFromGenerator( |
|
18
|
1 |
|
(new MethodGenerator('_before')) |
|
19
|
1 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
|
20
|
1 |
|
->setBody($this->generateHaveInRepo(2)) |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
1 |
|
$class->addMethodFromGenerator( |
|
24
|
1 |
|
(new MethodGenerator('updateEntity')) |
|
25
|
1 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
|
26
|
1 |
|
->setBody($this->getSuccessCase()) |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
1 |
|
$class->addMethodFromGenerator( |
|
30
|
1 |
|
(new MethodGenerator('updateNonExistingEntity')) |
|
31
|
1 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
|
32
|
1 |
|
->setBody($this->getNonExistingCase()) |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
1 |
|
$class->addMethodFromGenerator( |
|
36
|
1 |
|
(new MethodGenerator('updateSetIdEntity')) |
|
37
|
1 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
|
38
|
1 |
|
->setBody($this->getUpdateSetIdCase()) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
1 |
|
$class->addMethodFromGenerator( |
|
42
|
1 |
|
(new MethodGenerator('updateInvalidInput')) |
|
43
|
1 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
|
44
|
1 |
|
->setBody($this->getInvalidInputCase()) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
1 |
|
$class->addMethodFromGenerator( |
|
48
|
1 |
|
(new MethodGenerator('updateFailedConstraint')) |
|
49
|
1 |
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
|
50
|
1 |
|
->setBody($this->getUniqueConstraintCase()) |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
1 |
|
return (new FileGenerator()) |
|
54
|
1 |
|
->setClass($class) |
|
55
|
1 |
|
->generate(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
private function getSuccessCase(): string |
|
59
|
|
|
{ |
|
60
|
|
|
$body = <<<'BODY' |
|
61
|
1 |
|
$I->wantTo('update %1$s'); |
|
62
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
63
|
|
|
$I->sendPUT('/%1$s/%2$d', %3$s); |
|
64
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
65
|
|
|
$I->seeResponseContainsJson([ |
|
66
|
|
|
'success' => true, |
|
67
|
|
|
'data' => [ |
|
68
|
|
|
'%1$s' => %3$s |
|
69
|
|
|
] |
|
70
|
|
|
]); |
|
71
|
|
|
BODY; |
|
72
|
|
|
|
|
73
|
1 |
|
$provider = $this->entityProviderFactory->create($this->entityClassName); |
|
74
|
|
|
|
|
75
|
1 |
|
return sprintf($body, $provider->getShortName(), $this->getLatestProvider()->getId(), |
|
76
|
1 |
|
var_export($provider->getPostParams(), true)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
private function getNonExistingCase(): string |
|
80
|
|
|
{ |
|
81
|
|
|
$body = <<<'BODY' |
|
82
|
1 |
|
$I->wantTo('update non existing %1$s'); |
|
83
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
84
|
|
|
$I->sendPUT('/%1$s/0', %2$s); |
|
85
|
|
|
$I->seeResponseCodeIs(HttpCode::NOT_FOUND); |
|
86
|
|
|
$I->seeResponseContainsJson([ |
|
87
|
|
|
'success' => false, |
|
88
|
|
|
'data' => [ |
|
89
|
|
|
'%1$s' => null |
|
90
|
|
|
] |
|
91
|
|
|
]); |
|
92
|
|
|
BODY; |
|
93
|
|
|
|
|
94
|
1 |
|
$provider = $this->entityProviderFactory->create($this->entityClassName); |
|
95
|
|
|
|
|
96
|
1 |
|
return sprintf($body, $provider->getShortName(), var_export($provider->getPostParams(), true)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
private function getUpdateSetIdCase(): string |
|
100
|
|
|
{ |
|
101
|
|
|
$body = <<<'BODY' |
|
102
|
1 |
|
$I->wantTo('update %1$s and attempt to set id'); |
|
103
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
104
|
|
|
$I->sendPUT('/%1$s/%4$d', %2$s); |
|
105
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
106
|
|
|
$I->seeResponseContainsJson([ |
|
107
|
|
|
'success' => true, |
|
108
|
|
|
'data' => [ |
|
109
|
|
|
'%1$s' => %3$s |
|
110
|
|
|
] |
|
111
|
|
|
]); |
|
112
|
|
|
BODY; |
|
113
|
1 |
|
$first = reset($this->providers); |
|
114
|
1 |
|
$last = end($this->providers); |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
1 |
|
$provider = $this->entityProviderFactory->create($this->entityClassName); |
|
118
|
1 |
|
$paramsWithId = $provider->getPostParams(); |
|
119
|
1 |
|
$paramsWithId['id'] = $last->getId(); |
|
120
|
|
|
|
|
121
|
1 |
|
$expected = $provider->getPostParams(); |
|
122
|
1 |
|
$expected['id'] = $first->getId(); |
|
123
|
|
|
|
|
124
|
1 |
|
return sprintf($body, $provider->getShortName(), var_export($paramsWithId, true), var_export($expected, true), |
|
125
|
1 |
|
$first->getId()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
private function getInvalidInputCase(): string |
|
129
|
|
|
{ |
|
130
|
1 |
|
$provider = $this->entityProviderFactory->create($this->entityClassName); |
|
131
|
1 |
|
$params = $provider->getPostParams(); |
|
132
|
1 |
|
if (count($params) > 0) { |
|
133
|
|
|
$body = <<<'BODY' |
|
134
|
1 |
|
$I->wantTo('update %1$s set invalid input'); |
|
135
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
136
|
|
|
$I->sendPUT('/%1$s/%4$d', %2$s); |
|
137
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
138
|
|
|
$I->seeResponseContainsJson([ |
|
139
|
|
|
'success' => false, |
|
140
|
|
|
'data' => [ |
|
141
|
|
|
'validation' => %3$s |
|
142
|
|
|
] |
|
143
|
|
|
]); |
|
144
|
|
|
BODY; |
|
145
|
|
|
|
|
146
|
1 |
|
$validation = []; |
|
147
|
1 |
|
foreach ($params as $key => $val) { |
|
148
|
1 |
|
$validation[] = [ |
|
149
|
1 |
|
'field' => $key |
|
150
|
|
|
]; |
|
151
|
1 |
|
unset($params[$key]); |
|
152
|
1 |
|
break; |
|
153
|
|
|
} |
|
154
|
1 |
|
return sprintf($body, $provider->getShortName(), var_export($params, true), var_export($validation, true), |
|
155
|
1 |
|
$this->getLatestProvider()->getId()); |
|
156
|
|
|
} else { |
|
157
|
|
|
return '//TODO add validation case'; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
private function getUniqueConstraintCase(): string |
|
162
|
|
|
{ |
|
163
|
1 |
|
if (!$this->getLatestProvider()->hasUnique()) { |
|
164
|
|
|
return '//TODO add unique case'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
1 |
|
$params = $this->getHaveInRepoParams(); |
|
168
|
|
|
|
|
169
|
|
|
$body = <<<'BODY' |
|
170
|
1 |
|
$I->wantTo('update %1$s, fail constraint'); |
|
171
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
172
|
|
|
$I->sendPUT('/%1$s/%3$d', %2$s); |
|
173
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
174
|
|
|
$I->seeResponseContainsJson([ |
|
175
|
|
|
'success' => false, |
|
176
|
|
|
]); |
|
177
|
|
|
BODY; |
|
178
|
|
|
|
|
179
|
1 |
|
return sprintf($body, $this->getLatestProvider()->getShortName(), var_export($params, true), |
|
180
|
1 |
|
$this->getLatestProvider()->getId()); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public function getClassName(): string |
|
184
|
|
|
{ |
|
185
|
1 |
|
return 'Update' . $this->getLatestProvider()->getBaseName() . 'Cest'; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|