|
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\Notification; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Backend\MessageManagerBackend; |
|
15
|
|
|
use Sonata\NotificationBundle\Exception\HandlingException; |
|
16
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
17
|
|
|
use Sonata\NotificationBundle\Tests\Entity\Message; |
|
18
|
|
|
use ZendDiagnostics\Result\Failure; |
|
19
|
|
|
use ZendDiagnostics\Result\Success; |
|
20
|
|
|
use ZendDiagnostics\Result\Warning; |
|
21
|
|
|
|
|
22
|
|
|
class MessageManagerBackendTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testCreateAndPublish() |
|
25
|
|
|
{ |
|
26
|
|
|
$message = new Message(); |
|
27
|
|
|
$modelManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface'); |
|
28
|
|
|
$modelManager->expects($this->once())->method('save')->will($this->returnValue($message)); |
|
29
|
|
|
$modelManager->expects($this->once())->method('create')->will($this->returnValue($message)); |
|
30
|
|
|
|
|
31
|
|
|
$backend = new MessageManagerBackend($modelManager, array()); |
|
32
|
|
|
$message = $backend->createAndPublish('foo', array('message' => 'salut')); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertInstanceOf('Sonata\\NotificationBundle\\Model\\MessageInterface', $message); |
|
35
|
|
|
$this->assertEquals(MessageInterface::STATE_OPEN, $message->getState()); |
|
36
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
|
37
|
|
|
$this->assertEquals('foo', $message->getType()); |
|
38
|
|
|
$this->assertEquals(array('message' => 'salut'), $message->getBody()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testHandleSuccess() |
|
42
|
|
|
{ |
|
43
|
|
|
$message = new Message(); |
|
44
|
|
|
$modelManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface'); |
|
45
|
|
|
$modelManager->expects($this->exactly(2))->method('save')->will($this->returnValue($message)); |
|
46
|
|
|
|
|
47
|
|
|
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
|
48
|
|
|
$dispatcher->expects($this->once())->method('dispatch'); |
|
49
|
|
|
|
|
50
|
|
|
$backend = new MessageManagerBackend($modelManager, array()); |
|
51
|
|
|
|
|
52
|
|
|
$backend->handle($message, $dispatcher); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals(MessageInterface::STATE_DONE, $message->getState()); |
|
55
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
|
56
|
|
|
$this->assertNotNull($message->getCompletedAt()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testHandleError() |
|
60
|
|
|
{ |
|
61
|
|
|
$message = new Message(); |
|
62
|
|
|
$modelManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface'); |
|
63
|
|
|
$modelManager->expects($this->exactly(2))->method('save')->will($this->returnValue($message)); |
|
64
|
|
|
|
|
65
|
|
|
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
|
66
|
|
|
$dispatcher->expects($this->once())->method('dispatch')->will($this->throwException(new \RuntimeException())); |
|
67
|
|
|
$backend = new MessageManagerBackend($modelManager, array()); |
|
68
|
|
|
|
|
69
|
|
|
$e = false; |
|
70
|
|
|
try { |
|
71
|
|
|
$backend->handle($message, $dispatcher); |
|
72
|
|
|
} catch (HandlingException $e) { |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->assertInstanceOf('Sonata\NotificationBundle\Exception\HandlingException', $e); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals(MessageInterface::STATE_ERROR, $message->getState()); |
|
78
|
|
|
$this->assertNotNull($message->getCreatedAt()); |
|
79
|
|
|
$this->assertNotNull($message->getCompletedAt()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @dataProvider statusProvider |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testStatus($counts, $expectedStatus, $message) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!class_exists('ZendDiagnostics\Result\Success')) { |
|
88
|
|
|
$this->markTestSkipped('The class ZendDiagnostics\Result\Success does not exist'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$modelManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface'); |
|
92
|
|
|
$modelManager->expects($this->exactly(1))->method('countStates')->will($this->returnValue($counts)); |
|
93
|
|
|
|
|
94
|
|
|
$backend = new MessageManagerBackend($modelManager, array( |
|
95
|
|
|
MessageInterface::STATE_IN_PROGRESS => 10, |
|
96
|
|
|
MessageInterface::STATE_ERROR => 30, |
|
97
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
98
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
99
|
|
|
)); |
|
100
|
|
|
|
|
101
|
|
|
$status = $backend->getStatus(); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertInstanceOf(get_class($expectedStatus), $status); |
|
104
|
|
|
$this->assertEquals($message, $status->getMessage()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function statusProvider() |
|
108
|
|
|
{ |
|
109
|
|
|
if (!class_exists('ZendDiagnostics\Result\Success')) { |
|
110
|
|
|
return array(array(1, 1, 1)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$data = array(); |
|
114
|
|
|
|
|
115
|
|
|
$data[] = array( |
|
116
|
|
|
array( |
|
117
|
|
|
MessageInterface::STATE_IN_PROGRESS => 11, #here |
|
118
|
|
|
MessageInterface::STATE_ERROR => 31, |
|
119
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
120
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
121
|
|
|
), |
|
122
|
|
|
new Failure(), |
|
123
|
|
|
'Too many messages processed at the same time (Database)', |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$data[] = array( |
|
127
|
|
|
array( |
|
128
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
129
|
|
|
MessageInterface::STATE_ERROR => 31, #here |
|
130
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
131
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
132
|
|
|
), |
|
133
|
|
|
new Failure(), |
|
134
|
|
|
'Too many errors (Database)', |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
$data[] = array( |
|
138
|
|
|
array( |
|
139
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
140
|
|
|
MessageInterface::STATE_ERROR => 1, |
|
141
|
|
|
MessageInterface::STATE_OPEN => 101, #here |
|
142
|
|
|
MessageInterface::STATE_DONE => 10000, |
|
143
|
|
|
), |
|
144
|
|
|
new Warning(), |
|
145
|
|
|
'Too many messages waiting to be processed (Database)', |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
$data[] = array( |
|
149
|
|
|
array( |
|
150
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
151
|
|
|
MessageInterface::STATE_ERROR => 1, |
|
152
|
|
|
MessageInterface::STATE_OPEN => 100, |
|
153
|
|
|
MessageInterface::STATE_DONE => 10001, #here |
|
154
|
|
|
), |
|
155
|
|
|
new Warning(), |
|
156
|
|
|
'Too many processed messages, please clean the database (Database)', |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
$data[] = array( |
|
160
|
|
|
array( |
|
161
|
|
|
MessageInterface::STATE_IN_PROGRESS => 1, |
|
162
|
|
|
MessageInterface::STATE_ERROR => 1, |
|
163
|
|
|
MessageInterface::STATE_OPEN => 1, |
|
164
|
|
|
MessageInterface::STATE_DONE => 1, |
|
165
|
|
|
), |
|
166
|
|
|
new Success(), |
|
167
|
|
|
'Ok (Database)', |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return $data; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|