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