ShowThreadControllerTest::setConstructorMocks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 14
rs 9.4285
c 2
b 0
f 1
cc 1
eloc 10
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\MessagingBundle\Controller\ShowThreadController;
14
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
15
use Miliooo\Messaging\User\ParticipantInterface;
16
use Miliooo\Messaging\ValueObjects\ReadStatus;
17
use Miliooo\Messaging\Model\MessageMetaInterface;
18
19
/**
20
 * Test file for Miliooo\MessagingBundle\Controller\ShowThreadController
21
 *
22
 * @author Michiel Boeckaert <[email protected]>
23
 */
24
class ShowThreadControllerTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * The class under test
28
     * @var ShowThreadController
29
     */
30
    private $controller;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $participantProvider;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject
39
     */
40
    private $templating;
41
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject
45
     */
46
    private $threadProvider;
47
48
    /**
49
     * @var ParticipantInterface
50
     */
51
    private $loggedInUser;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private $thread;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject
60
     */
61
    private $formFactory;
62
63
    /**
64
     * @var \PHPUnit_Framework_MockObject_MockObject
65
     */
66
    private $formHandler;
67
68
    /**
69
     * @var \PHPUnit_Framework_MockObject_MockObject
70
     */
71
    private $form;
72
73
    /**
74
     * @var \PHPUnit_Framework_MockObject_MockObject
75
     */
76
    private $formView;
77
78
    /**
79
     * @var \PHPUnit_Framework_MockObject_MockObject
80
     */
81
    private $readStatusManager;
82
83
    /**
84
     * @var \PHPUnit_Framework_MockObject_MockObject
85
     */
86
    private $message;
87
88
    /**
89
     * @var \PHPUnit_Framework_MockObject_MockObject
90
     */
91
    private $arrayCollection;
92
93
    /**
94
     * @var \PHPUnit_Framework_MockObject_MockObject
95
     */
96
    private $router;
97
98
99
    public function setUp()
100
    {
101
        $this->setConstructorMocks();
102
        $this->controller = new ShowThreadController(
103
            $this->formFactory,
104
            $this->formHandler,
105
            $this->threadProvider,
106
            $this->templating,
107
            $this->participantProvider,
108
            $this->readStatusManager,
109
            $this->router
110
        );
111
112
        $this->loggedInUser = new ParticipantTestHelper(1);
113
        $this->thread = $this->getMock('Miliooo\Messaging\Model\ThreadInterface');
114
        $this->form = $this->getMockBuilder('Symfony\Component\Form\Form')
115
                ->disableOriginalConstructor()->getMock();
116
        $this->formView = $this->getMockBuilder('Symfony\Component\Form\FormView')
117
                ->disableOriginalConstructor()->getMock();
118
119
        $this->message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
120
        $this->arrayCollection = $this->getMock('Doctrine\Common\Collections\ArrayCollection');
121
    }
122
123
    /**
124
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
125
     * @expectedExceptionMessage Thread not found
126
     */
127
    public function testShowActionThreadNotFoundThrowsException()
128
    {
129
        $this->expectsUser();
130
        $this->threadProvider->expects($this->once())
131
            ->method('findThreadForParticipant')
132
            ->with($this->loggedInUser, 1)
133
            ->will($this->returnValue(null));
134
135
        $this->controller->showAction(1);
136
    }
137
138
    public function testShowActionReturnsResponseWhenFormNotProcessed()
139
    {
140
        $this->expectsUser();
141
        $this->expectsThread();
142
        $this->expectsFormFactoryCreatesForm();
143
        $this->expectsFormHandlerProcessesFormAndReturnsFalse();
144
        $this->expectsFormCreatesAView();
145
        $this->expectsTemplatingRendersResponse();
146
147
        $this->expectsReadStatusUpdates();
148
149
        $this->controller->showAction(1);
150
    }
151
152
    public function testShowActionRedirectsResponseWhenFormProcessed()
153
    {
154
        $this->expectsUser();
155
        $this->expectsThread();
156
        $this->expectsFormFactoryCreatesForm();
157
        $this->expectsFormHandlerProcessesFormAndReturnsTrue();
158
        $this->router->expects($this->once())->method('generate')
159
            ->with('miliooo_message_thread_view', ['threadId' => 1])
160
            ->will($this->returnValue('http://test.com'));
161
162
        $this->controller->showAction(1);
163
    }
164
165
    protected function expectsFormFactoryCreatesForm()
166
    {
167
        $this->formFactory->expects($this->once())
168
            ->method('create')
169
            ->with($this->thread, $this->loggedInUser)
170
            ->will($this->returnValue($this->form));
171
    }
172
173
    protected function expectsFormHandlerProcessesFormAndReturnsFalse()
174
    {
175
        $this->formHandler->expects($this->once())
176
            ->method('process')->with($this->form)
177
            ->will($this->returnValue(false));
178
    }
179
180
    protected function expectsFormHandlerProcessesFormAndReturnsTrue()
181
    {
182
        $this->formHandler->expects($this->once())
183
            ->method('process')->with($this->form)
184
            ->will($this->returnValue(true));
185
186
    }
187
188
    protected function expectsFormCreatesAView()
189
    {
190
        $this->form->expects($this->once())
191
            ->method('createView')
192
            ->will($this->returnValue($this->formView));
193
    }
194
195
    protected function expectsTemplatingRendersResponse()
196
    {
197
        $template = 'MilioooMessagingBundle:ShowThread:show_thread.html.twig';
198
        $this->templating->expects($this->once())->method('renderResponse')
199
            ->with($template, ['thread' => $this->thread, 'form' => $this->formView]);
200
    }
201
202
    protected function setConstructorMocks()
203
    {
204
        $this->formFactory = $this->getMockBuilder('Miliooo\Messaging\Form\FormFactory\ReplyMessageFormFactory')
205
                ->disableOriginalConstructor()->getMock();
206
207
        $this->formHandler = $this->getMockBuilder('Miliooo\Messaging\Form\FormHandler\NewReplyFormHandler')
208
                ->disableOriginalConstructor()->getMock();
209
210
        $this->participantProvider = $this->getMock('Miliooo\Messaging\User\ParticipantProviderInterface');
211
        $this->templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
212
        $this->threadProvider = $this->getMock('Miliooo\Messaging\ThreadProvider\SecureThreadProviderInterface');
213
        $this->readStatusManager = $this->getMock('Miliooo\Messaging\Manager\ReadStatusManagerInterface');
214
        $this->router = $this->getMock('Symfony\Component\Routing\RouterInterface');
215
    }
216
217
    protected function expectsUser()
218
    {
219
        $this->participantProvider->expects($this->once())
220
            ->method('getAuthenticatedParticipant')->will($this->returnValue($this->loggedInUser));
221
    }
222
223
    protected function expectsThread()
224
    {
225
        $this->threadProvider->expects($this->once())
226
            ->method('findThreadForParticipant')
227
            ->with($this->loggedInUser, 1)
228
            ->will($this->returnValue($this->thread));
229
    }
230
231
    protected function expectsReadStatusUpdates()
232
    {
233
        $this->thread->expects($this->once())
234
            ->method('getMessages')
235
            ->will($this->returnValue($this->arrayCollection));
236
237
        $this->arrayCollection->expects($this->once())->method('toArray')
238
            ->will($this->returnValue([$this->message]));
239
240
        $this->readStatusManager
241
            ->expects($this->once())
242
            ->method('updateReadStatusForMessageCollection')
243
            ->with(new ReadStatus(MessageMetaInterface::READ_STATUS_READ), $this->loggedInUser, [$this->message]);
244
    }
245
}
246