getMessageWithUnreadMessageMetaForParticipant()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
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\Messaging\Tests\Manager;
12
13
use Miliooo\Messaging\Manager\ReadStatusManager;
14
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
15
use Miliooo\Messaging\User\ParticipantInterface;
16
use Miliooo\Messaging\TestHelpers\Model\Message;
17
use Miliooo\Messaging\TestHelpers\Model\MessageMeta;
18
use Miliooo\Messaging\TestHelpers\Model\Thread;
19
use Miliooo\Messaging\TestHelpers\Model\ThreadMeta;
20
use Miliooo\Messaging\Model\MessageInterface;
21
use Miliooo\Messaging\Model\MessageMetaInterface;
22
use Miliooo\Messaging\ValueObjects\ReadStatus;
23
24
/**
25
 * Test file for the read status manager
26
 *
27
 * @author Michiel Boeckaert <[email protected]>
28
 */
29
class ReadStatusManagerTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * The class under test
33
     *
34
     * @var ReadStatusManager
35
     */
36
    private $readStatusManager;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject
40
     */
41
    private $messageRepository;
42
43
    /**
44
     * @var ParticipantInterface
45
     */
46
    private $participant;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $message;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private $messageMeta;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject
60
     */
61
    private $thread;
62
63
    /**
64
     * @var \PHPUnit_Framework_MockObject_MockObject
65
     */
66
    private $threadMeta;
67
68
    public function setUp()
69
    {
70
        $this->participant = new ParticipantTestHelper(1);
71
        $this->messageRepository = $this->getMock('Miliooo\Messaging\Repository\MessageRepositoryInterface');
72
        $this->readStatusManager = new ReadStatusManager($this->messageRepository);
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
    }
78
79
    public function testInterface()
80
    {
81
        $this->assertInstanceOf('Miliooo\Messaging\Manager\ReadStatusManagerInterface', $this->readStatusManager);
82
    }
83
84
    public function testUpdateReadStatusDoesNotUpdateWhenNoMessageMetaFoundForParticipant()
85
    {
86
        $notParticipant = new ParticipantTestHelper('now participant');
87
        $message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
88
89
        $message->expects($this->once())
90
            ->method('getMessageMetaForParticipant')
91
            ->with($notParticipant)
92
            ->will($this->returnValue(null));
93
94
95
        $this->messageRepository->expects($this->never())
96
            ->method('save');
97
98
        $this->messageRepository->expects($this->never())
99
            ->method('flush');
100
101
        $this->readStatusManager->updateReadStatusForMessageCollection(
102
            new ReadStatus(MessageMetaInterface::READ_STATUS_READ),
103
            $notParticipant,
104
            [$message]
105
        );
106
    }
107
108
    public function testUpdateReadStatusSavesTheNewStatusWhenStatusChanged()
109
    {
110
        $this->expectsMessageMetaForParticipant();
111
        $this->expectsAskingMessageMetaForReadStatusWillReturn(MessageMetaInterface::READ_STATUS_READ);
112
        $this->expectsUpdatingMessageMetaReadStatusWith(MessageMetaInterface::READ_STATUS_NEVER_READ);
113
        $this->expectsThreadUnreadCountUpdateIncrement();
114
115
116
        //expects save
117
        $this->messageRepository->expects($this->once())
118
            ->method('save')
119
            ->with($this->message, false);
120
121
        //expects flush
122
        $this->messageRepository->expects($this->once())
123
            ->method('flush');
124
125
        $results = $this->readStatusManager->updateReadStatusForMessageCollection(
126
            new ReadStatus(MessageMetaInterface::READ_STATUS_NEVER_READ),
127
            $this->participant,
128
            [$this->message]
129
        );
130
        //expect an array wit this message
131
        $this->assertEquals([$this->message], $results);
132
        //expect only one message in the array
133
        $this->assertCount(1, $results);
134
    }
135
136
    public function testUpdateReadStatusDoesNotUpdateAndReturnsEmptyWhenNoUpdates()
