Passed
Pull Request — master (#3)
by Flavien
03:30
created

WebTestCase::checkAddOrUpdateOrPatchAnElement()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 12
nop 3
crap 5
1
<?php
2
3
namespace QualityCode\ApiFeaturesBundle\Tests\Functionnal;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
6
use Symfony\Bundle\FrameworkBundle\Client;
7
use Faker\Factory as FakerFactory;
8
9
class WebTestCase extends BaseWebTestCase
10
{
11
    protected $faker;
12
13
    /**
14
     * @var array
15
     */
16
    protected $fieldsList;
17
18
    /**
19
     * @var array
20
     */
21
    protected $links = [
22
        'self', 'create', 'update', 'patch', 'remove', 'list',
23
    ];
24
25
    /**
26
     * @var array
27
     */
28
    protected $itemValues;
29
30
    /**
31
     * @var array
32
     */
33
    protected $itemsBadValues;
34
35
    /**
36
     * @var string
37
     */
38
    protected $route;
39
40 13
    protected function setUp()
41
    {
42 13
        $this->faker = FakerFactory::create();
43 13
    }
44
45
    /**
46
     * @param string $route
47
     * @param int    $expectedItems
48
     */
49 1 View Code Duplication
    protected function checkGetAll(string $route, int $expectedItems)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 1
        $client = $this->getClient('GET', $route);
52 1
        $this->checkStatusCodeAndContentType($client, 200);
53
54 1
        $items = json_decode($client->getResponse()->getContent());
55
56 1
        $this->checkIfListHaveRightStructure($items);
57
58 1
        $this->checkItemList($items, $expectedItems);
59 1
    }
60
61
    /**
62
     * @param string $route
63
     */
64 1
    protected function checkGetAnElement(string $route)
65
    {
66 1
        $client = $this->getClient('GET', $route);
67 1
        $this->checkStatusCodeAndContentType($client, 200);
68 1
        $item = json_decode($client->getResponse()->getContent());
69
70 1
        $this->checkIfItemHasTheRightFieldsNumber((array) $item);
71 1
        $this->checkIfItemHasFields((array) $item);
72 1
        $this->checkIfItemHasLinks((array) $item);
73 1
    }
74
75
    /**
76
     * @param string $route
77
     */
78 1 View Code Duplication
    protected function checkGetAUnexistingElement(string $route)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 1
        $client = $this->getClient('GET', $route);
81 1
        $this->checkStatusCodeAndContentType($client, '404');
82 1
        $adresse = json_decode($client->getResponse()->getContent());
83
84 1
        $this->assertArrayHasKey('message', (array) $adresse);
85 1
        $this->assertEquals('Element not found', $adresse->message);
86 1
    }
87
88
    /**
89
     * @param string $route
90
     * @param string $method
91
     */
92 2
    protected function checkUpdateOrPatchAUnexistingElement(string $route, string $method)
93
    {
94 2
        $client = static::createClient();
95 2
        $client->request(
96 2
                $method, $route, array(), array(), array('CONTENT_TYPE' => 'application/json'), json_encode($this->itemValues)
97
        );
98
99 2
        $this->checkStatusCodeAndContentType($client, 404);
100 2
        $item = json_decode($client->getResponse()->getContent());
101 2
        $this->assertArrayHasKey('message', (array) $item);
102 2
        $this->assertEquals('Element not found', $item->message);
103 2
    }
104
105
    /**
106
     * @param string $route
107
     * @param bool   $mustHaveErrors
108
     * @param string $method
109
     */
110 6
    protected function checkAddOrUpdateOrPatchAnElement(string $route, bool $mustHaveErrors = false, string $method = 'POST')
111
    {
112 6
        $values = $this->itemValues;
113 6
        $statusCode = $method === 'POST' ? 201 : 200;
114 6
        if ($mustHaveErrors) {
115 3
            $values = $this->itemsBadValues;
116 3
            $statusCode = 400;
117
        }
118
119 6
        $client = $client = static::createClient();
120 6
        $client->request(
121 6
                $method, $route, array(), array(), array('CONTENT_TYPE' => 'application/json'), json_encode($values)
122
        );
123 6
        $this->checkStatusCodeAndContentType($client, $statusCode);
124 6
        $item = json_decode($client->getResponse()->getContent(), true);
125
126 6
        if (!$mustHaveErrors) {
127 3
            $this->checkIfItemHasTheRightFieldsNumber((array) $item);
128 3
            $this->checkIfItemHasFields((array) $item);
129 3
            $this->checkIfItemHasLinks((array) $item);
130 3
            foreach ($values as $key => $value) {
131 3
                $this->assertEquals($value, $item[$key]);
132
            }
133
        } else {
134 3
            $this->assertEquals(3, sizeof((array) $item));
135 3
            $this->assertArrayHasKey('code', (array) $item);
136 3
            $this->assertArrayHasKey('message', (array) $item);
137 3
            $this->assertArrayHasKey('errors', (array) $item);
138
        }
139 6
    }
