NewThreadControllerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the MilioooMessageBundle package.
5
 *
6
 * (c) Michiel boeckaert <[email protected]>
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Miliooo\MessagingBundle\Tests\Controller;
12
13
use Miliooo\Messaging\User\ParticipantInterface;
14
use Miliooo\MessagingBundle\Controller\NewThreadController;
15
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
16
use Miliooo\Messaging\Helpers\FlashMessages\FlashMessageProviderInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Test file for Miliooo\MessagingBundle\Controller\NewThreadController
21
 *
22
 * @author Michiel Boeckaert <[email protected]>
23
 */
24
class NewThreadControllerTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * The class under test
28
     *
29
     * @var NewThreadController
30
     */
31
    private $controller;
32
33
    /**
34
     * @var \PHPUnit_Framework_MockObject_MockObject
35
     */
36
    private $formFactory;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject
40
     */
41
    private $formHandler;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject
45
     */
46
    private $participantProvider;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $templating;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private $flashMessageProvider;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject
60
     */
61
    private $router;
62
63
    /**
64
     * @var ParticipantInterface
65
     */
66
    private $loggedInUser;
67
68
    /**
69
     * @var \PHPUnit_Framework_MockObject_MockObject
70
     */
71
    private $form;
72
73
    public function setUp()
74
    {
75
        $this->formFactory = $this->getMockBuilder('Miliooo\Messaging\Form\FormFactory\NewThreadMessageFormFactory')
76
            ->disableOriginalConstructor()->getMock();
77
        $this->formHandler = $this->getMockBuilder('Miliooo\Messaging\Form\FormHandler\NewSingleThreadFormHandler')
78
            ->disableOriginalConstructor()->getMock();
79
        $this->participantProvider = $this->getMock('Miliooo\Messaging\User\ParticipantProviderInterface');
80
        $this->templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
81
        $this->flashMessageProvider = $this->getMock(
82
            'Miliooo\Messaging\Helpers\FlashMessages\FlashMessageProviderInterface'
83
        );
84
        $this->router = $this->getMock('Symfony\Component\Routing\RouterInterface');
85
        $this->loggedInUser = new ParticipantTestHelper('1');
86
        $this->form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
87
        $this->controller = new NewThreadController(
88
            $this->formFactory,
89
            $this->formHandler,
90
            $this->participantProvider,
91
            $this->templating,
92
            $this->flashMessageProvider,
93
            $this->router
94
        );
95
    }
96
97
    public function testCreateActionWithNonProcessedForm()
98
    {
99
        $this->expectsLoggedInUser();
100
        $this->expectsFormProcessingWillReturn(false);
101
        $this->expectsTwigRendering();
102
        $this->flashMessageProvider->expects($this->never())->method('addFlash');
103
        $this->controller->createAction(new Request());
104
    }
105
106
    public function testCreateActionWithProcessedForm()
107
    {
108
        $this->expectsLoggedInUser();
109
        $this->expectsFormProcessingWillReturn(true);
110
        $this->flashMessageProvider->expects($this->once())->method('addFlash')
111
            ->with(FlashMessageProviderInterface::TYPE_SUCCESS, 'flash.thread_created_success');
112
        $this->router->expects($this->once())
113
            ->method('generate')
114
            ->with('miliooo_message_thread_new')
115
            ->will($this->returnValue('http://www.test.com'));
116
117
        $this->controller->createAction(new Request());
118
    }
119
120
    protected function expectsLoggedInUser()
121
    {
122
        $this->participantProvider->expects($this->once())
123
            ->method('getAuthenticatedParticipant')->will($this->returnValue($this->loggedInUser));
124
    }
125
126
    /**
127
     * Expects Form processing and the process method will return the first parameter value.
128
     *
129
     * @param boolean $boolean
130
     */
131
    protected function expectsFormProcessingWillReturn($boolean)
132
    {
133
        $this->formFactory->expects($this->once())
134
            ->method('create')
135
            ->with($this->loggedInUser)
136
            ->will($this->returnValue($this->form));
137
138
        $this->formHandler->expects($this->once())->method('process')->with($this->form)
139
            ->will($this->returnValue($boolean));
140
    }
141
142
    protected function expectsTwigRendering()
143
    {
144
        $formView = $this->getMockBuilder('Symfony\Component\Form\FormView')->disableOriginalConstructor()->getMock();
145
        $this->form->expects($this->once())->method('createView')->will($this->returnValue($formView));
146
147
        $this->templating
148
            ->expects($this->once())
149
            ->method('renderResponse')
150
            ->with('MilioooMessagingBundle:NewThread:new_thread.html.twig', ['form' => $formView]);
151
    }
152
}
153