Completed
Push — 3.x-dev-kit ( 21be5a )
by
unknown
03:10
created

PostControllerTest::testGetPostCommentsAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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