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