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