1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata 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
|
|
|
/** |
49
|
|
|
* @return \Sonata\NotificationBundle\Tests\Entity\MessageManagerMock |
50
|
|
|
*/ |
51
|
|
|
protected function getMessageManagerMock() |
52
|
|
|
{ |
53
|
|
|
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); |
54
|
|
|
|
55
|
|
|
$manager = new MessageManagerMock('Sonata\notificationBundle\Tests\Entity\Message', $registry); |
56
|
|
|
|
57
|
|
|
return $manager; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return \Sonata\NotificationBundle\Entity\MessageManager |
62
|
|
|
*/ |
63
|
|
|
protected function getMessageManager($qbCallback) |
64
|
|
|
{ |
65
|
|
|
$query = $this->getMockForAbstractClass('Doctrine\ORM\AbstractQuery', array(), '', false, true, true, array('execute')); |
66
|
|
|
$query->expects($this->any())->method('execute')->will($this->returnValue(true)); |
67
|
|
|
|
68
|
|
|
$qb = $this->getMock('Doctrine\ORM\QueryBuilder', array(), array( |
69
|
|
|
$this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(), |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
$qb->expects($this->any())->method('select')->will($this->returnValue($qb)); |
73
|
|
|
$qb->expects($this->any())->method('getQuery')->will($this->returnValue($query)); |
74
|
|
|
|
75
|
|
|
$qbCallback($qb); |
76
|
|
|
|
77
|
|
|
$repository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock(); |
78
|
|
|
$repository->expects($this->any())->method('createQueryBuilder')->will($this->returnValue($qb)); |
79
|
|
|
|
80
|
|
|
$metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
81
|
|
|
$metadata->expects($this->any())->method('getFieldNames')->will($this->returnValue(array( |
82
|
|
|
'state', |
83
|
|
|
'type', |
84
|
|
|
))); |
85
|
|
|
|
86
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
87
|
|
|
$em->expects($this->any())->method('getRepository')->will($this->returnValue($repository)); |
88
|
|
|
$em->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata)); |
89
|
|
|
|
90
|
|
|
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); |
91
|
|
|
$registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em)); |
92
|
|
|
|
93
|
|
|
return new MessageManager('Sonata\NotificationBundle\Entity\BaseMessage', $registry); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return \Sonata\NotificationBundle\Tests\Entity\Message |
98
|
|
|
*/ |
99
|
|
|
protected function getMessage($state = MessageInterface::STATE_OPEN) |
100
|
|
|
{ |
101
|
|
|
$message = new Message(); |
102
|
|
|
|
103
|
|
|
$message->setState($state); |
104
|
|
|
|
105
|
|
|
return $message; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetPager() |
109
|
|
|
{ |
110
|
|
|
$self = $this; |
111
|
|
|
$this |
112
|
|
|
->getMessageManager(function ($qb) use ($self) { |
113
|
|
|
$qb->expects($self->never())->method('andWhere'); |
114
|
|
|
$qb->expects($self->once())->method('setParameters')->with(array()); |
115
|
|
|
$qb->expects($self->once())->method('orderBy')->with( |
116
|
|
|
$self->equalTo('m.type'), |
117
|
|
|
$self->equalTo('ASC') |
118
|
|
|
); |
119
|
|
|
}) |
120
|
|
|
->getPager(array(), 1); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @expectedException \RuntimeException |
125
|
|
|
* @expectedExceptionMessage Invalid sort field 'invalid' in 'Sonata\NotificationBundle\Entity\BaseMessage' class |
126
|
|
|
*/ |
127
|
|
|
public function testGetPagerWithInvalidSort() |
128
|
|
|
{ |
129
|
|
|
$self = $this; |
130
|
|
|
$this |
131
|
|
|
->getMessageManager(function ($qb) use ($self) { }) |
|
|
|
|
132
|
|
|
->getPager(array(), 1, 10, array('invalid' => 'ASC')); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGetPagerWithMultipleSort() |
136
|
|
|
{ |
137
|
|
|
$self = $this; |
138
|
|
|
$this |
139
|
|
|
->getMessageManager(function ($qb) use ($self) { |
140
|
|
|
$qb->expects($self->never())->method('andWhere'); |
141
|
|
|
$qb->expects($self->once())->method('setParameters')->with(array()); |
142
|
|
|
$qb->expects($self->exactly(2))->method('orderBy')->with( |
143
|
|
|
$self->logicalOr( |
144
|
|
|
$self->equalTo('m.type'), |
145
|
|
|
$self->equalTo('m.state') |
146
|
|
|
), |
147
|
|
|
$self->logicalOr( |
148
|
|
|
$self->equalTo('ASC'), |
149
|
|
|
$self->equalTo('DESC') |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo(array())); |
153
|
|
|
}) |
154
|
|
|
->getPager(array(), 1, 10, array( |
155
|
|
|
'type' => 'ASC', |
156
|
|
|
'state' => 'DESC', |
157
|
|
|
)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testGetPagerWithOpenedMessages() |
161
|
|
|
{ |
162
|
|
|
$self = $this; |
163
|
|
|
$this |
164
|
|
|
->getMessageManager(function ($qb) use ($self) { |
165
|
|
|
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); |
166
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo(array('state' => MessageInterface::STATE_OPEN))); |
167
|
|
|
}) |
168
|
|
|
->getPager(array('state' => MessageInterface::STATE_OPEN), 1); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetPagerWithCanceledMessages() |
172
|
|
|
{ |
173
|
|
|
$self = $this; |
174
|
|
|
$this |
175
|
|
|
->getMessageManager(function ($qb) use ($self) { |
176
|
|
|
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); |
177
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo(array('state' => MessageInterface::STATE_CANCELLED))); |
178
|
|
|
}) |
179
|
|
|
->getPager(array('state' => MessageInterface::STATE_CANCELLED), 1); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testGetPagerWithInProgressMessages() |
183
|
|
|
{ |
184
|
|
|
$self = $this; |
185
|
|
|
$this |
186
|
|
|
->getMessageManager(function ($qb) use ($self) { |
187
|
|
|
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); |
188
|
|
|
$qb->expects($self->once())->method('setParameters')->with($self->equalTo(array('state' => MessageInterface::STATE_IN_PROGRESS))); |
189
|
|
|
}) |
190
|
|
|
->getPager(array('state' => MessageInterface::STATE_IN_PROGRESS), 1); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.