forked from FriendsOfSymfony/FOSMessageBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadManagerTest.php
220 lines (183 loc) · 6.14 KB
/
ThreadManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
namespace FOS\MessageBundle\Tests\EntityManager;
use FOS\MessageBundle\EntityManager\ThreadManager;
use FOS\MessageBundle\Model\ThreadInterface;
use PHPUnit\Framework\TestCase;
/**
* Class ThreadManagerTest.
*
* @author Tobias Nyholm
*/
class ThreadManagerTest extends TestCase
{
protected $user;
protected $date;
/**
* This method should be setUp(): void
* For compatibility reasons with old versions of PHP, we cannot use neither setUp(): void nor setUp().
*/
public function setUpBeforeTest()
{
$this->user = $this->createParticipantMock('4711');
$this->date = new \DateTime('2013-12-25');
}
/**
* Usual test case where neither createdBy or createdAt is set.
*/
public function testDoCreatedByAndAt()
{
$this->setUpBeforeTest();
$thread = $this->createThreadMock();
$thread->expects($this->exactly(1))->method('getFirstMessage')
->will($this->returnValue($this->createMessageMock()));
$threadManager = new TestThreadManager();
$threadManager->doCreatedByAndAt($thread);
}
/**
* Test where createdBy is set.
*/
public function testDoCreatedByAndAtWithCreatedBy()
{
$this->setUpBeforeTest();
$thread = $this->createThreadMock();
$thread->expects($this->exactly(0))->method('setCreatedBy');
$thread->expects($this->exactly(1))->method('setCreatedAt');
$thread->expects($this->exactly(1))->method('getCreatedBy')
->will($this->returnValue($this->user));
$thread->expects($this->exactly(1))->method('getFirstMessage')
->will($this->returnValue($this->createMessageMock()));
$threadManager = new TestThreadManager();
$threadManager->doCreatedByAndAt($thread);
}
/**
* Test where createdAt is set.
*/
public function testDoCreatedByAndAtWithCreatedAt()
{
$this->setUpBeforeTest();
$thread = $this->createThreadMock();
$thread->expects($this->exactly(1))->method('setCreatedBy');
$thread->expects($this->exactly(0))->method('setCreatedAt');
$thread->expects($this->exactly(1))->method('getCreatedAt')
->will($this->returnValue($this->date));
$thread->expects($this->exactly(1))->method('getFirstMessage')
->will($this->returnValue($this->createMessageMock()));
$threadManager = new TestThreadManager();
$threadManager->doCreatedByAndAt($thread);
}
/**
* Test where both craetedAt and createdBy is set.
*/
public function testDoCreatedByAndAtWithCreatedAtAndBy()
{
$this->setUpBeforeTest();
$thread = $this->createThreadMock();
$thread->expects($this->exactly(0))->method('setCreatedBy');
$thread->expects($this->exactly(0))->method('setCreatedAt');
$thread->expects($this->exactly(1))->method('getCreatedAt')
->will($this->returnValue($this->date));
$thread->expects($this->exactly(1))->method('getCreatedBy')
->will($this->returnValue($this->user));
$thread->expects($this->exactly(1))->method('getFirstMessage')
->will($this->returnValue($this->createMessageMock()));
$threadManager = new TestThreadManager();
$threadManager->doCreatedByAndAt($thread);
}
/**
* Test where thread do not have a message.
*/
public function testDoCreatedByAndNoMessage()
{
$this->setUpBeforeTest();
$thread = $this->createThreadMock();
$thread->expects($this->exactly(0))->method('setCreatedBy');
$thread->expects($this->exactly(0))->method('setCreatedAt');
$thread->expects($this->exactly(0))
->method('getCreatedAt')
->will($this->returnValue($this->date));
$thread->expects($this->exactly(0))
->method('getCreatedBy')
->will($this->returnValue($this->user));
$threadManager = new TestThreadManager();
$threadManager->doCreatedByAndAt($thread);
}
/**
* Get a message mock.
*
* @return mixed
*/
protected function createMessageMock()
{
$message = $this->getMockBuilder('FOS\MessageBundle\Document\Message')
->getMock();
$message->expects($this->any())
->method('getSender')
->will($this->returnValue($this->user));
$message->expects($this->any())
->method('getCreatedAt')
->will($this->returnValue($this->date));
return $message;
}
/**
* Add expectations on the thread mock.
*
* @param mock &$thread
* @param int $createdByCalls
* @param int $createdAtCalls
*/
protected function addThreadExpectations(&$thread, $createdByCalls = 1, $createdAtCalls = 1)
{
$thread->expects($this->exactly($createdByCalls))
->method('setCreatedBy')
->with($this->equalTo($this->user));
$thread->expects($this->exactly($createdAtCalls))
->method('setCreatedAt')
->with($this->equalTo($this->date));
}
/**
* Get a Participant.
*
* @param $id
*
* @return mixed
*/
protected function createParticipantMock($id)
{
$participant = $this->getMockBuilder('FOS\MessageBundle\Model\ParticipantInterface')
->disableOriginalConstructor(true)
->getMock();
$participant->expects($this->any())
->method('getId')
->will($this->returnValue($id));
return $participant;
}
/**
* Returns a thread mock.
*
* @return mixed
*/
protected function createThreadMock()
{
return $this->getMockBuilder('FOS\MessageBundle\Model\ThreadInterface')
->disableOriginalConstructor(true)
->getMock();
}
}
class TestThreadManager extends ThreadManager
{
/**
* Empty constructor.
*/
public function __construct()
{
}
/**
* Make the function public.
*
* @param ThreadInterface $thread
*/
public function doCreatedByAndAt(ThreadInterface $thread)
{
return parent::doCreatedByAndAt($thread);
}
}