140
141
    /**
142
     * @param Client $client
143
     * @param int    $statusCode
144
     */
145 11
    protected function checkStatusCodeAndContentType(Client $client, int $statusCode)
146
    {
147 11
        $this->assertEquals($statusCode, $client->getResponse()->getStatusCode());
148 11
        $this->assertTrue(
149 11
                $client->getResponse()->headers->contains(
150 11
                        'Content-Type', 'application/json'
151 11
                ), 'the "Content-Type" header is "application/json"'
152
        );
153 11
    }
154
155
    /**
156
     * @param string $route
157
     */
158 2
    protected function checkDeleteAnElement(string $route)
159
    {
160 2
        $client = static::createClient();
161
162 2
        $client->request('DELETE', $route);
163 2
        $this->assertEquals(204, $client->getResponse()->getStatusCode());
164 2
    }
165
166
    /**
167
     * @param string $method
168
     * @param string $route
169
     *
170
     * @return Client
171
     */
172 3
    protected function getClient(string $method, string $route)
173
    {
174 3
        $client = static::createClient();
175
176 3
        $client->request($method, $route);
177
178 3
        return $client;
179
    }
180
181
    /**
182
     * @param array $item
183
     */
184 5
    protected function checkIfItemHasFields(array $item)
185
    {
186 5
        foreach ($this->fieldsList as $fieldName) {
187 5
            $this->assertArrayHasKey($fieldName, (array) $item);
188
        }
189 5
    }
190
191
    /**
192
     * @param array $item
193
     */
194 5
    protected function checkIfItemHasLinks(array $item)
195
    {
196 5
        $this->assertArrayHasKey('_links', (array) $item);
197 5
        foreach ($this->links as $fieldName) {
198 5
            $this->assertArrayHasKey($fieldName, (array) $item['_links']);
199
        }
200 5
    }
201
202
    /**
203
     * @param array $item
204
     */
205 5
    protected function checkIfItemHasTheRightFieldsNumber(array $item)
206
    {
207 5
        $this->assertEquals(sizeof($this->fieldsList) + 1, sizeof((array) $item));
208 5
    }
209
210
    /**
211
     * @param \stdClass $list
212
     */
213 1
    protected function checkIfListHaveRightStructure(\stdClass $list)
214
    {
215 1
        $this->assertEquals(1, sizeof($list));
216 1
        $this->assertObjectHasAttribute('page', $list);
217 1
        $this->assertObjectHasAttribute('pages', $list);
218 1
        $this->assertObjectHasAttribute('limit', $list);
219 1
        $this->assertObjectHasAttribute('total', $list);
220 1
        $this->assertObjectHasAttribute('_links', $list);
221 1
        $this->assertObjectHasAttribute('self', $list->_links);
222 1
        $this->assertObjectHasAttribute('first', $list->_links);
223 1
        $this->assertObjectHasAttribute('last', $list->_links);
224 1
        $this->assertObjectHasAttribute('_embedded', $list);
225 1
        $this->assertObjectHasAttribute('items', $list->_embedded);
226 1
    }
227
228
    /**
229
     * @param \stdClass $items
230
     * @param int       $expectedSize
231
     */
232 1
    protected function checkItemList(\stdClass $items, int $expectedSize)
233
    {
234 1
        $this->assertEquals($expectedSize, sizeof($items->_embedded->items));
235
236 1
        foreach ($items->_embedded->items as $adresse) {
237 1
            $this->checkIfItemHasTheRightFieldsNumber((array) $adresse);
238 1
            $this->checkIfItemHasFields((array) $adresse);
239 1
            $this->checkIfItemHasLinks((array) $adresse);
240
        }
241 1
    }
242
}
243