testGetThreadMetaReturnsRightAmountOfMetas()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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\Model;
12
13
use Miliooo\Messaging\Model\Thread;
14
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
15
use Miliooo\Messaging\Model\ThreadMeta;
16
use Miliooo\Messaging\TestHelpers\Model\Message;
17
18
/**
19
 * Test file for the thread model
20
 *
21
 * @author Michiel Boeckaert <[email protected]>
22
 */
23
class ThreadTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * The class under test
27
     *
28
     * @var Thread
29
     */
30
    protected $thread;
31
32
    public function setUp()
33
    {
34
        $this->thread = $this->getMockForAbstractClass('Miliooo\Messaging\Model\Thread');
35
    }
36
37
    public function testGettersAndSetters()
38
    {
39
        $this->subjectWorks();
40
        $this->createdByWorks();
41
        $this->createdAtworks();
42
    }
43
44
    public function testInterface()
45
    {
46
        $this->assertInstanceOf('Miliooo\Messaging\Model\ThreadInterface', $this->thread);
47
    }
48
49
    public function testIdWorks()
50
    {
51
        $this->assertAttributeEquals(null, 'id', $this->thread);
52
        $this->assertNull($this->thread->getId());
53
    }
54
55
    public function testArrayCollections()
56
    {
57
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $this->thread->getMessages());
58
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $this->thread->getThreadMeta());
59
        $this->assertAttributeInstanceOf('Doctrine\Common\Collections\ArrayCollection', 'participants', $this->thread);
60
    }
61
62
    public function testAddMessageWorksByCheckingItIsPartOfTheGetMessages()
63
    {
64
        $message = new Message();
65
        $this->thread->addMessage($message);
66
        $messages = $this->thread->getMessages();
67
        $this->assertTrue($messages->contains($message));
68
    }
69
70
    public function testAddThreadMetaWorksByCheckingItIsPartOfTheGetThreadMeta()
71
    {
72
        $threadMeta = $this->getMock('Miliooo\Messaging\Model\ThreadMetaInterface');
73
        $this->thread->addThreadMeta($threadMeta);
74
        $threadMetas = $this->thread->getThreadMeta();
75
        $this->assertTrue($threadMetas->contains($threadMeta));
76
    }
77
78
    public function testAddMessageSetsTheMessageThreadToCurrentThread()
79
    {
80
        $message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
81
        $message->expects($this->once())->method('setThread')->with($this->thread);
82
        $this->thread->addMessage($message);
83
    }
84
85
    public function testAddThreadMetaSetsTheThreadMetaToTheCurrentThread()
86
    {
87
        $threadMeta = $this->getMock('Miliooo\Messaging\Model\ThreadMetaInterface');
88
        $threadMeta->expects($this->once())->method('setThread')->with($this->thread);
89
        $this->thread->addThreadMeta($threadMeta);
90
    }
91
92
    public function testGetParticipants()
93
    {
94
        $participant = new ParticipantTestHelper(1);
95
        $threadMeta = $this->getNewThreadMeta();
96
        $threadMeta->setParticipant($participant);
97
        $threadMeta->setThread($this->thread);
98
        $this->assertEmpty($this->thread->getParticipants());
99
        $this->thread->addThreadMeta($threadMeta);
100
        $this->assertCount(1, $this->thread->getParticipants());
101
        $this->assertEquals($participant, $this->thread->getParticipants()[0]);
102
    }
103
104
    public function testIsParticipantWithNotParticipantReturnsFalse()
105
    {
106
        //add some dummie participants first
107
        $participant = new ParticipantTestHelper(1);
108
        $threadMeta = $this->getNewThreadMeta();
109
        $threadMeta->setParticipant($participant);
110
        $this->thread->addThreadMeta($threadMeta);
111
112
        $participant2 = new ParticipantTestHelper(2);
113
        $threadMeta2 = $this->getNewThreadMeta();
114
        $threadMeta2->setParticipant($participant2);
115
        $this->thread->addThreadMeta($threadMeta2);
116
117
        $participant3 = new ParticipantTestHelper(3);
118
        $this->assertCount(2, $this->thread->getParticipants());
119
120
        $this->assertFalse($this->thread->isParticipant($participant3));
121
    }
122
123
    public function testGetOtherParticipants()
124
    {
125
        //add some dummie participants first
126
        $participant = new ParticipantTestHelper(1);
127
        $threadMeta = $this->getNewThreadMeta();
128
        $threadMeta->setParticipant($participant);
129
        $this->thread->addThreadMeta($threadMeta);
130
131
        $participant2 = new ParticipantTestHelper(2);
132
        $threadMeta2 = $this->getNewThreadMeta();
133
        $threadMeta2->setParticipant($participant2);
134
        $this->thread->addThreadMeta($threadMeta2);
135
136
        $participant3 = new ParticipantTestHelper(3);
137
        $threadMeta3 = $this->getNewThreadMeta();
138
        $threadMeta3->setParticipant($participant3);
139
        $this->thread->addThreadMeta($threadMeta3);
140
141
        $otherParticipants = $this->thread->getOtherParticipants($participant3);
142
        foreach ($otherParticipants as $otherParticipant) {
143
            $this->assertNotEquals($otherParticipant, $participant3);
144
            $this->assertContains($otherParticipant, array($participant2, $participant));
145
        }
146
    }
