|
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\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; |
|
15
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
16
|
|
|
use Http\HttplugBundle\HttplugBundle; |
|
17
|
|
|
use Liip\MonitorBundle\LiipMonitorBundle; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Sonata\NotificationBundle\DependencyInjection\Compiler\NotificationCompilerPass; |
|
20
|
|
|
use Sonata\NotificationBundle\DependencyInjection\SonataNotificationExtension; |
|
21
|
|
|
use Sonata\NotificationBundle\SonataNotificationBundle; |
|
22
|
|
|
use Symfony\Bridge\Monolog\Logger; |
|
23
|
|
|
use Symfony\Bundle\MonologBundle\MonologBundle; |
|
24
|
|
|
use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
26
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
27
|
|
|
|
|
28
|
|
|
class SonataNotificationExtensionTest extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ContainerBuilder |
|
32
|
|
|
*/ |
|
33
|
|
|
private $container; |
|
34
|
|
|
|
|
35
|
|
|
protected function tearDown() |
|
36
|
|
|
{ |
|
37
|
|
|
unset($this->container); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testEmptyConfig() |
|
41
|
|
|
{ |
|
42
|
|
|
$container = $this->getContainerBuilder([ |
|
43
|
|
|
'MonologBundle' => MonologBundle::class, |
|
44
|
|
|
'SwiftmailerBundle' => SwiftmailerBundle::class, |
|
45
|
|
|
]); |
|
46
|
|
|
$extension = new SonataNotificationExtension(); |
|
47
|
|
|
$extension->load([], $container); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertAlias('sonata.notification.backend.runtime', 'sonata.notification.backend'); |
|
50
|
|
|
$this->assertHasDefinition('sonata.notification.consumer.swift_mailer'); |
|
51
|
|
|
$this->assertHasDefinition('sonata.notification.consumer.logger'); |
|
52
|
|
|
$this->assertParameter('sonata.notification.backend.runtime', 'sonata.notification.backend'); |
|
53
|
|
|
|
|
54
|
|
|
$container->compile(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testDoNotRegisterDefaultConsumers() |
|
58
|
|
|
{ |
|
59
|
|
|
$container = $this->getContainerBuilder(); |
|
60
|
|
|
$extension = new SonataNotificationExtension(); |
|
61
|
|
|
$extension->load([ |
|
62
|
|
|
[ |
|
63
|
|
|
'consumers' => [ |
|
64
|
|
|
'register_default' => false, |
|
65
|
|
|
], |
|
66
|
|
|
], |
|
67
|
|
|
], $container); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertHasNoDefinition('sonata.notification.consumer.swift_mailer'); |
|
70
|
|
|
$this->assertHasNoDefinition('sonata.notification.consumer.logger'); |
|
71
|
|
|
$this->assertHasNoDefinition('sonata.notification.manager.message.default'); |
|
72
|
|
|
$this->assertHasNoDefinition('sonata.notification.erroneous_messages_selector'); |
|
73
|
|
|
$this->assertHasNoDefinition('sonata.notification.event.doctrine_optimize'); |
|
74
|
|
|
$this->assertHasNoDefinition('sonata.notification.event.doctrine_backend_optimize'); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertParameter([], 'sonata.notification.event.iteration_listeners'); |
|
77
|
|
|
|
|
78
|
|
|
$container->compile(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testDoctrineBackendNoConfig() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->expectException(\RuntimeException::class); |
|
84
|
|
|
$this->expectExceptionMessage('Please configure the sonata_notification.backends.doctrine section'); |
|
85
|
|
|
|
|
86
|
|
|
$container = $this->getContainerBuilder([ |
|
87
|
|
|
'DoctrineBundle' => DoctrineBundle::class, |
|
88
|
|
|
]); |
|
89
|
|
|
$extension = new SonataNotificationExtension(); |
|
90
|
|
|
|
|
91
|
|
|
$extension->load([ |
|
92
|
|
|
[ |
|
93
|
|
|
'backend' => 'sonata.notification.backend.doctrine', |
|
94
|
|
|
], |
|
95
|
|
|
], $container); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testDoctrineBackend() |
|
99
|
|
|
{ |
|
100
|
|
|
$container = $this->getContainerBuilder([ |
|
101
|
|
|
'DoctrineBundle' => DoctrineBundle::class, |
|
102
|
|
|
]); |
|
103
|
|
|
$extension = new SonataNotificationExtension(); |
|
104
|
|
|
$extension->load([ |
|
105
|
|
|
[ |
|
106
|
|
|
'backend' => 'sonata.notification.backend.doctrine', |
|
107
|
|
|
'backends' => [ |
|
108
|
|
|
'doctrine' => null, |
|
109
|
|
|
], |
|
110
|
|
|
'consumers' => [ |
|
111
|
|
|
'register_default' => false, |
|
112
|
|
|
], |
|
113
|
|
|
], |
|
114
|
|
|
], $container); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertHasDefinition('sonata.notification.manager.message.default'); |
|
117
|
|
|
$this->assertHasDefinition('sonata.notification.erroneous_messages_selector'); |
|
118
|
|
|
$this->assertHasDefinition('sonata.notification.event.doctrine_optimize'); |
|
119
|
|
|
$this->assertHasDefinition('sonata.notification.event.doctrine_backend_optimize'); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertParameter( |
|
122
|
|
|
['sonata.notification.event.doctrine_backend_optimize'], |
|
123
|
|
|
'sonata.notification.event.iteration_listeners' |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$container->compile(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testRabbitMQBackendNoConfig() |
|
130
|
|
|
{ |
|
131
|
|
|
$this->expectException(\RuntimeException::class); |
|
132
|
|
|
$this->expectExceptionMessage('Please configure the sonata_notification.backends.rabbitmq section'); |
|
133
|
|
|
|
|
134
|
|
|
$container = $this->getContainerBuilder(); |
|
135
|
|
|
$extension = new SonataNotificationExtension(); |
|
136
|
|
|
|
|
137
|
|
|
$extension->load([ |
|
138
|
|
|
[ |
|
139
|
|
|
'backend' => 'sonata.notification.backend.rabbitmq', |
|
140
|
|
|
], |
|
141
|
|
|
], $container); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testRabbitMQBackend() |
|
145
|
|
|
{ |
|
146
|
|
|
$container = $this->getContainerBuilder(); |
|
147
|
|
|
$extension = new SonataNotificationExtension(); |
|
148
|
|
|
$extension->load([ |
|
149
|
|
|
[ |
|
150
|
|
|
'backend' => 'sonata.notification.backend.rabbitmq', |
|
151
|
|
|
'backends' => [ |
|
152
|
|
|
'rabbitmq' => [ |
|
153
|
|
|
'exchange' => 'logs', |
|
154
|
|
|
], |
|
155
|
|
|
], |
|
156
|
|
|
'consumers' => [ |
|
157
|
|
|
'register_default' => false, |
|
158
|
|
|
], |
|
159
|
|
|
], |
|
160
|
|
|
], $container); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertHasNoDefinition('sonata.notification.manager.message.default'); |
|
163
|
|
|
$this->assertHasNoDefinition('sonata.notification.erroneous_messages_selector'); |
|
164
|
|
|
$this->assertHasNoDefinition('sonata.notification.event.doctrine_optimize'); |
|
165
|
|
|
$this->assertHasNoDefinition('sonata.notification.event.doctrine_backend_optimize'); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertParameter([], 'sonata.notification.event.iteration_listeners'); |
|
168
|
|
|
|
|
169
|
|
|
$container->compile(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function testRabbitMQMonitoringProviders() |
|
173
|
|
|
{ |
|
174
|
|
|
$this->expectException(ServiceNotFoundException::class); |
|
175
|
|
|
|
|
176
|
|
|
$container = $this->getContainerBuilder([ |
|
177
|
|
|
'LiipMonitorBundle' => LiipMonitorBundle::class, |
|
178
|
|
|
'HttplugBundle' => HttplugBundle::class, |
|
179
|
|
|
]); |
|
180
|
|
|
$extension = new SonataNotificationExtension(); |
|
181
|
|
|
$extension->load([ |
|
182
|
|
|
[ |
|
183
|
|
|
'backend' => 'sonata.notification.backend.rabbitmq', |
|
184
|
|
|
'backends' => [ |
|
185
|
|
|
'rabbitmq' => [ |
|
186
|
|
|
'exchange' => 'logs', |
|
187
|
|
|
], |
|
188
|
|
|
], |
|
189
|
|
|
'consumers' => [ |
|
190
|
|
|
'register_default' => false, |
|
191
|
|
|
], |
|
192
|
|
|
], |
|
193
|
|
|
], $container); |
|
194
|
|
|
|
|
195
|
|
|
// NEXT_MAJOR: remove this assert |
|
196
|
|
|
$this->assertHasDefinition('sonata.notification.rabbitmq.queue_status.guzzle_provider'); |
|
197
|
|
|
|
|
198
|
|
|
$this->assertHasDefinition('sonata.notification.rabbitmq.queue_status.http_provider'); |
|
199
|
|
|
$this->assertHasDefinition('sonata.notification.backend.heath_check'); |
|
200
|
|
|
|
|
201
|
|
|
$container->compile(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
private function getContainerBuilder(array $bundles = []) |
|
205
|
|
|
{ |
|
206
|
|
|
$container = new ContainerBuilder(); |
|
207
|
|
|
|
|
208
|
|
|
$containerBundles = array_merge( |
|
209
|
|
|
['SonataNotificationBundle' => SonataNotificationBundle::class], $bundles |
|
210
|
|
|
); |
|
211
|
|
|
$container->setParameter('kernel.bundles', $containerBundles); |
|
212
|
|
|
|
|
213
|
|
|
$container->addCompilerPass(new NotificationCompilerPass()); |
|
214
|
|
|
|
|
215
|
|
|
if (isset($containerBundles['MonologBundle'])) { |
|
216
|
|
|
$container->register('logger') |
|
217
|
|
|
->setClass(Logger::class) |
|
218
|
|
|
->setPublic(true); |
|
219
|
|
|
} |
|
220
|
|
|
if (isset($containerBundles['SwiftmailerBundle'])) { |
|
221
|
|
|
$container->register('mailer') |
|
222
|
|
|
->setClass(\Swift_Mailer::class) |
|
223
|
|
|
->setPublic(true); |
|
224
|
|
|
} |
|
225
|
|
|
if (isset($containerBundles['DoctrineBundle'])) { |
|
226
|
|
|
$container->register('doctrine') |
|
227
|
|
|
->setClass(Registry::class) |
|
228
|
|
|
->setPublic(true); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $this->container = $container; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
private function assertAlias($alias, $service) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->assertSame( |
|
237
|
|
|
$alias, (string) $this->container->getAlias($service), sprintf('%s alias is correct', $service) |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
private function assertParameter($expectedValue, $name) |
|
242
|
|
|
{ |
|
243
|
|
|
$this->assertSame( |
|
244
|
|
|
$expectedValue, $this->container->getParameter($name), sprintf('%s parameter is correct', $name) |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
private function assertHasDefinition($definition) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->assertTrue( |
|
251
|
|
|
$this->container->hasDefinition($definition) ? true : $this->container->hasAlias($definition) |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
private function assertHasNoDefinition($service) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->assertFalse( |
|
258
|
|
|
$this->container->hasDefinition($service) ? true : $this->container->hasAlias($service) |
|
259
|
|
|
); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|