Completed
Push — master ( 57a1ef...d5f3e2 )
by Ivo
02:52
created

ContentControllerTest::getMultipleErrorResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Tests\Controller;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Controller\ContentController;
15
use Ivoaz\Bundle\ContentEditableBundle\Entity\Content;
16
use Ivoaz\Bundle\ContentEditableBundle\Form\Model\Batch;
17
use Ivoaz\Bundle\ContentEditableBundle\Form\Model\BatchContent;
18
use Ivoaz\Bundle\ContentEditableBundle\Form\Model\Content as FormContent;
19
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManagerInterface;
20
use Symfony\Component\Form\FormError;
21
use Symfony\Component\Form\FormFactoryInterface;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
26
use Symfony\Component\Validator\ConstraintViolation;
27
28
class ContentControllerTest extends \PHPUnit_Framework_TestCase
29
{
30
    /**
31
     * @var ContentManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $manager;
34
35
    /**
36
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $formFactory;
39
40
    /**
41
     * @var FormInterface|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $form;
44
45
    /**
46
     * @var AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $authorizationChecker;
49
50
    /**
51
     * @var ContentController
52
     */
53
    private $controller;
54
55
    public function setUp()
56
    {
57
        $this->manager = $this->getMock(ContentManagerInterface::class);
58
        $this->form = $this->getMock(FormInterface::class);
59
        $this->formFactory = $this->getMock(FormFactoryInterface::class);
60
        $this->formFactory->method('create')
61
            ->willReturn($this->form);
62
        $this->authorizationChecker = $this->getMock(AuthorizationCheckerInterface::class);
63
64
        $this->controller = new ContentController($this->manager, $this->formFactory, $this->authorizationChecker);
65
    }
66
67
    public function testUpdateActionReturnsErrorWhenContentNotFound()
68
    {
69
        $this->setAuthorized(true);
70
71
        $this->manager->method('find')
72
            ->with(1)
73
            ->willReturn(null);
74
75
        $request = new Request([], [], ['id' => 1]);
76
        $response = $this->controller->updateAction($request);
77
78
        $expectedResponse = new JsonResponse(
79
            ['errors' => [['title' => sprintf('Content with id "1" was not found.')]]],
80
            400
81
        );
82
83
        $this->assertEquals($expectedResponse, $response);
84
    }
85
86
    public function testUpdateActionReturnsFormErrors()
87
    {
88
        $this->setAuthorized(true);
89
90
        $content = new Content();
91
92
        $this->manager->method('find')
93
            ->with(1)
94
            ->willReturn($content);
95
96
        $this->form->method('isValid')
97
            ->willReturn(false);
98
99
        $this->form->method('getErrors')
100
            ->with(true, true)
101
            ->willReturn($this->getMultipleErrors());
102
103
        $request = new Request([], [], ['id' => 1]);
104
        $response = $this->controller->updateAction($request);
105
106
        $this->assertEquals($this->getMultipleErrorResponse(), $response);
107
    }
108
109
    public function testUpdateActionUpdatesContent()
110
    {
111
        $this->setAuthorized(true);
112
113
        $content = new Content();
114
115
        $this->manager->method('find')
116
            ->with(1)
117
            ->willReturn($content);
118
119
        $this->form->method('isValid')
120
            ->willReturn(true);
121
122
        $this->form->method('getData')
123
            ->willReturn(new FormContent());
124
125
        $this->manager->expects($this->once())
126
            ->method('update')
127
            ->with($content);
128
129
        $request = new Request([], [], ['id' => 1]);
130
        $response = $this->controller->updateAction($request);
131
132
        $expectedResponse = new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
133
134
        $this->assertEquals($expectedResponse, $response);
135
    }
136
137
    public function testBatchUpdateActionReturnsErrorWhenContentNotFound()
138
    {
139
        $this->setAuthorized(true);
140
141
        $this->form->method('isValid')
142
            ->willReturn(true);
143
144
        $this->form->method('getData')
145
            ->willReturn($this->getBatch());
146
147
        $this->manager->method('find')
148
            ->willReturn(null);
149
150
        $response = $this->controller->batchUpdateAction(new Request());
151
152
        $expectedResponse = new JsonResponse(
153
            [
154
                'errors' => [
155
                    ['title' => 'Content with id "1" was not found.'],
156
                    ['title' => 'Content with id "2" was not found.'],
157
                ],
158
            ],
159
            400
160
        );
161
162
        $this->assertEquals($expectedResponse, $response);
163
    }
164
165
    public function testBatchUpdateActionReturnsFormErrors()
166
    {
167
        $this->setAuthorized(true);
168
169
        $this->form->method('isValid')
170
            ->willReturn(false);
171
172
        $this->form->method('getErrors')
173
            ->with(true, true)
174
            ->willReturn($this->getMultipleErrors());
175
176
        $response = $this->controller->batchUpdateAction(new Request());
177
178
        $this->assertEquals($this->getMultipleErrorResponse(), $response);
179
    }
180
181
    public function testBatchUpdateActionUpdatesContent()
182
    {
183
        $this->setAuthorized(true);
184
185
        $content1 = new Content();
186
        $content2 = new Content();
187
188
        $this->manager->method('find')
189
            ->will($this->returnValueMap([[1, $content1], [2, $content2]]));
190
191
        $this->form->method('isValid')
192
            ->willReturn(true);
193
194
        $this->form->method('getData')
195
            ->willReturn($this->getBatch());
196
197
        $this->manager->expects($this->exactly(2))
198
            ->method('update')
199
            ->withConsecutive([$content1], [$content2]);
200
201
        $response = $this->controller->batchUpdateAction(new Request());
202
203
        $expectedResponse = new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
204
205
        $this->assertEquals($expectedResponse, $response);
206
    }
207
208
    /**
209
     * @dataProvider getActionIsForbiddenTestData
210
     *
211
     * @param string $method
212
     */
213
    public function testActionIsForbidden($method)
214
    {
215
        $this->setAuthorized(false);
216
217
        $response = call_user_func([$this->controller, $method], new Request());
218
        $expectedResponse = new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN);
219
220
        $this->assertEquals($expectedResponse, $response, sprintf('Action "%s" should be forbidden.', $method));
221
    }
