Completed
Push — master ( f2406b...f65e35 )
by Kevin
07:20 queued 06:04
created

testDisabledDefaultMailer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Liip\MonitorBundle\Tests\DependencyInjection;
4
5
use Liip\MonitorBundle\DependencyInjection\Compiler\AddGroupsCompilerPass;
6
use Liip\MonitorBundle\DependencyInjection\Compiler\CheckCollectionTagCompilerPass;
7
use Liip\MonitorBundle\DependencyInjection\Compiler\CheckTagCompilerPass;
8
use Liip\MonitorBundle\DependencyInjection\Compiler\GroupRunnersCompilerPass;
9
use Liip\MonitorBundle\DependencyInjection\LiipMonitorExtension;
10
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
11
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
class LiipMonitorExtensionTest extends AbstractExtensionTestCase
17
{
18
    protected function setUp(): void
19
    {
20
        parent::setUp();
21
22
        $doctrineMock = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry')->getMock();
23
        $this->container->set('doctrine', $doctrineMock);
24
        $this->container->addCompilerPass(new AddGroupsCompilerPass());
25
        $this->container->addCompilerPass(new GroupRunnersCompilerPass());
26
        $this->container->addCompilerPass(new CheckTagCompilerPass());
27
        $this->container->addCompilerPass(new CheckCollectionTagCompilerPass());
28
    }
29
30
    /**
31
     * @dataProvider checkProvider
32
     */
33
    public function testChecksLoaded($name, $config, $checkClass, $checkAlias = null, $checkCount = 1)
34
    {
35
        // skip checks for missing classes
36
        if (!class_exists($checkClass)) {
37
            $this->setExpectedException('InvalidArgumentException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not exist on Liip\MonitorBundle\Tests...iipMonitorExtensionTest. Did you maybe mean setExpectedExceptionFromAnnotation()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
        }
39
40
        if (!$checkAlias) {
41
            $checkAlias = $name;
42
        }
43
44
        $this->load(['checks' => [$name => $config]]);
45
        $this->compile();
46
47
        $runner = $this->container->get('liip_monitor.runner');
48
49
        $this->assertCount($checkCount, $runner->getChecks());
50
        $this->assertInstanceOf($checkClass, $runner->getCheck($checkAlias));
51
    }
52
53
    public function testDefaultNoChecks()
54
    {
55
        $this->load();
56
        $this->compile();
57
58
        $this->assertCount(0, $this->container->get('liip_monitor.runner')->getChecks());
59
    }
60
61
    public function testDefaultGroupParameterHasNoChecks()
62
    {
63
        $this->load();
64
        $this->compile();
65
66
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
67
        $this->assertSame('default', $this->container->getParameter('liip_monitor.default_group'));
68
    }
69
70 View Code Duplication
    public function testDefaultGroupParameter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $this->load(['checks' => ['php_extensions' => ['foo']]]);
73
        $this->compile();
74
75
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
76
        $this->assertSame('default', $this->container->getParameter('liip_monitor.default_group'));
77
    }
78
79 View Code Duplication
    public function testDefaultGroupParameterCustom()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $this->load(['checks' => ['php_extensions' => ['foo']], 'default_group' => 'foo_bar']);
82
        $this->compile();
83
84
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
85
        $this->assertSame('foo_bar', $this->container->getParameter('liip_monitor.default_group'));
86
    }
87
88
    public function testEnableController()
89
    {
90
        $this->load();
91
92
        $this->assertFalse($this->container->has('liip_monitor.health_controller'));
93
94
        $this->load(['enable_controller' => true]);
95
96
        $this->assertTrue($this->container->has('liip_monitor.health_controller'));
97
    }
98
99
    public function testDisabledDefaultMailer()
100
    {
101
        $this->load();
102
103
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.enabled'));
104
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.recipient'));
105
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.sender'));
106
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.subject'));
107
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.send_on_warning'));
108
    }
109
110
    public function testDisabledMailer()
111
    {
112
        $this->load(
113
            [
114
                'mailer' => [
115
                    'enabled' => false,
116
                    'recipient' => '[email protected]',
117
                    'sender' => '[email protected]',
118
                    'subject' => 'Health Check',
119
                    'send_on_warning' => true,
120
                ],
121
            ]
122
        );
123
124
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.enabled'));
125
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.recipient'));
126
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.sender'));
127
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.subject'));
128
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.send_on_warning'));
129
    }
130
131
    public function testEnabledMailer()
132
    {
133
        $this->load(
134
            [
135
                'mailer' => [
136
                    'enabled' => true,
137
                    'recipient' => '[email protected]',
138
                    'sender' => '[email protected]',
139
                    'subject' => 'Health Check',
140
                    'send_on_warning' => true,
141
                ],
142
            ]
143
        );
144
145
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.enabled', true);
146
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.recipient', ['[email protected]']);
147
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.sender', '[email protected]');
148
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.subject', 'Health Check');
149
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.send_on_warning', true);
150
    }
151
152
    /**
153
     * @dataProvider mailerConfigProvider
154
     */
155
    public function testInvalidMailerConfig($config)
156
    {
157
        $this->expectException(InvalidConfigurationException::class);
158
159
        $this->load($config);
160
    }
161
162
    public function mailerConfigProvider()