147
148
    public function testIsParticipantWithParticipantReturnsTrue()
149
    {
150
        $participant = new ParticipantTestHelper(1);
151
        $threadMeta = $this->getNewThreadMeta();
152
        $threadMeta->setParticipant($participant);
153
        $this->thread->addThreadMeta($threadMeta);
154
        $this->assertTrue($this->thread->isParticipant($participant));
155
    }
156
157
    public function testGetMessagesReturnsRightAmountOfmessages()
158
    {
159
        $message = $this->getMock('Miliooo\Messaging\Model\MessageInterface');
160
        $this->assertCount(0, $this->thread->getMessages());
161
        $this->thread->addMessage($message);
162
        $this->thread->addMessage($message);
163
        $this->assertCount(2, $this->thread->getMessages());
164
    }
165
166
    public function testGetThreadMetaReturnsRightAmountOfMetas()
167
    {
168
        $threadMeta = $this->getMock('Miliooo\Messaging\Model\ThreadMetaInterface');
169
        $this->assertCount(0, $this->thread->getThreadMeta());
170
        $this->thread->addThreadMeta($threadMeta);
171
        $this->thread->addThreadMeta($threadMeta);
172
        $this->assertCount(2, $this->thread->getThreadMeta());
173
    }
174
175
    public function testGetFirstMessage()
176
    {
177
        $message = $this->getNewMessage();
178
        $message->setBody('the first message');
179
        $message2 = $this->getNewMessage();
180
        $message2->setBody('this is the second message');
181
        $this->thread->addMessage($message);
182
        $this->thread->addMessage($message2);
183
184
        $this->assertCount(2, $this->thread->getMessages());
185
        $this->assertEquals('the first message', $this->thread->getFirstMessage()->getBody());
186
    }
187
188
    public function testLastMessageWorks()
189
    {
190
        //assert last message is null by default
191
        $this->assertNull($this->thread->getLastMessage());
192
193
        $message = new Message();
194
        $message->setBody('hoi hoi');
195
196
        $this->thread->setLastMessage($message);
197
        $this->assertSame($message, $this->thread->getLastMessage());
198
    }
199
200
    public function testGetThreadMetaForParticipant()
201
    {
202
        $participant1 = new ParticipantTestHelper(1);
203
        $threadMeta = $this->getNewThreadMeta();
204
        $threadMeta->setParticipant($participant1);
205
        $this->thread->addThreadMeta($threadMeta);
206
207
        $participant2 = new ParticipantTestHelper(2);
208
        $threadMeta2 = $this->getNewThreadMeta();
209
        $threadMeta2->setParticipant($participant2);
210
        $this->thread->addThreadMeta($threadMeta2);
211
212
        $this->assertSame($threadMeta2, $this->thread->getThreadMetaForParticipant($participant2));
213
    }
214
215
    public function testGetThreadMetaForParticipantReturnsNullWhenNotFound()
216
    {
217
        $participant1 = new ParticipantTestHelper(1);
218
219
        $this->assertNull($this->thread->getThreadMetaForParticipant($participant1));
220
        $threadMeta = $this->getNewThreadMeta();
221
        $threadMeta->setParticipant($participant1);
222
        $this->thread->addThreadMeta($threadMeta);
223
224
        $participant2 = new ParticipantTestHelper(2);
225
        $this->assertNull($this->thread->getThreadMetaForParticipant($participant2));
226
    }
227
228
    protected function subjectWorks()
229
    {
230
        $subject = 'this is the subject';
231
        $this->thread->setSubject($subject);
232
        $this->assertSame($subject, $this->thread->getSubject());
233
    }
234
235
    protected function createdByWorks()
236
    {
237
        $participant = new ParticipantTestHelper('participant');
238
        $this->thread->setCreatedBy($participant);
239
        $this->assertEquals($participant, $this->thread->getCreatedBy());
240
    }
241
242
    protected function createdAtWorks()
243
    {
244
        $createdAt = new \DateTime('2013-10-12 00:00:00');
245
        $this->thread->setCreatedAt($createdAt);
246
        $this->assertSame($createdAt, $this->thread->getCreatedAt());
247
    }
248
249
    /**
250
     * Helper function to get a new message instance from an abstract class
251
     *
252
     * @return Message
253
     */
254
    protected function getNewMessage()
255
    {
256
        return $this->getMockForAbstractClass('Miliooo\Messaging\Model\Message');
257
    }
258
259
    /**
260
     * Helper function to get a new thread meta from an abstract class
261
     *
262
     * @return ThreadMeta
263
     */
264
    protected function getNewThreadMeta()
265
    {
266
        return $this->getMockForAbstractClass('Miliooo\Messaging\Model\ThreadMeta');
267
    }
268
}
269