Completed
Push — 3.x-dev-kit ( cc4dbe )
by
unknown
03:15
created

testPostMessageInvalidAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 1
eloc 11
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\NotificationBundle\Tests\Controller\Api;
13
14
use Sonata\NotificationBundle\Controller\Api\MessageController;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Class MessageControllerTest.
19
 *
20
 *
21
 * @author Hugo Briand <[email protected]>
22
 */
23
class MessageControllerTest extends \PHPUnit_Framework_TestCase
24
{
25
    public function testGetMessagesAction()
26
    {
27
        $messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface');
28
        $messageManager->expects($this->once())->method('getPager')->will($this->returnValue(array()));
29
30
        $paramFetcher = $this->getMock('FOS\RestBundle\Request\ParamFetcherInterface');
31
        $paramFetcher->expects($this->exactly(3))->method('get');
32
        $paramFetcher->expects($this->once())->method('all')->will($this->returnValue(array()));
33
34
        $this->assertEquals(array(), $this->createMessageController(null, $messageManager)->getMessagesAction($paramFetcher));
35
    }
36
37
    public function testPostMessageAction()
38
    {
39
        $message = $this->getMock('Sonata\NotificationBundle\Model\MessageInterface');
40
41
        $messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface');
42
        $messageManager->expects($this->once())->method('save')->will($this->returnValue($message));
43
44
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
45
        $form->expects($this->once())->method('handleRequest');
46
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
47
        $form->expects($this->once())->method('getData')->will($this->returnValue($message));
48
49
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
50
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
51
52
        $view = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request());
53
54
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
55
    }
56
57
    public function testPostMessageInvalidAction()
58
    {
59
        $message = $this->getMock('Sonata\NotificationBundle\Model\MessageInterface');
60
61
        $messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface');
62
        $messageManager->expects($this->never())->method('save')->will($this->returnValue($message));
63
64
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
65
        $form->expects($this->once())->method('handleRequest');
66
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
67
68
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
69
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
70
71
        $view = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request());
72
73
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
74
    }
75
76
    /**
77
     * @param $message
78
     * @param $messageManager
79
     * @param $formFactory
80
     *
81
     * @return MessageController
82
     */
83
    public function createMessageController($message = null, $messageManager = null, $formFactory = null)
84
    {
85
        if (null === $messageManager) {
86
            $messageManager = $this->getMock('Sonata\PageBundle\Model\SiteManagerInterface');
87
        }
88
        if (null !== $message) {
89
            $messageManager->expects($this->once())->method('findOneBy')->will($this->returnValue($message));
90
        }
91
        if (null === $formFactory) {
92
            $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
93
        }
94
95
        return new MessageController($messageManager, $formFactory);
96
    }
97
}
98