1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SoliDry\Blocks; |
4
|
|
|
|
5
|
|
|
use Faker\Factory; |
6
|
|
|
use SoliDry\Helpers\Classes; |
7
|
|
|
use SoliDry\Helpers\MethodOptions; |
8
|
|
|
use SoliDry\Types\DefaultInterface; |
9
|
|
|
use SoliDry\Types\MethodsInterface; |
10
|
|
|
use SoliDry\Types\PhpInterface; |
11
|
|
|
use SoliDry\Types\ApiInterface; |
12
|
|
|
use SoliDry\Types\TestsInterface; |
13
|
|
|
|
14
|
|
|
class Tests |
15
|
|
|
{ |
16
|
|
|
use ContentManager; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private string $className; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private array $attributesState = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected string $sourceCode = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Tests constructor. |
35
|
|
|
* @param $generator |
36
|
|
|
*/ |
37
|
|
|
public function __construct($generator) |
38
|
|
|
{ |
39
|
|
|
$this->generator = $generator; |
40
|
|
|
$this->className = Classes::getClassName($this->generator->objectName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setContent() : void |
44
|
|
|
{ |
45
|
|
|
$this->setTag(); |
46
|
|
|
$this->startClass($this->className . DefaultInterface::FUNCTIONAL_POSTFIX); |
47
|
|
|
$methodOpts = new MethodOptions(); |
48
|
|
|
$methodOpts->setName(MethodsInterface::TEST_BEFORE); |
49
|
|
|
$methodOpts->setParams([ |
50
|
|
|
TestsInterface::FUNCTIONAL_TESTER => TestsInterface::PARAM_I, |
51
|
|
|
]); |
52
|
|
|
$this->startMethod($methodOpts); |
53
|
|
|
$this->endMethod(); |
54
|
|
|
$methodOpts->setName(MethodsInterface::TEST_AFTER); |
55
|
|
|
$this->startMethod($methodOpts); |
56
|
|
|
$this->endMethod(); |
57
|
|
|
// main test methods |
58
|
|
|
$this->collectProps(); |
59
|
|
|
$this->setComment(DefaultInterface::METHOD_START); |
60
|
|
|
$this->setCreateContent($methodOpts); |
61
|
|
|
$this->setIndexContent($methodOpts); |
62
|
|
|
$this->setViewContent($methodOpts); |
63
|
|
|
$this->setUpdateContent($methodOpts); |
64
|
|
|
$this->setDeleteContent($methodOpts); |
65
|
|
|
$this->setComment(DefaultInterface::METHOD_END); |
66
|
|
|
$this->endClass(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param MethodOptions $methodOpts |
71
|
|
|
*/ |
72
|
|
|
private function setIndexContent(MethodOptions $methodOpts) : void |
73
|
|
|
{ |
74
|
|
|
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::INDEX)); |
75
|
|
|
$this->startMethod($methodOpts); |
76
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO, |
77
|
|
|
[TestsInterface::TEST_WORD . PhpInterface::SPACE . $this->generator->objectName . ' ' . MethodsInterface::INDEX]); |
78
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_GET, |
79
|
|
|
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName)]); |
80
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON); |
81
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false); |
82
|
|
|
$this->endMethod(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param MethodOptions $methodOpts |
87
|
|
|
*/ |
88
|
|
|
private function setViewContent(MethodOptions $methodOpts) : void |
89
|
|
|
{ |
90
|
|
|
$id = $id = $this->getId(); |
|
|
|
|
91
|
|
|
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::VIEW)); |
92
|
|
|
$this->startMethod($methodOpts); |
93
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO, |
94
|
|
|
[TestsInterface::TEST_WORD . PhpInterface::SPACE . $this->generator->objectName |
95
|
|
|
. PhpInterface::SPACE . MethodsInterface::VIEW]); |
96
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_GET, |
97
|
|
|
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName) |
98
|
|
|
. PhpInterface::SLASH . $id]); |
99
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON); |
100
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false); |
101
|
|
|
$this->endMethod(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param MethodOptions $methodOpts |
106
|
|
|
*/ |
107
|
|
|
private function setCreateContent(MethodOptions $methodOpts) : void |
108
|
|
|
{ |
109
|
|
|
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::CREATE)); |
110
|
|
|
$this->startMethod($methodOpts); |
111
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO, |
112
|
|
|
[TestsInterface::TEST_WORD . PhpInterface::SPACE . $this->generator->objectName |
113
|
|
|
. PhpInterface::SPACE . MethodsInterface::CREATE]); |
114
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_POST, |
115
|
|
|
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName), |
116
|
|
|
$this->getJsonApiRequest()]); |
117
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON); |
118
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false); |
119
|
|
|
$this->endMethod(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param MethodOptions $methodOpts |
124
|
|
|
*/ |
125
|
|
|
private function setUpdateContent(MethodOptions $methodOpts) : void |
126
|
|
|
{ |
127
|
|
|
$id = $id = $this->getId(); |
|
|
|
|
128
|
|
|
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::UPDATE)); |
129
|
|
|
$this->startMethod($methodOpts); |
130
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO, |
131
|
|
|
[TestsInterface::TEST_WORD . PhpInterface::SPACE . $this->generator->objectName |
132
|
|
|
. PhpInterface::SPACE . MethodsInterface::UPDATE]); |
133
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_PATCH, |
134
|
|
|
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName) |
135
|
|
|
. PhpInterface::SLASH . $id, $this->getJsonApiRequest()]); |
136
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_IS_JSON); |
137
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEE_RESP_CONTAINS_JSON, [$this->getJsonApiResponse(true)], false); |
138
|
|
|
$this->endMethod(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param MethodOptions $methodOpts |
143
|
|
|
*/ |
144
|
|
|
private function setDeleteContent(MethodOptions $methodOpts) : void |
145
|
|
|
{ |
146
|
|
|
$id = $this->getId(); |
147
|
|
|
if (empty($this->attributesState[ApiInterface::RAML_ID]) === false) { |
148
|
|
|
$id = $this->attributesState[ApiInterface::RAML_ID]; |
149
|
|
|
} |
150
|
|
|
$methodOpts->setName(TestsInterface::TRY . $this->generator->objectName . ucfirst(MethodsInterface::DELETE)); |
151
|
|
|
$this->startMethod($methodOpts); |
152
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::IM_GOING_TO, |
153
|
|
|
[TestsInterface::TEST_WORD . PhpInterface::SPACE . $this->generator->objectName |
154
|
|
|
. PhpInterface::SPACE . MethodsInterface::DELETE]); |
155
|
|
|
$this->methodCallOnObject(TestsInterface::PARAM_I, TestsInterface::SEND_DELETE, |
156
|
|
|
[PhpInterface::SLASH . $this->generator->version . PhpInterface::SLASH . mb_strtolower($this->generator->objectName) . PhpInterface::SLASH . $id]); |
157
|
|
|
$this->endMethod(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function getId() |
161
|
|
|
{ |
162
|
|
|
if (empty($this->attributesState[ApiInterface::RAML_ID]) === false) { |
163
|
|
|
return $this->attributesState[ApiInterface::RAML_ID]; |
164
|
|
|
} |
165
|
|
|
return 1; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param bool $required |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
private function getJsonApiRequest($required = false) : array |
173
|
|
|
{ |
174
|
|
|
$attrs = []; |
175
|
|
|
// set id if it's string |
176
|
|
|
if (empty($this->attributesState[ApiInterface::RAML_ID]) === false) { |
177
|
|
|
$attrs[ApiInterface::RAML_ID] = $this->attributesState[ApiInterface::RAML_ID]; |
178
|
|
|
} |
179
|
|
|
$props = $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS]; |
180
|
|
|
foreach ($this->attributesState as $attrKey => $attrVal) { |
181
|
|
|
if ($required === false || ($required === true && empty($props[$attrKey][ApiInterface::RAML_KEY_REQUIRED]) === false)) { |
182
|
|
|
$attrs[$attrKey] = $attrVal; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
return [ |
186
|
|
|
ApiInterface::RAML_DATA => [ |
187
|
|
|
ApiInterface::RAML_TYPE => mb_strtolower($this->generator->objectName), |
188
|
|
|
ApiInterface::RAML_ATTRS => $attrs, |
189
|
|
|
], |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param bool $required |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
private function getJsonApiResponse($required = false) : array |
198
|
|
|
{ |
199
|
|
|
$attrs = []; |
200
|
|
|
$props = $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS]; |
201
|
|
|
foreach ($this->attributesState as $attrKey => $attrVal) { |
202
|
|
|
if ($required === false || ($required === true && empty($props[$attrKey][ApiInterface::RAML_KEY_REQUIRED]) === false)) { |
203
|
|
|
$attrs[$attrKey] = $attrVal; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return [ |
207
|
|
|
ApiInterface::RAML_DATA => [ |
208
|
|
|
ApiInterface::RAML_TYPE => mb_strtolower($this->generator->objectName), |
209
|
|
|
ApiInterface::RAML_ID => $this->getId(), |
210
|
|
|
ApiInterface::RAML_ATTRS => $attrs, |
211
|
|
|
], |
212
|
|
|
]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
private function collectProps() |
216
|
|
|
{ |
217
|
|
|
$idObject = $this->generator->types[$this->generator->types[$this->generator->objectName][ApiInterface::RAML_PROPS][ApiInterface::RAML_ID]]; |
218
|
|
|
if ($idObject[ApiInterface::RAML_TYPE] === ApiInterface::RAML_TYPE_STRING) { |
219
|
|
|
$this->attributesState[ApiInterface::RAML_ID] = uniqid(); |
220
|
|
|
} |
221
|
|
|
$props = $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS]; |
222
|
|
|
foreach ($props as $attrKey => $attrVal) { |
223
|
|
|
$this->attributesState[$attrKey] = $this->getAttributeValue($attrVal); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param array $attrVal |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
private function getAttributeValue(array $attrVal) |
232
|
|
|
{ |
233
|
|
|
$faker = Factory::create(); |
234
|
|
|
$type = !empty($attrVal[ApiInterface::RAML_ENUM]) ? ApiInterface::RAML_ENUM : $attrVal[ApiInterface::RAML_TYPE]; |
235
|
|
|
switch ($type) { |
236
|
|
|
case ApiInterface::RAML_TYPE_NUMBER: |
237
|
|
|
case ApiInterface::RAML_TYPE_INTEGER: |
238
|
|
|
return $faker->randomDigit; |
239
|
|
|
case ApiInterface::RAML_TYPE_STRING: |
240
|
|
|
return $faker->userName; |
241
|
|
|
case ApiInterface::RAML_TYPE_BOOLEAN: |
242
|
|
|
return $faker->boolean; |
243
|
|
|
case ApiInterface::RAML_ENUM: |
244
|
|
|
return $attrVal[ApiInterface::RAML_ENUM][0]; |
245
|
|
|
case ApiInterface::RAML_DATE: |
246
|
|
|
return $faker->date(); |
247
|
|
|
case ApiInterface::RAML_TIME: |
248
|
|
|
return $faker->time(); |
249
|
|
|
case ApiInterface::RAML_DATETIME: |
250
|
|
|
return $faker->dateTime()->format('Y-m-d H:i:s'); |
251
|
|
|
} |
252
|
|
|
return $faker->name; |
253
|
|
|
} |
254
|
|
|
} |