UserControllerTest::testDeleteAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Tests\Controller;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
class UserControllerTest extends AbstractWebTestCase
18
{
19
    /**
20
     * @var int
21
     */
22
    protected static $entityId;
23
24
    protected static $email;
25
26
    public function testCreateAction()
27
    {
28
        self::$email = $this->faker->email;
29
30
        $this->client->request(
31
            Request::METHOD_POST,
32
            '/users',
33
            [],
34
            [],
35
            [],
36
            json_encode([
37
                'fullName' => 'Test User',
38
                'email' => self::$email,
39
                'plainPassword' => 'test',
40
            ])
41
        );
42
43
        $this->assertEquals(Response::HTTP_CREATED, $this->client->getResponse()->getStatusCode());
44
45
        $this->assertTrue(
46
            $this->client->getResponse()->headers->contains(
47
                'Content-Type',
48
                'application/json'
49
            )
50
        );
51
52
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
53
54
        $this->assertArrayHasKey('id', $responseContent);
55
56
        self::$entityId = $responseContent['id'];
57
    }
58
59
    public function testBadRequestCreateAction()
60
    {
61
        $this->client->request(
62
            Request::METHOD_POST,
63
            '/users',
64
            [],
65
            [],
66
            [],
67
            json_encode([
68
                'fullName' => 'Test User',
69
                'email' => self::$email,
70
                'plainPassword' => 'test',
71
            ])
72
        );
73
74
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $this->client->getResponse()->getStatusCode());
75
76
        $this->assertTrue(
77
            $this->client->getResponse()->headers->contains(
78
                'Content-Type',
79
                'application/json'
80
            )
81
        );
82
    }
83
84
    public function testUnauthorizedUpdateAction()
85
    {
86
        self::$email = $this->faker->email;
87
88
        $this->client->request(
89
            Request::METHOD_PATCH,
90
            sprintf('/users/%d', self::$entityId),
91
            [],
92
            [],
93
            [],
94
            json_encode([
95
                'email' => self::$email,
96
            ])
97
        );
98
99
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
100
101
        $this->assertTrue(
102
            $this->client->getResponse()->headers->contains(
103
                'Content-Type',
104
                'application/json'
105
            )
106
        );
107
    }
108
109
    public function testUpdateAction()
110
    {
111
        $this->client->request(
112
            Request::METHOD_PATCH,
113
            sprintf('/users/%d', self::$entityId),
114
            [],
115
            [],
116
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
117
            json_encode([
118
                'email' => self::$email,
119
            ])
120
        );
121
122
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
123
124
        $this->assertTrue(
125
            $this->client->getResponse()->headers->contains(
126
                'Content-Type',
127
                'application/json'
128
            )
129
        );
130
131
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
132
133
        $this->assertSame('email', array_search(self::$email, $responseContent));
134
    }
135
136
    public function testNotFoundUpdateAction()
137
    {
138
        $this->client->request(
139
            Request::METHOD_PATCH,
140
            '/users/0',
141
            [],
142
            [],
143
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token],
144
            json_encode([
145
                'email' => self::$email,
146
            ])
147
        );
148
149
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
150
151
        $this->assertTrue(
152
            $this->client->getResponse()->headers->contains(
153
                'Content-Type',
154
                'application/json'
155
            )
156
        );
157
    }
158
159
    public function testListAction()
160
    {
161
        $this->client->request(Request::METHOD_GET, '/users');
162
163
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
164
165
        $this->assertTrue(
166
            $this->client->getResponse()->headers->contains(
167
                'Content-Type',
168
                'application/json'
169
            )
170
        );
171
172
        $this->assertContains('users', $this->client->getResponse()->getContent());
173
    }
174
175
    public function testFilterListAction()
176
    {
177
        $this->client->request(Request::METHOD_GET, sprintf('/users?user_filter[email]=%s', self::$email));
178
179
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
180
181
        $this->assertTrue(
182
            $this->client->getResponse()->headers->contains(
183
                'Content-Type',
184
                'application/json'
185
            )
186
        );
187
188
        $this->assertContains('users', $this->client->getResponse()->getContent());
189
190
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
191
192
        $this->assertSame('email', array_search(self::$email, $responseContent['users'][0]));
193
    }
194
195
    public function testShowAction()
196
    {
197
        $this->client->request(Request::METHOD_GET, sprintf('/users/%d', self::$entityId));
198
199
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
200
201
        $this->assertTrue(
202
            $this->client->getResponse()->headers->contains(
203
                'Content-Type',
204
                'application/json'
205
            )
206
        );
207
208
        $responseContent = json_decode($this->client->getResponse()->getContent(), true);
209
210
        $this->assertSame('id', array_search(self::$entityId, $responseContent));
211
    }
212
213
    public function testNotFoundShowAction()
214
    {
215
        $this->client->request(Request::METHOD_GET, '/users/0');
216
217
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
218
219
        $this->assertTrue(
220
            $this->client->getResponse()->headers->contains(
221
                'Content-Type',
222
                'application/json'
223
            )
224
        );
225
    }
226
227
    public function testUnauthorizedDeleteAction()
228
    {
229
        $this->client->request(Request::METHOD_DELETE, sprintf('/users/%d', self::$entityId));
230
231
        $this->assertEquals(Response::HTTP_UNAUTHORIZED, $this->client->getResponse()->getStatusCode());
232
233
        $this->assertTrue(
234
            $this->client->getResponse()->headers->contains(
235
                'Content-Type',
236
                'application/json'
237
            )
238
        );
239
    }
240
241
    public function testDeleteAction()
242
    {
243
        $this->client->request(
244
            Request::METHOD_DELETE,
245
            sprintf('/users/%d', self::$entityId),
246
            [],
247
            [],
248
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token]
249
        );
250
251
        $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
252
253
        $this->assertTrue(
254
            $this->client->getResponse()->headers->contains(
255
                'Content-Type',
256
                'application/json'
257
            )
258
        );
259
    }
260
261
    public function testNotFoundDeleteAction()
262
    {
263
        $this->client->request(
264
            Request::METHOD_DELETE,
265
            '/users/0',
266
            [],
267
            [],
268
            ['HTTP_AUTHORIZATION' => 'Bearer '.$this->token]
269
        );
270
271
        $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode());
272
273
        $this->assertTrue(
274
            $this->client->getResponse()->headers->contains(
275
                'Content-Type',
276
                'application/json'
277
            )
278
        );
279
    }
280
}
281