137
    {
138
        $this->expectsMessageMetaForParticipant();
139
        $this->expectsAskingMessageMetaForReadStatusWillReturn(MessageMetaInterface::READ_STATUS_NEVER_READ);
140
        $this->messageMeta->expects($this->never())->method('setReadStatus');
141
142
        $this->messageRepository->expects($this->never())->method('save');
143
        $this->messageRepository->expects($this->never())->method('flush');
144
145
        $results = $this->readStatusManager->updateReadStatusForMessageCollection(
146
            new ReadStatus(MessageMetaInterface::READ_STATUS_NEVER_READ),
147
            $this->participant,
148
            [$this->message]
149
        );
150
151
        $this->assertCount(0, $results);
152
        $this->assertNotContains($this->message, $results);
153
    }
154
155
    public function testUpdateReadStatusWith2MessagesReturns2MessagesAndFlushesOnce()
156
    {
157
        //use a real message in this test this is more a functional test now.
158
        $message = $this->getMessageWithUnreadMessageMetaForParticipant();
159
        $message2 = $this->getMessageWithUnreadMessageMetaForParticipant();
160
161
        $this->messageRepository->expects($this->at(0))
162
            ->method('save')
163
            ->with($message, false);
164
165
        $this->messageRepository->expects($this->at(1))
166
            ->method('save')
167
            ->with($message, false);
168
169
        $this->messageRepository->expects($this->once())
170
            ->method('flush');
171
172
        //expects an array with both messages to be updated
173
        $updated = $this->readStatusManager->updateReadStatusForMessageCollection(
174
            new ReadStatus(MessageMetaInterface::READ_STATUS_READ),
175
            $this->participant,
176
            [$message, $message2]
177
        );
178
179
        $this->assertCount(2, $updated);
180
        $this->assertContains($message, $updated);
181
        $this->assertContains($message2, $updated);
182
183
        //lets check if the count has actually increased
184
        $count = $message->getThread()->getThreadMetaForParticipant($this->participant)->getUnreadMessageCount();
185
        $this->assertEquals(4, $count);
186
    }
187
188
    /**
189
     * Returns a message with status unread for the given participant;
190
     *
191
     * @return MessageInterface
192
     */
193
    protected function getMessageWithUnreadMessageMetaForParticipant()
194
    {
195
        $message = new Message();
196
        $messageMeta = new MessageMeta();
197
        $thread = new Thread();
198
199
200
        $threadMeta = new ThreadMeta();
201
        $threadMeta->setParticipant($this->participant);
202
        $threadMeta->setThread($thread);
203
        $threadMeta->setUnreadMessageCount(5);
204
        $thread->addThreadMeta($threadMeta);
205
206
207
        $messageMeta->setParticipant($this->participant);
208
        $messageMeta->setReadStatus(new ReadStatus(MessageMetaInterface::READ_STATUS_NEVER_READ));
209
        $messageMeta->setMessage($message);
210
211
        $message->addMessageMeta($messageMeta);
212
        $message->setThread($thread);
213
214
        $thread->addMessage($message);
215
216
        return $message;
217
    }
218
219
    protected function expectsMessageMetaForParticipant()
220
    {
221
        $this->message->expects($this->once())->method('getMessageMetaForParticipant')
222
            ->with($this->participant)
223
            ->will($this->returnValue($this->messageMeta));
224
    }
225
226
    /**
227
     * @param integer $readStatusValue The read status value
228
     */
229
    protected function expectsAskingMessageMetaForReadStatusWillReturn($readStatusValue)
230
    {
231
        $this->messageMeta->expects($this->once())->method('getReadStatus')
232
            ->will($this->returnValue($readStatusValue));
233
    }
234
235
    /**
236
     * @param integer $readStatusValue The read status value
237
     */
238
    protected function expectsUpdatingMessageMetaReadStatusWith($readStatusValue)
239
    {
240
        $this->messageMeta->expects($this->once())->method('setReadStatus')
241
            ->with(new ReadStatus($readStatusValue));
242
243
    }
244
245
    protected function expectsThreadUnreadCountUpdateIncrement()
246
    {
247
        $this->message->expects($this->once())->method('getThread')->will($this->returnValue($this->thread));
248
249
        $this->thread->expects($this->once())->method('getThreadMetaForParticipant')->with($this->participant)
250
            ->will($this->returnValue($this->threadMeta));
251
252
        $this->threadMeta->expects($this->once())->method('getUnreadMessageCount')->will($this->returnValue(0));
253
254
        $this->threadMeta->expects($this->once())->method('setUnreadMessageCount')->with(1);
255
    }
256
}
257