Completed
Push — 3.x ( 66771e...b01f9e )
by Jordi Sala
01:55
created

SonataNotificationExtensionTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 202
rs 10
c 0
b 0
f 0

12 Methods

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