expectsCurrentStatusCallOnThreadMetaAndReturns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\Messaging\Tests\Manager;
12
13
use Miliooo\Messaging\Manager\ThreadStatusManager;
14
use Miliooo\Messaging\User\ParticipantInterface;
15
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
16
use Miliooo\Messaging\ValueObjects\ThreadStatus;
17
use Miliooo\Messaging\Model\ThreadMetaInterface;
18
use Miliooo\Messaging\ValueObjects\ReadStatus;
19
use Miliooo\Messaging\Model\MessageMetaInterface;
20
21
/**
22
 * Test file for the thread status manager.
23
 *
24
 * @author Michiel Boeckaert <[email protected]>
25
 */
26
class ThreadStatusManagerTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * The class under test.
30
     *
31
     * @var ThreadStatusManager
32
     */
33
    private $threadStatusManager;
34
35
    /**
36
     * @var ParticipantInterface
37
     */
38
    private $participant;
39
40
    /**
41
     * @var \PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $thread;
44
45
    /**
46
     * @var \PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $threadMeta;
49
50
    /**
51
     * @var \PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $threadRepository;
54
55
    /**
56
     * @var \PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $messageRepository;
59
60
    /**
61
     * @var \PHPUnit_Framework_MockObject_MockObject
62
     */
63
    private $readStatusManager;
64
65
    /**
66
     * @var \PHPUnit_Framework_MockObject_MockObject
67
     */
68
    private $message;
69
70
    public function setUp()
71
    {
72
        $this->threadRepository = $this->getMock('Miliooo\Messaging\Repository\ThreadRepositoryInterface');
73
        $this->messageRepository = $this->getMock('Miliooo\Messaging\Repository\MessageRepositoryInterface');
74
        $this->readStatusManager = $this->getMock('Miliooo\Messaging\Manager\ReadStatusManagerInterface');
75
        $this->threadStatusManager = new ThreadStatusManager(
76
            $this->threadRepository,
77
            $this->messageRepository,
78
            $this->readStatusManager
79
        );
80
        $this->participant = new ParticipantTestHelper(1);
81
        $this->thread = $this->getMock('Miliooo\Messaging\Model\ThreadInterface');
82
        $this->threadMeta = $this->getMock('Miliooo\Messaging\Model\ThreadMetaInterface');
83
        $this->message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
84
    }
85
86
    public function testInterface()
87
    {
88
        $this->assertInstanceOf('Miliooo\Messaging\Manager\ThreadStatusManagerInterface', $this->threadStatusManager);
89
    }
90
91
    public function testArchivingAnActiveThreadWhichHasUnreadMessages()
92
    {
93
        $this->expectsThreadMetaForParticipant();
94
        $this->expectsCurrentStatusCallOnThreadMetaAndReturns(ThreadMetaInterface::STATUS_ACTIVE);
95
96
        $this->expectsSetStatusWith(ThreadMetaInterface::STATUS_ARCHIVED);
97
98
        $this->expectsMessageRepositoryCallAndWillReturn([$this->message]);
99
100
        $readStatus = new ReadStatus(MessageMetaInterface::READ_STATUS_READ);
101
        $this->readStatusManager->expects($this->once())
102
            ->method('updateReadStatusForMessageCollection')
103
            ->with($readStatus, $this->participant, [$this->message]);
104
105
        $result = $this->threadStatusManager->updateThreadStatusForParticipant(
106
            new ThreadStatus(ThreadMetaInterface::STATUS_ARCHIVED),
107
            $this->thread,
108
            $this->participant
109
        );
110
111
        $this->assertTrue($result);
112
    }
113
114
    public function testArchivingAnActiveThreadWhichHasNoUnreadMessages()
