DeleteThreadControllerTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 154
rs 10
c 1
b 0
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A expectsThreadExists() 0 7 1
A expectsRouterInbox() 0 8 1
A setUp() 0 21 1
A testDeleteWithExistingThreadAndPermissions() 0 18 1
A testDeleteWithExistingThreadButNoPermissions() 0 19 1
A testDeleteWithNonExistingThread() 0 20 1
A expectsLoggedInUser() 0 5 1
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\DeleteThreadController;
14
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
15
use Miliooo\Messaging\User\ParticipantInterface;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
18
/**
19
 * Test file for the delete thread controller.
20
 *
21
 * @author Michiel Boeckaert <[email protected]>
22
 */
23
class DeleteThreadControllerTest extends \PHPUnit_Framework_TestCase
24
{
25
    const THREAD_ID = 1;
26
27
    /**
28
     * @var DeleteThreadController
29
     */
30
    private $controller;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $templating;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject
39
     */
40
    private $deleteThreadManager;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject
44
     */
45
    private $threadProvider;
46
47
    /**
48
     * @var \PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $thread;
51
52
    /**
53
     * @var \PHPUnit_Framework_MockObject_MockObject
54
     */
55
    private $flashMessageProvider;
56
57
    /**
58
     * @var \PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $router;
61
62
    /**
63
     * @var \PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $participantProvider;
66
67
    /**
68
     * @var ParticipantInterface
69
     */
70
    private $loggedInUser;
71
72
    public function setUp()
73
    {
74
        $this->templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
75
        $this->deleteThreadManager = $this->getMock('Miliooo\Messaging\Manager\DeleteThreadManagerSecureInterface');
76
        $this->threadProvider = $this->getMock('Miliooo\Messaging\ThreadProvider\ThreadProviderInterface');
77
        $this->flashMessageProvider = $this->getMock('Miliooo\Messaging\Helpers\FlashMessages\FlashMessageProviderInterface');
78
        $this->router = $this->getMock('Symfony\Component\Routing\RouterInterface');
79
        $this->participantProvider = $this->getMock('Miliooo\Messaging\User\ParticipantProviderInterface');
80
        $this->loggedInUser = new ParticipantTestHelper(1);
81
82
        $this->controller = new DeleteThreadController(
83
            $this->templating,
84
            $this->deleteThreadManager,
85
            $this->threadProvider,
86
            $this->flashMessageProvider,
87
            $this->router,
88
            $this->participantProvider
89
        );
90
91
        $this->thread = $this->getMock('Miliooo\Messaging\Model\ThreadInterface');
92
    }
93
94
    public function testDeleteWithExistingThreadAndPermissions()
95
    {
96
        $this->expectsLoggedInUser();
97
        $this->expectsThreadExists();
98
99
        //doesn't throw an exception
100
        $this->deleteThreadManager->expects($this->once())
101
            ->method('deleteThread')
102
            ->with($this->loggedInUser, $this->thread);
103
104
        $this->flashMessageProvider->expects($this->once())
105
            ->method('addFlash')
106
            ->with('success', 'flash.thread_deleted_success');
107
108
        $this->expectsRouterInbox();
109
110
        $this->controller->deleteAction(self::THREAD_ID);
111
    }
112
113
    public function testDeleteWithExistingThreadButNoPermissions()
114
    {
115
        $this->expectsLoggedInUser();
116
        $this->expectsThreadExists();
117
118
        //throws an exception
119
        $this->deleteThreadManager->expects($this->once())
120
            ->method('deleteThread')
121
            ->with($this->loggedInUser, $this->thread)
122
            ->will($this->throwException(new AccessDeniedException()));
123
124
        $this->flashMessageProvider->expects($this->once())
125
            ->method('addFlash')
126
            ->with('error', 'flash.thread_delete_no_permission');
127
128
        $this->expectsRouterInbox();
129
130
        $this->controller->deleteAction(self::THREAD_ID);
131
    }
132
133
    public function testDeleteWithNonExistingThread()
134
    {
135
        $this->expectsLoggedInUser();
136
137
        $this->threadProvider->expects($this->once())
138
            ->method('findThreadById')
139
            ->with(self::THREAD_ID)
140
            ->will($this->returnValue(null));
141
142
        $this->flashMessageProvider->expects($this->once())
143
            ->method('addFlash')
144
            ->with('error', 'flash.thread_not_found');
145
146
        $this->deleteThreadManager->expects($this->never())
147
            ->method('deleteThread');
148
149
        $this->expectsRouterInbox();
150
151
        $this->controller->deleteAction(self::THREAD_ID);
152
    }
153
154
    protected function expectsThreadExists()
155
    {
156
        $this->threadProvider->expects($this->once())
157
            ->method('findThreadById')
158
            ->with(self::THREAD_ID)
159
            ->will($this->returnValue($this->thread));
160
    }
161
162
    protected function expectsRouterInbox()
163
    {
164
        //we need to return an url or redirect response will error out...
165
        $this->router->expects($this->once())
166
            ->method('generate')
167
            ->with('miliooo_message_inbox')
168
            ->will($this->returnValue('http://test.com'));
169
    }
170
171
    protected function expectsLoggedInUser()
172
    {
173
        $this->participantProvider->expects($this->once())->method('getAuthenticatedParticipant')
174
            ->will($this->returnValue($this->loggedInUser));
175
    }
176
}
177