testUpdateReadStatusFiresEventWhenReadStatusChanged()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
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\ReadStatusManagerEventAware;
14
use Miliooo\Messaging\Model\MessageMetaInterface;
15
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
16
use Miliooo\Messaging\User\ParticipantInterface;
17
use Miliooo\Messaging\ValueObjects\ReadStatus;
18
use Miliooo\Messaging\Event\MilioooMessagingEvents;
19
use Miliooo\Messaging\Event\ReadStatusMessageEvent;
20
21
/**
22
 * Test file for Miliooo\Messaging\Manager\NewMessageManager
23
 *
24
 * @author Michiel Boeckaert <[email protected]>
25
 */
26
class ReadStatusManagerEventAwareTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * The class under test.
30
     *
31
     * @var ReadStatusManagerEventAware
32
     */
33
    private $object;
34
35
    /**
36
     * @var \PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $readStatusManager;
39
40
    /**
41
     * @var \PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @var ParticipantInterface
47
     */
48
    private $participant;
49
50
    /**
51
     * @var \PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $message;
54
55
    /**
56
     * @var \PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $messageMeta;
59
60
    /**
61
     * @var ReadStatus
62
     */
63
    private $newReadStatus;
64
65
    public function setUp()
66
    {
67
        $this->readStatusManager = $this->getMock('Miliooo\Messaging\Manager\ReadStatusManagerInterface');
68
        $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
69
        $this->object = new ReadStatusManagerEventAware($this->readStatusManager, $this->eventDispatcher);
70
        $this->participant = new ParticipantTestHelper(1);
71
        $this->message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
72
        $this->messageMeta = $this->getMock('Miliooo\Messaging\Model\MessageMetaInterface');
73
        $this->newReadStatus = new ReadStatus(MessageMetaInterface::READ_STATUS_READ);
74
    }
75
76
    public function testInterface()
77
    {
78
        $this->assertInstanceOf('Miliooo\Messaging\Manager\ReadStatusManagerInterface', $this->object);
79
    }
80
81
    public function testUpdateReadStatusFiresEventWhenReadStatusChanged()
82
    {
83
        //the readstatus manager returns an array with updated messages
84
        $this->readStatusManager->expects($this->once())
85
            ->method('updateReadStatusForMessageCollection')
86
            ->with($this->newReadStatus, $this->participant, [$this->message])
87
            ->will($this->returnValue([$this->message]));
88
89
90
        // create the event
91
        $event = new ReadStatusMessageEvent(
92
            $this->message,
93
            $this->participant
94
0 ignored issues
show
Coding Style introduced by
Empty lines are not allowed in multi-line function calls
Loading history...
95
        );
96
        //we expect a dispatch call
97
        $this->eventDispatcher->expects($this->once())->method('dispatch')
98
            ->with(MilioooMessagingEvents::READ_STATUS_CHANGED, $event);
99
100
        $result = $this->object->updateReadStatusForMessageCollection(
101
            $this->newReadStatus,
102
            $this->participant,
103
            [$this->message]
104
        );
105
106
        $this->assertEquals([$this->message], $result);
107
    }
108
109
    public function testUpdateReadStatusDoesNotDispatchWhenNoUpdates()
110
    {
111
        //the read status manager returns an empty array so no messages have been updated
112
        $this->readStatusManager->expects($this->once())
113
            ->method('updateReadStatusForMessageCollection')
114
            ->with($this->newReadStatus, $this->participant, [$this->message])
115
            ->will($this->returnValue([]));
116
117
        //we don't expect a dispatch call
118
        $this->eventDispatcher->expects($this->never())->method('dispatch');
119
120
        $result = $this->object->updateReadStatusForMessageCollection(
121
            $this->newReadStatus,
122
            $this->participant,
123
            [$this->message]
124
        );
125
126
        $this->assertEquals([], $result);
127
        $this->assertCount(0, $result);
128
    }
129
}
130