222
223
    /**
224
     * @return array
225
     */
226
    public function getActionIsForbiddenTestData()
227
    {
228
        return [['updateAction'], ['batchUpdateAction']];
229
    }
230
231
    /**
232
     * @param bool $authorized
233
     */
234
    private function setAuthorized($authorized)
235
    {
236
        $this->authorizationChecker->method('isGranted')
237
            ->with('ROLE_ADMIN')
238
            ->willReturn($authorized);
239
    }
240
241
    /**
242
     * @return Batch
243
     */
244
    private function getBatch()
245
    {
246
        $batch = new Batch();
247
        $batch->contents[] = new BatchContent();
248
        $batch->contents[] = new BatchContent();
249
        $batch->contents[0]->id = 1;
250
        $batch->contents[1]->id = 2;
251
        $batch->contents[0]->text = 'Text 1';
252
        $batch->contents[1]->text = 'Text 2';
253
254
        return $batch;
255
    }
256
257
    /**
258
     * @return array
259
     */
260
    private function getMultipleErrors()
261
    {
262
        $errors = [
263
            new FormError(
264
                'Test error1',
265
                null,
266
                [],
267
                null,
268
                new ConstraintViolation('', '', [], null, 'field1', null)
269
            ),
270
            new FormError(
271
                'Test error2',
272
                null,
273
                [],
274
                null,
275
                new ConstraintViolation('', '', [], null, 'field2', null)
276
            ),
277
        ];
278
279
        return $errors;
280
    }
281
282
    /**
283
     * @return JsonResponse
284
     */
285
    private function getMultipleErrorResponse()
286
    {
287
        $response = new JsonResponse(
288
            [
289
                'errors' => [
290
                    ['field' => 'field1', 'title' => 'Test error1'],
291
                    ['field' => 'field2', 'title' => 'Test error2'],
292
                ],
293
            ],
294
            400
295
        );
296
297
        return $response;
298
    }
299
}
300