163
    {
164
        return [
165
            [
166
                [
167
                    'mailer' => [
168
                        'recipient' => '[email protected]',
169
                    ],
170
                ],
171
            ],
172
            [
173
                [
174
                    'mailer' => [
175
                        'recipient' => '[email protected]',
176
                        'sender' => '[email protected]',
177
                        'subject' => null,
178
                    ],
179
                ],
180
            ],
181
        ];
182
    }
183
184
    /**
185
     * @dataProvider invalidCheckProvider
186
     */
187
    public function testInvalidExpressionConfig(array $config)
188
    {
189
        $this->expectException(InvalidConfigurationException::class);
190
191
        $this->load(['checks' => ['expressions' => $config]]);
192
        $this->compile();
193
    }
194
195
    public function invalidCheckProvider()
196
    {
197
        return [
198
            [['foo']],
199
            [['foo' => ['critical_expression' => 'true']]],
200
            [['foo' => ['label' => 'foo']]],
201
        ];
202
    }
203
204
    public function checkProvider()
205
    {
206
        return [
207
            ['php_extensions', ['foo'], 'ZendDiagnostics\Check\ExtensionLoaded'],
208
            ['php_flags', ['foo' => 'true'], 'ZendDiagnostics\Check\PhpFlag', 'php_flag_foo'],
209
            ['php_version', ['5.3.3' => '='], 'ZendDiagnostics\Check\PhpVersion', 'php_version_5.3.3'],
210
            ['process_running', 'foo', 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running'],
211
            ['process_running', ['foo'], 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running'],
212
            ['process_running', ['foo', 'bar'], 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running', 2],
213
            ['process_running', ['foo', 'bar'], 'ZendDiagnostics\Check\ProcessRunning', 'process_bar_running', 2],
214
            ['readable_directory', ['foo'], 'ZendDiagnostics\Check\DirReadable'],
215
            ['writable_directory', ['foo'], 'ZendDiagnostics\Check\DirWritable'],
216
            ['class_exists', ['Foo'], 'ZendDiagnostics\Check\ClassExists'],
217
            ['cpu_performance', 0.5, 'ZendDiagnostics\Check\CpuPerformance'],
218
            ['disk_usage', ['path' => __DIR__], 'ZendDiagnostics\Check\DiskUsage'],
219
            ['symfony_requirements', ['file' => __DIR__.'/../../LiipMonitorBundle.php'], 'Liip\MonitorBundle\Check\SymfonyRequirements'],
220
            ['opcache_memory', null, 'ZendDiagnostics\Check\OpCacheMemory'],
221
            ['apc_memory', null, 'ZendDiagnostics\Check\ApcMemory'],
222
            ['apc_fragmentation', null, 'ZendDiagnostics\Check\ApcFragmentation'],
223
            ['doctrine_dbal', 'foo', 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection'],
224
            ['doctrine_dbal', ['foo'], 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection'],
225
            ['doctrine_dbal', ['foo', 'bar'], 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection', 2],
226
            ['doctrine_dbal', ['foo', 'bar'], 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_bar_connection', 2],
227
            ['memcache', ['foo' => null], 'ZendDiagnostics\Check\Memcache', 'memcache_foo'],
228
            ['redis', ['foo' => null], 'ZendDiagnostics\Check\Redis', 'redis_foo'],
229
            ['http_service', ['foo' => null], 'ZendDiagnostics\Check\HttpService', 'http_service_foo'],
230
            ['guzzle_http_service', ['foo' => null], 'ZendDiagnostics\Check\GuzzleHttpService', 'guzzle_http_service_foo'],
231
            ['rabbit_mq', ['foo' => null], 'ZendDiagnostics\Check\RabbitMQ', 'rabbit_mq_foo'],
232
            ['symfony_version', null, 'Liip\MonitorBundle\Check\SymfonyVersion'],
233
            ['custom_error_pages', ['error_codes' => [500], 'path' => __DIR__, 'controller' => 'foo'], 'Liip\MonitorBundle\Check\CustomErrorPages'],
234
            ['security_advisory', ['lock_file' => __DIR__.'/../../composer.lock'], 'ZendDiagnostics\Check\SecurityAdvisory'],
235
            ['stream_wrapper_exists', ['foo'], 'ZendDiagnostics\Check\StreamWrapperExists'],
236
            ['file_ini', ['foo.ini'], 'ZendDiagnostics\Check\IniFile'],
237
            ['file_json', ['foo.json'], 'ZendDiagnostics\Check\JsonFile'],
238
            ['file_xml', ['foo.xml'], 'ZendDiagnostics\Check\XmlFile'],
239
            ['file_yaml', ['foo.yaml'], 'ZendDiagnostics\Check\YamlFile'],
240
            ['expressions', ['foo' => ['label' => 'foo', 'critical_expression' => 'true']], 'Liip\MonitorBundle\Check\Expression', 'expression_foo'],
241
            ['pdo_connections', ['foo' => ['dsn' => 'my-dsn']], 'ZendDiagnostics\Check\PDOCheck', 'pdo_foo'],
242
        ];
243
    }
244
245
    protected function getContainerExtensions(): array
246
    {
247
        return [new LiipMonitorExtension()];
248
    }
249
}
250