Completed
Pull Request — 3.x (#475)
by Grégoire
29:02
created

PostControllerTest::getPoolMock()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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