MessagingExtensionTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 12
rs 9.4285
c 2
b 0
f 1
cc 1
eloc 9
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\Twig\Extension;
12
13
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
14
use Miliooo\MessagingBundle\Twig\Extension\MessagingExtension;
15
use Miliooo\Messaging\User\ParticipantInterface;
16
use Miliooo\Messaging\Model\MessageMetaInterface;
17
18
/**
19
 * Test file for Miliooo\MessagingBundle\Twig\Extension\MessagingExtension
20
 *
21
 * @author Michiel Boeckaert <[email protected]>
22
 */
23
class MessagingExtensionTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * The class under test
27
     *
28
     * @var MessagingExtension
29
     */
30
    private $messagingExtension;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $participantProvider;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject
39
     */
40
    private $message;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject
44
     */
45
    private $messageMeta;
46
47
    /**
48
     * @var \PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $thread;
51
52
    /**
53
     * @var \PHPUnit_Framework_MockObject_MockObject
54
     */
55
    private $threadMeta;
56
57
    /**
58
     * @var ParticipantInterface
59
     */
60
    private $loggedInUser;
61
62
    /**
63
     * @var \PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $unreadMessagesProvider;
66
67
    public function setUp()
68
    {
69
        $this->participantProvider = $this->getMock('Miliooo\Messaging\User\ParticipantProviderInterface');
70
        $this->unreadMessagesProvider = $this->getMock('Miliooo\Messaging\Notifications\UnreadMessagesProviderInterface');
71
        $this->messagingExtension = new MessagingExtension($this->participantProvider, $this->unreadMessagesProvider);
72
73
        $this->message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
74
        $this->messageMeta = $this->getMock('Miliooo\Messaging\Model\MessageMetaInterface');
75
        $this->thread = $this->getMock('Miliooo\Messaging\Model\ThreadInterface');
76
        $this->threadMeta = $this->getMock('Miliooo\Messaging\Model\ThreadMetaInterface');
77
        $this->loggedInUser = new ParticipantTestHelper(1);
78
    }
79
80
    public function testGetName()
81
    {
82
        $this->assertEquals('miliooo_messaging', $this->messagingExtension->getName());
83
    }
84
85
    public function testIsMessageReadWithNoMessageMetaForParticipant()
86
    {
87
        $this->expectsLoggedInUser();
88
        $this->expectsNoMessageMetaForLoggedInUser();
89
90
        $this->assertFalse($this->messagingExtension->isMessageNewRead($this->message));
91
    }
92
93
    public function testIsMessageReadWithPreviousStateNull()
94
    {
95
        $this->expectsLoggedInUser();
96
        $this->expectsMessageMetaForLoggedInUser();
97
        //read status has not changed
98
        $this->messageMeta->expects($this->once())
99
            ->method('getPreviousReadStatus')
100
            ->will($this->returnValue(null));
101
        //so it cant be a new read
102
        $this->messageMeta->expects($this->never())
103
            ->method('getReadStatus');
104
105
        $this->assertFalse($this->messagingExtension->isMessageNewRead($this->message));
106
    }
107
108
    public function testIsMessageReadWithPreviousReadStatusButReadStatusNotRead()
109
    {
110
        $this->expectsLoggedInUser();
111
        $this->expectsMessageMetaForLoggedInUser();
112
        $this->messageMeta->expects($this->once())->method('getPreviousReadStatus')
113
            ->will($this->returnValue(MessageMetaInterface::READ_STATUS_READ));
114
115
        $this->messageMeta->expects($this->once())->method('getReadStatus')
116
            ->will($this->returnValue(MessageMetaInterface::READ_STATUS_MARKED_UNREAD));
117
118
        $this->assertFalse($this->messagingExtension->isMessageNewRead($this->message));
119
    }
120
121
    public function testIsNewReadWReturnsTrue()
122
    {
123
        $this->expectsLoggedInUser();
124
        $this->expectsMessageMetaForLoggedInUser();
125
126
        $this->messageMeta->expects($this->once())->method('getPreviousReadStatus')
127
            ->will($this->returnValue(MessageMetaInterface::READ_STATUS_NEVER_READ));
128
129
        $this->messageMeta->expects($this->once())->method('getReadStatus')
130
            ->will($this->returnValue(MessageMetaInterface::READ_STATUS_READ));
131
132
        $this->assertTrue($this->messagingExtension->isMessageNewRead($this->message));
133
    }
134
135
    public function testGetThreadUnreadCount()
136
    {
137
        $this->expectsLoggedInUser();
138
        $this->expectsThreadMetaForLoggedInUser();
139
140
        $this->threadMeta->expects($this->once())->method('getUnreadMessageCount')
141
            ->will($this->returnValue(5));
142
143
        $this->assertEquals(5, $this->messagingExtension->getThreadUnreadCount($this->thread));
144
    }
145
146
    public function testGetUnreadMessageCountWhenParticipantNotThreadParticipant()
147
    {
148
        $this->expectsLoggedInUser();
149
        $this->expectsNoThreadMetaForLoggedInUser();
150
        $this->assertEquals(0, $this->messagingExtension->getThreadUnreadCount($this->thread));
151
    }
152
153
    public function testGetFunctions()
154
    {
155
        $this->assertArrayHasKey('miliooo_messaging_is_new_read', $this->messagingExtension->getFunctions());
156
        $this->assertArrayHasKey('miliooo_messaging_thread_unread_count', $this->messagingExtension->getFunctions());
157
        $this->assertArrayHasKey('miliooo_messaging_unread_messages_count', $this->messagingExtension->getFunctions());
158
    }
159
160
    public function testGetUnreadMessagesCount()
161
    {
162
        $this->expectsLoggedInUser();
163
        $this->unreadMessagesProvider->expects($this->once())
164
            ->method('getUnreadMessageCountForParticipant')
165
            ->with($this->loggedInUser)
166
            ->will($this->returnValue(3));
167
168
        $this->assertEquals(3, $this->messagingExtension->getUnreadMessagesCount());
169
    }
170
171
    protected function expectsLoggedInUser()
172
    {
173
        $this->participantProvider
174
            ->expects($this->once())
175
            ->method('getAuthenticatedParticipant')
176
            ->will($this->returnValue($this->loggedInUser));
177
    }
178
179
    protected function expectsMessageMetaForLoggedInUser()
180
    {
181
        $this->message->expects($this->once())
182
            ->method('getMessageMetaForParticipant')
183
            ->with($this->loggedInUser)
184
            ->will($this->returnValue($this->messageMeta));
185
    }
186
187
    protected function expectsThreadMetaForLoggedInUser()
188
    {
189
        $this->thread->expects($this->once())->method('getThreadMetaForParticipant')
190
            ->with($this->loggedInUser)
191
            ->will($this->returnValue($this->threadMeta));
192
    }
193
194
    protected function expectsNoThreadMetaForLoggedInUser()
195
    {
196
        $this->thread->expects($this->once())->method('getThreadMetaForParticipant')
197
            ->with($this->loggedInUser)
198
            ->will($this->returnValue(null));
199
    }
200
201
    protected function expectsNoMessageMetaForLoggedInUser()
202
    {
203
        $this->message->expects($this->once())
204
            ->method('getMessageMetaForParticipant')
205
            ->with($this->loggedInUser)
206
            ->will($this->returnValue(null));
207
    }
208
}
209