1 | <?php |
||
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() |
||
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() |
||
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) |
||
200 | |||
201 | protected function expectsNoMessageMetaForLoggedInUser() |
||
208 | } |
||
209 |