115
    {
116
        $this->expectsThreadMetaForParticipant();
117
        $this->expectsCurrentStatusCallOnThreadMetaAndReturns(ThreadMetaInterface::STATUS_ACTIVE);
118
        $this->expectsSetStatusWith(ThreadMetaInterface::STATUS_ARCHIVED);
119
        $this->expectsMessageRepositoryCallAndWillReturn([]);
120
        $this->readStatusManager->expects($this->never())
121
            ->method('updateReadStatusForMessageCollection');
122
123
124
        $result = $this->threadStatusManager->updateThreadStatusForParticipant(
125
            new ThreadStatus(ThreadMetaInterface::STATUS_ARCHIVED),
126
            $this->thread,
127
            $this->participant
128
        );
129
        $this->assertTrue($result);
130
    }
131
132
    public function testUpdatingAThreadStatusWithTheSameStatus()
133
    {
134
        $this->expectsThreadMetaForParticipant();
135
        $this->expectsCurrentStatusCallOnThreadMetaAndReturns(ThreadMetaInterface::STATUS_ARCHIVED);
136
        $this->expectsNoSetStatusCall();
137
138
        $result = $this->threadStatusManager->updateThreadStatusForParticipant(
139
            new ThreadStatus(ThreadMetaInterface::STATUS_ARCHIVED),
140
            $this->thread,
141
            $this->participant
142
        );
143
144
        $this->assertFalse($result);
145
    }
146
147
    public function testMakingAnArchivedTreadActiveDoesNotCheckReadStatusUpdates()
148
    {
149
        $this->expectsThreadMetaForParticipant();
150
        $this->expectsCurrentStatusCallOnThreadMetaAndReturns(ThreadMetaInterface::STATUS_ARCHIVED);
151
        $this->expectsSetStatusWith(ThreadMetaInterface::STATUS_ACTIVE);
152
        $this->expectsNoMessageReadStatusUpdateChecks();
153
154
        $result = $this->threadStatusManager->updateThreadStatusForParticipant(
155
            new ThreadStatus(ThreadMetaInterface::STATUS_ACTIVE),
156
            $this->thread,
157
            $this->participant
158
        );
159
160
        $this->assertTrue($result);
161
    }
162
163
    /**
164
     * @param mixed $mixed The return value
165
     */
166
    protected function expectsMessageRepositoryCallAndWillReturn($mixed)
167
    {
168
        $this->messageRepository->expects($this->once())->method('getUnreadMessagesFromThreadForParticipant')
169
            ->with($this->participant, $this->thread)
170
            ->will($this->returnValue($mixed));
171
    }
172
173
    protected function expectsThreadMetaForParticipant()
174
    {
175
        $this->thread->expects($this->once())->method('getThreadMetaForParticipant')
176
            ->with($this->participant)
177
            ->will($this->returnValue($this->threadMeta));
178
    }
179
180
181
    /**
182
     * @param integer $threadStatus The current thread status
183
     */
184
    protected function expectsCurrentStatusCallOnThreadMetaAndReturns($threadStatus)
185
    {
186
        $this->threadMeta->expects($this->once())->method('getStatus')
187
            ->will($this->returnValue($threadStatus));
188
    }
189
190
    protected function expectsNoSetStatusCall()
191
    {
192
        $this->threadMeta->expects($this->never())->method('setStatus');
193
        $this->expectsNoMessageReadStatusUpdateChecks();
194
195
        $this->threadRepository->expects($this->never())->method('save');
196
    }
197
198
199
    /**
200
     * expects a set status update.
201
     *
202
     * - setting the status
203
     * - saving the new thread
204
     *
205
     * @param integer $newThreadStatus The new thread status
206
     */
207
    protected function expectsSetStatusWith($newThreadStatus)
208
    {
209
        $this->threadMeta->expects($this->once())->method('setStatus')
210
            ->with($newThreadStatus);
211
212
        $this->threadRepository->expects($this->once())->method('save')->with($this->thread);
213
    }
214
215
    protected function expectsNoMessageReadStatusUpdateChecks()
216
    {
217
        $this->messageRepository->expects($this->never())->method('getUnreadMessagesFromThreadForParticipant');
218
        $this->readStatusManager->expects($this->never())
219
            ->method('updateReadStatusForMessageCollection');
220
    }
221
}
222