Completed
Push — 3.x ( 733455...aa9981 )
by Grégoire
02:00
created

PostControllerTest::testGetPostCommentsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Tests\Controller\Api;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\NewsBundle\Controller\Api\PostController;
16
use Sonata\NewsBundle\Model\CommentInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * @author Hugo Briand <[email protected]>
21
 */
22
class PostControllerTest extends TestCase
23
{
24
    public function testGetPostsAction()
25
    {
26
        $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface');
27
        $paramFetcher->expects($this->once())->method('all')->will($this->returnValue([]));
28
29
        $pager = $this->createMock('Sonata\DatagridBundle\Pager\PagerInterface');
30
31
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
32
        $postManager->expects($this->once())->method('getPager')->will($this->returnValue($pager));
33
34
        $this->assertSame($pager, $this->createPostController($postManager)->getPostsAction($paramFetcher));
35
    }
36
37
    public function testGetPostAction()
38
    {
39
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
40
41
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
42
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
43
44
        $this->assertEquals($post, $this->createPostController($postManager)->getPostAction(1));
45
    }
46
47
    public function testGetPostNotFoundExceptionAction()
48
    {
49
        $this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
50
        $this->expectExceptionMessage('Post (42) not found');
51
52
        $this->createPostController()->getPostAction(42);
53
    }
54
55
    public function testGetPostCommentsAction()
56
    {
57
        $parameters = [
58
            'page' => 2,
59
            'count' => 5,
60
        ];
61
62
        $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface');
63
        $paramFetcher->expects($this->once())->method('all')->will($this->returnValue([]));
64
        $paramFetcher->expects($this->exactly(2))->method('get')
65
            ->with($this->logicalOr($this->equalTo('page'), $this->equalTo('count')))
66
            ->will($this->returnCallback(function ($parameter) use ($parameters) {
67
                return $parameters[$parameter];
68
            }));
69
70
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
71
72
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
73
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
74
75
        $pager = $this->createMock('Sonata\DatagridBundle\Pager\PagerInterface');
76
77
        // Will assert that param fetcher parameters are used for the pager
78
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
79
        $commentManager->expects($this->once())
80
            ->method('getPager')
81
            ->with($this->anything(), $this->equalTo($parameters['page']), $this->equalTo($parameters['count']))
82
            ->will($this->returnValue($pager));
83
84
        $this->assertEquals($pager, $this->createPostController($postManager, $commentManager)->getPostCommentsAction(1, $paramFetcher));
85
    }
86
87
    public function testGetPostCommentsActionNotFoundExceptionAction()
88
    {
89
        $this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
90
        $this->expectExceptionMessage('Post (42) not found');
91
92
        $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface');
93
94
        $this->createPostController()->getPostCommentsAction(42, $paramFetcher);
95
    }
96
97
    public function testPostPostAction()
98
    {
99
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
100
        $post->expects($this->once())->method('setContent');
101
102
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
103
        $postManager->expects($this->once())->method('save')->will($this->returnValue($post));
104
105
        $formatterPool = $this->createMock('Sonata\FormatterBundle\Formatter\Pool');
106
        $formatterPool->expects($this->once())->method('transform')->will($this->returnValue($post->getContent()));
107
108
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
109
        $form->expects($this->once())->method('handleRequest');
110
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
111
        $form->expects($this->once())->method('getData')->will($this->returnValue($post));
112
113
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
114
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
115
116
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->postPostAction(new Request());
117
118
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
119
    }
120
121
    public function testPostPostInvalidAction()
122
    {
123
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
124
        $post->expects($this->never())->method('setContent');
125
126
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
127
        $postManager->expects($this->never())->method('save')->will($this->returnValue($post));
128
129
        $formatterPool = $this->createMock('Sonata\FormatterBundle\Formatter\Pool');
130
        $formatterPool->expects($this->never())->method('transform')->will($this->returnValue($post->getContent()));
131
132
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
133
        $form->expects($this->once())->method('handleRequest');
134
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
135
136
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
137
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
138
139
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->postPostAction(new Request());
140
141
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
142
    }
143
144
    public function testPutPostAction()
145
    {
146
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
147
        $post->expects($this->once())->method('setContent');
148
149
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
150
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
151
        $postManager->expects($this->once())->method('save')->will($this->returnValue($post));
152
153
        $formatterPool = $this->createMock('Sonata\FormatterBundle\Formatter\Pool');
154
        $formatterPool->expects($this->once())->method('transform')->will($this->returnValue($post->getContent()));
155
156
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
157
        $form->expects($this->once())->method('handleRequest');
158
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
159
        $form->expects($this->once())->method('getData')->will($this->returnValue($post));
160
161
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
162
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
163
164
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->putPostAction(1, new Request());
165
166
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
167
    }
168
169
    public function testPutPostInvalidAction()
170
    {
171
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
172
        $post->expects($this->never())->method('setContent');
173
174
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
175
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
176
        $postManager->expects($this->never())->method('save')->will($this->returnValue($post));
177
178
        $formatterPool = $this->createMock('Sonata\FormatterBundle\Formatter\Pool');
179
        $formatterPool->expects($this->never())->method('transform')->will($this->returnValue($post->getContent()));
180
181
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
182
        $form->expects($this->once())->method('handleRequest');
183
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
184
185
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
186
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
187
188
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->putPostAction(1, new Request());
189
190
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
191
    }
192
193
    public function testDeletePostAction()
194
    {
195
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
196
197
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
198
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
199
        $postManager->expects($this->once())->method('delete');
200
201
        $view = $this->createPostController($postManager)->deletePostAction(1);
202
203
        $this->assertEquals(['deleted' => true], $view);
204
    }
205
206
    public function testDeletePostInvalidAction()
207
    {
208
        $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
209
210
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
211
        $postManager->expects($this->once())->method('find')->will($this->returnValue(null));
212
        $postManager->expects($this->never())->method('delete');
213
214
        $this->createPostController($postManager)->deletePostAction(1);
215
    }
216
217
    public function testPostPostCommentsAction()
218
    {
219
        $comment = $this->createMock(CommentInterface::class);
220
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
221
        $post->expects($this->once())->method('isCommentable')->will($this->returnValue(true));
222
223
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
224
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
225
226
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
227
        $commentManager->expects($this->once())->method('save');
228
        $commentManager->expects($this->once())->method('create')->will($this->returnValue($comment));
229
230
        $mailer = $this->createMock('Sonata\NewsBundle\Mailer\MailerInterface');
231
        $mailer->expects($this->once())->method('sendCommentNotification');
232
233
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
234
        $form->expects($this->once())->method('handleRequest');
235
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
236
        $form->expects($this->once())->method('getData')->will($this->returnValue($comment));
237
238
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
239
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
240
241
        $postController = $this->createPostController($postManager, $commentManager, $mailer, $formFactory);
242
243
        $postController->postPostCommentsAction(1, new Request());
244
245
        $this->assertInstanceOf(CommentInterface::class, $comment);
246
    }
247
248
    public function testPostPostCommentsInvalidFormAction()
249
    {
250
        $comment = $this->createMock(CommentInterface::class);
251
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
252
        $post->expects($this->once())->method('isCommentable')->will($this->returnValue(true));
253
254
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
255
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
256
257
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
258
        $commentManager->expects($this->once())->method('create')->will($this->returnValue($comment));
259
260
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
261
        $form->expects($this->once())->method('handleRequest');
262
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
263
264
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
265
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
266
267
        $this->assertInstanceOf('Symfony\Component\Form\Form', $this->createPostController($postManager, $commentManager, null, $formFactory)->postPostCommentsAction(1, new Request()));
268
    }
269
270
    public function testPostPostCommentsNotCommentableAction()
271
    {
272
        $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
273
        $this->expectExceptionMessage('Post (42) not commentable');
274
275
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
276
        $post->expects($this->once())->method('isCommentable')->will($this->returnValue(false));
277
278
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
279
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
280
281
        $this->createPostController($postManager)->postPostCommentsAction(42, new Request());
282
    }
283
284
    public function testPutPostCommentAction()
285
    {
286
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
287
        $post->expects($this->once())->method('isCommentable')->will($this->returnValue(true));
288
289
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
290
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
291
292
        $comment = $this->createMock(CommentInterface::class);
293
294
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
295
        $commentManager->expects($this->once())->method('find')->will($this->returnValue($comment));
296
        $commentManager->expects($this->once())->method('save');
297
298
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
299
        $form->expects($this->once())->method('handleRequest');
300
        $form->expects($this->once())->method('getData')->will($this->returnValue($comment));
301
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
302
303
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
304
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
305
306
        $postController = $this->createPostController($postManager, $commentManager, null, $formFactory);
307
308
        $comment = $postController->putPostCommentsAction(1, 1, new Request());
309
310
        $this->assertInstanceOf(CommentInterface::class, $comment);
311
    }
312
313
    public function testPutPostCommentInvalidAction()
314
    {
315
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
316
        $post->expects($this->once())->method('isCommentable')->will($this->returnValue(true));
317
318
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
319
        $postManager->expects($this->once())->method('find')->will($this->returnValue($post));
320
321
        $comment = $this->createMock(CommentInterface::class);
322
323
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
324
        $commentManager->expects($this->once())->method('find')->will($this->returnValue($comment));
325
        $commentManager->expects($this->never())->method('save');
326
327
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
328
        $form->expects($this->once())->method('handleRequest');
329
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
330
331
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
332
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
333
334
        $view = $this->createPostController($postManager, $commentManager, null, $formFactory)->putPostCommentsAction(1, 1, new Request());
335
336
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
337
    }
338
339
    /**
340
     * @param null $postManager
341
     * @param null $commentManager
342
     * @param null $mailer
343
     * @param null $formFactory
344
     * @param null $formatterPool
345
     *
346
     * @return PostController
347
     */
348
    protected function createPostController($postManager = null, $commentManager = null, $mailer = null, $formFactory = null, $formatterPool = null)
349
    {
350
        if (null === $postManager) {
351
            $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
352
        }
353
        if (null === $commentManager) {
354
            $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
355
        }
356
        if (null === $mailer) {
357
            $mailer = $this->createMock('Sonata\NewsBundle\Mailer\MailerInterface');
358
        }
359
        if (null === $formFactory) {
360
            $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
361
        }
362
        if (null === $formatterPool) {
363
            $formatterPool = $this->createMock('Sonata\FormatterBundle\Formatter\Pool');
364
        }
365
366
        return new PostController($postManager, $commentManager, $mailer, $formFactory, $formatterPool);
367
    }
368
}
369