Passed
Push — master ( 90aec0...26fb73 )
by Arthur
03:07
created

src/Blocks/Tests.php (2 issues)

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