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