|
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\Backend; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Backend\PostponeRuntimeBackend; |
|
15
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEventInterface; |
|
16
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @covers Sonata\NotificationBundle\Backend\PostponeRuntimeBackend |
|
21
|
|
|
*/ |
|
22
|
|
|
class PostponeRuntimeBackendTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testIteratorContainsPublishedMessages() |
|
25
|
|
|
{ |
|
26
|
|
|
$backend = new PostponeRuntimeBackend($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), true); |
|
27
|
|
|
|
|
28
|
|
|
$messages = array(); |
|
29
|
|
|
|
|
30
|
|
|
$message = $backend->create('foo', array()); |
|
31
|
|
|
$messages[] = $message; |
|
32
|
|
|
$backend->publish($message); |
|
33
|
|
|
|
|
34
|
|
|
$message = $backend->create('bar', array()); |
|
35
|
|
|
$messages[] = $message; |
|
36
|
|
|
$backend->publish($message); |
|
37
|
|
|
|
|
38
|
|
|
$message = $backend->create('baz', array()); |
|
39
|
|
|
$messages[] = $message; |
|
40
|
|
|
$backend->publish($message); |
|
41
|
|
|
|
|
42
|
|
|
$backend->create('not_published', array()); |
|
43
|
|
|
|
|
44
|
|
|
$iterator = $backend->getIterator(); |
|
45
|
|
|
foreach ($iterator as $eachKey => $eachMessage) { |
|
46
|
|
|
$this->assertSame($messages[$eachKey], $eachMessage); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testNoMessagesOnEvent() |
|
51
|
|
|
{ |
|
52
|
|
|
$backend = $this->getMock('Sonata\NotificationBundle\Backend\PostponeRuntimeBackend', array('handle'), array($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), true)); |
|
53
|
|
|
$backend |
|
54
|
|
|
->expects($this->never()) |
|
55
|
|
|
->method('handle') |
|
56
|
|
|
; |
|
57
|
|
|
|
|
58
|
|
|
$backend->onEvent(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testLiveEnvironment() |
|
62
|
|
|
{ |
|
63
|
|
|
$dispatcher = new EventDispatcher(); |
|
64
|
|
|
$backend = new PostponeRuntimeBackend($dispatcher, true); |
|
65
|
|
|
$dispatcher->addListener('kernel.terminate', array($backend, 'onEvent')); |
|
66
|
|
|
|
|
67
|
|
|
$message = $backend->create('notification.demo', array()); |
|
68
|
|
|
$backend->publish($message); |
|
69
|
|
|
|
|
70
|
|
|
// This message will not be handled. |
|
71
|
|
|
$backend->create('notification.demo', array()); |
|
72
|
|
|
|
|
73
|
|
|
$phpunit = $this; |
|
74
|
|
|
$phpunit->passed = false; |
|
|
|
|
|
|
75
|
|
|
$dispatcher->addListener('notification.demo', function (ConsumerEventInterface $event) use ($phpunit, $message) { |
|
76
|
|
|
$phpunit->assertSame($message, $event->getMessage()); |
|
77
|
|
|
|
|
78
|
|
|
$phpunit->passed = true; |
|
79
|
|
|
}); |
|
80
|
|
|
|
|
81
|
|
|
$dispatcher->dispatch('kernel.terminate'); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertTrue($phpunit->passed); |
|
84
|
|
|
$this->assertEquals(MessageInterface::STATE_DONE, $message->getState()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testRecursiveMessage() |
|
88
|
|
|
{ |
|
89
|
|
|
$dispatcher = new EventDispatcher(); |
|
90
|
|
|
$backend = new PostponeRuntimeBackend($dispatcher, true); |
|
91
|
|
|
$dispatcher->addListener('kernel.terminate', array($backend, 'onEvent')); |
|
92
|
|
|
|
|
93
|
|
|
$message1 = $backend->create('notification.demo1', array()); |
|
94
|
|
|
$message2 = $backend->create('notification.demo2', array()); |
|
95
|
|
|
|
|
96
|
|
|
$backend->publish($message1); |
|
97
|
|
|
|
|
98
|
|
|
$phpunit = $this; |
|
99
|
|
|
$phpunit->passed1 = false; |
|
|
|
|
|
|
100
|
|
|
$phpunit->passed2 = false; |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$dispatcher->addListener('notification.demo1', function (ConsumerEventInterface $event) use ($phpunit, $message1, $message2, $backend, $dispatcher) { |
|
103
|
|
|
$phpunit->assertSame($message1, $event->getMessage()); |
|
104
|
|
|
|
|
105
|
|
|
$phpunit->passed1 = true; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$backend->publish($message2); |
|
108
|
|
|
}); |
|
109
|
|
|
|
|
110
|
|
|
$dispatcher->addListener('notification.demo2', function (ConsumerEventInterface $event) use ($phpunit, $message2) { |
|
111
|
|
|
$phpunit->assertSame($message2, $event->getMessage()); |
|
112
|
|
|
|
|
113
|
|
|
$phpunit->passed2 = true; |
|
|
|
|
|
|
114
|
|
|
}); |
|
115
|
|
|
|
|
116
|
|
|
$dispatcher->dispatch('kernel.terminate'); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertTrue($phpunit->passed1); |
|
|
|
|
|
|
119
|
|
|
$this->assertTrue($phpunit->passed2); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
$this->assertEquals(MessageInterface::STATE_DONE, $message1->getState()); |
|
122
|
|
|
$this->assertEquals(MessageInterface::STATE_DONE, $message2->getState()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testStatusIsOk() |
|
126
|
|
|
{ |
|
127
|
|
|
if (!class_exists('ZendDiagnostics\Result\Success')) { |
|
128
|
|
|
$this->markTestSkipped('The class ZendDiagnostics\Result\Success does not exist'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$backend = new PostponeRuntimeBackend($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), true); |
|
132
|
|
|
|
|
133
|
|
|
$status = $backend->getStatus(); |
|
134
|
|
|
$this->assertInstanceOf('ZendDiagnostics\Result\Success', $status); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testOnCliPublishHandlesDirectly() |
|
138
|
|
|
{ |
|
139
|
|
|
$backend = $this->getMock('Sonata\NotificationBundle\Backend\PostponeRuntimeBackend', array('handle'), array($this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'), false)); |
|
140
|
|
|
$backend |
|
141
|
|
|
->expects($this->once()) |
|
142
|
|
|
->method('handle') |
|
143
|
|
|
; |
|
144
|
|
|
|
|
145
|
|
|
$message = $backend->create('notification.demo', array()); |
|
146
|
|
|
$backend->publish($message); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: