1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Tests\Entity; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
18
|
|
|
use Doctrine\ORM\AbstractQuery; |
19
|
|
|
use Doctrine\ORM\EntityManager; |
20
|
|
|
use Doctrine\ORM\EntityRepository; |
21
|
|
|
use Doctrine\ORM\QueryBuilder; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
use Sonata\NotificationBundle\Entity\BaseMessage; |
24
|
|
|
use Sonata\NotificationBundle\Entity\MessageManager; |
25
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
26
|
|
|
|
27
|
|
|
class MessageManagerTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
public function testCancel(): void |
30
|
|
|
{ |
31
|
|
|
$manager = $this->getMessageManagerMock(); |
32
|
|
|
|
33
|
|
|
$message = $this->getMessage(); |
34
|
|
|
|
35
|
|
|
$manager->cancel($message); |
36
|
|
|
|
37
|
|
|
$this->assertTrue($message->isCancelled()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testRestart(): void |
41
|
|
|
{ |
42
|
|
|
$manager = $this->getMessageManagerMock(); |
43
|
|
|
|
44
|
|
|
// test un-restartable status |
45
|
|
|
$this->assertNull($manager->restart($this->getMessage(MessageInterface::STATE_OPEN))); |
46
|
|
|
$this->assertNull($manager->restart($this->getMessage(MessageInterface::STATE_CANCELLED))); |
47
|
|
|
$this->assertNull($manager->restart($this->getMessage(MessageInterface::STATE_IN_PROGRESS))); |
48
|
|
|
|
49
|
|
|
$message = $this->getMessage(MessageInterface::STATE_ERROR); |
50
|
|
|
$message->setRestartCount(12); |
51
|
|
|
|
52
|
|
|
$newMessage = $manager->restart($message); |
53
|
|
|
|
54
|
|
|
$this->assertSame(MessageInterface::STATE_OPEN, $newMessage->getState()); |
55
|
|
|
$this->assertSame(13, $newMessage->getRestartCount()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetPager(): void |
59
|
|
|
{ |
60
|
|
|
$self = $this; |
61
|
|
|
$this |
62
|
|
|
->getMessageManager(function ($qb) use ($self): void { |
63
|
|
|
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); |
64
|
|
|
$qb->expects($self->never())->method('andWhere'); |
65
|
|
|
$qb->expects($self->once())->method('setParameters')->with([]); |
66
|
|
|
$qb->expects($self->once())->method('orderBy')->with( |
67
|
|
|
$self->equalTo('m.type'), |
68
|
|
|
$self->equalTo('ASC') |
69
|
|
|
); |
70
|
|
|
}) |
71
|
|
|
->getPager([], 1); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testGetPagerWithInvalidSort(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(\RuntimeException::class); |
77
|
|
|
$this->expectExceptionMessage('Invalid sort field \'invalid\' in \'Sonata\\NotificationBundle\\Entity\\BaseMessage\' class'); |
78
|
|
|
|
79
|
|
|
$self = $this; |
80
|
|
|
$this |
81
|
|
|
->getMessageManager(function ($qb) use ($self): void { |
|
|
|
|
82
|
|
|
}) |
83
|
|
|
->getPager([], 1, 10, ['invalid' => 'ASC']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGetPagerWithMultipleSort(): void |
87
|
|
|
{ |
88
|
|
|
$self = $this; |
89
|
|
|
$this |
90
|
|
|
->getMessageManager(function ($qb) use ($self): void { |
91
|
|
|
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); |
92
|
|
|
$qb->expects($self->never())->method('andWhere'); |
93
|
|
|
$qb->expects($self->once())->method('setParameters')->with([]); |
94
|
|
|
$qb->expects($self->exactly(2))->method('orderBy')->with( |
95
|
|
|
$self->logicalOr( |
96
|
|
|
$self->equalTo('m.type'), |
97
|
|
|
$self->equalTo('m.state') |
98
|
|
|
), |
99
|
|
|
$self->logicalOr( |
100
|
|
|
$self->equalTo('ASC'), |
101
|
|
|
$self->equalTo('DESC') |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo([])); |
105
|
|
|
}) |
106
|
|
|
->getPager([], 1, 10, [ |
107
|
|
|
'type' => 'ASC', |
108
|
|
|
'state' => 'DESC', |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGetPagerWithOpenedMessages(): void |
113
|
|
|
{ |
114
|
|
|
$self = $this; |
115
|
|
|
$this |
116
|
|
|
->getMessageManager(function ($qb) use ($self): void { |
117
|
|
|
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); |
118
|
|
|
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); |
119
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo([ |
120
|
|
|
'state' => MessageInterface::STATE_OPEN, |
121
|
|
|
])); |
122
|
|
|
}) |
123
|
|
|
->getPager(['state' => MessageInterface::STATE_OPEN], 1); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testGetPagerWithCanceledMessages(): void |
127
|
|
|
{ |
128
|
|
|
$self = $this; |
129
|
|
|
$this |
130
|
|
|
->getMessageManager(function ($qb) use ($self): void { |
131
|
|
|
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); |
132
|
|
|
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); |
133
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo([ |
134
|
|
|
'state' => MessageInterface::STATE_CANCELLED, |
135
|
|
|
])); |
136
|
|
|
}) |
137
|
|
|
->getPager(['state' => MessageInterface::STATE_CANCELLED], 1); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testGetPagerWithInProgressMessages(): void |
141
|
|
|
{ |
142
|
|
|
$self = $this; |
143
|
|
|
$this |
144
|
|
|
->getMessageManager(function ($qb) use ($self): void { |
145
|
|
|
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); |
146
|
|
|
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); |
147
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo([ |
148
|
|
|
'state' => MessageInterface::STATE_IN_PROGRESS, |
149
|
|
|
])); |
150
|
|
|
}) |
151
|
|
|
->getPager(['state' => MessageInterface::STATE_IN_PROGRESS], 1); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testFindByTypes(): void |
155
|
|
|
{ |
156
|
|
|
$this |
157
|
|
|
->getMessageManager(function ($qb): void { |
158
|
|
|
$qb->expects($this->never())->method('andWhere'); |
159
|
|
|
$qb->expects($this->once())->method('where')->willReturnSelf(); |
160
|
|
|
$qb->expects($this->once())->method('setParameters') |
161
|
|
|
->with(['state' => MessageInterface::STATE_OPEN]); |
162
|
|
|
$qb->expects($this->once())->method('orderBy')->with( |
163
|
|
|
$this->equalTo('m.createdAt') |
164
|
|
|
)->willReturnSelf(); |
165
|
|
|
}) |
166
|
|
|
->findByTypes([], MessageInterface::STATE_OPEN, 10); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return MessageManagerMock |
171
|
|
|
*/ |
172
|
|
|
protected function getMessageManagerMock() |
173
|
|
|
{ |
174
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
175
|
|
|
|
176
|
|
|
$manager = new MessageManagerMock(Message::class, $registry); |
177
|
|
|
|
178
|
|
|
return $manager; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return MessageManager |
183
|
|
|
*/ |
184
|
|
|
protected function getMessageManager($qbCallback) |
185
|
|
|
{ |
186
|
|
|
$query = $this->getMockForAbstractClass( |
187
|
|
|
AbstractQuery::class, |
188
|
|
|
[], |
189
|
|
|
'', |
190
|
|
|
false, |
191
|
|
|
true, |
192
|
|
|
true, |
193
|
|
|
['execute'] |
194
|
|
|
); |
195
|
|
|
$query->expects($this->any())->method('execute')->will($this->returnValue(true)); |
196
|
|
|
|
197
|
|
|
$qb = $this->getMockBuilder(QueryBuilder::class) |
198
|
|
|
->setConstructorArgs([$this->createMock(EntityManager::class)]) |
199
|
|
|
->getMock(); |
200
|
|
|
|
201
|
|
|
$qb->expects($this->any())->method('select')->will($this->returnValue($qb)); |
202
|
|
|
$qb->expects($this->any())->method('getQuery')->will($this->returnValue($query)); |
203
|
|
|
|
204
|
|
|
$qbCallback($qb); |
205
|
|
|
|
206
|
|
|
$repository = $this->createMock(EntityRepository::class); |
207
|
|
|
$repository->expects($this->any())->method('createQueryBuilder')->will($this->returnValue($qb)); |
208
|
|
|
|
209
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
210
|
|
|
$metadata->expects($this->any())->method('getFieldNames')->will($this->returnValue([ |
211
|
|
|
'state', |
212
|
|
|
'type', |
213
|
|
|
])); |
214
|
|
|
|
215
|
|
|
$em = $this->createMock(EntityManager::class); |
216
|
|
|
$em->expects($this->any())->method('getRepository')->will($this->returnValue($repository)); |
217
|
|
|
$em->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata)); |
218
|
|
|
|
219
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
220
|
|
|
$registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em)); |
221
|
|
|
|
222
|
|
|
return new MessageManager(BaseMessage::class, $registry); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param int $state |
227
|
|
|
* |
228
|
|
|
* @return Message |
229
|
|
|
*/ |
230
|
|
|
protected function getMessage($state = MessageInterface::STATE_OPEN) |
231
|
|
|
{ |
232
|
|
|
$message = new Message(); |
233
|
|
|
|
234
|
|
|
$message->setState($state); |
235
|
|
|
|
236
|
|
|
return $message; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.