Completed
Pull Request — master (#250)
by
unknown
20:15
created

LiipMonitorExtensionTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 265
Duplicated Lines 6.04 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 8
dl 16
loc 265
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testChecksLoaded() 0 20 3
A testDefaultNoChecks() 0 7 1
A testDefaultGroupParameterHasNoChecks() 0 8 1
A testDefaultGroupParameter() 8 8 1
A testDefaultGroupParameterCustom() 8 8 1
A testEnableController() 0 10 1
A testDisabledDefaultMailer() 0 10 1
A testDisabledMailer() 0 20 1
A testEnabledMailer() 0 20 1
A testInvalidMailerConfig() 0 6 1
A mailerConfigProvider() 0 21 1
A testInvalidExpressionConfig() 0 7 1
A invalidCheckProvider() 0 8 1
B checkProvider() 0 70 1
A getContainerExtensions() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\MonitorBundle\Tests\DependencyInjection;
4
5
use Laminas\Diagnostics\Check\GuzzleHttpService;
6
use Laminas\Diagnostics\Check\HttpService;
7
use Laminas\Diagnostics\Check\Memcache;
8
use Laminas\Diagnostics\Check\Memcached;
9
use Laminas\Diagnostics\Check\PDOCheck;
10
use Laminas\Diagnostics\Check\PhpFlag;
11
use Laminas\Diagnostics\Check\PhpVersion;
12
use Laminas\Diagnostics\Check\ProcessRunning;
13
use Laminas\Diagnostics\Check\RabbitMQ;
14
use Laminas\Diagnostics\Check\Redis;
15
use Liip\MonitorBundle\Check\ApcFragmentation;
16
use Liip\MonitorBundle\Check\ApcMemory;
17
use Liip\MonitorBundle\Check\ClassExists;
18
use Liip\MonitorBundle\Check\CpuPerformance;
19
use Liip\MonitorBundle\Check\CustomErrorPages;
20
use Liip\MonitorBundle\Check\DiskUsage;
21
use Liip\MonitorBundle\Check\DoctrineDbal;
22
use Liip\MonitorBundle\Check\Expression;
23
use Liip\MonitorBundle\Check\IniFile;
24
use Liip\MonitorBundle\Check\JsonFile;
25
use Liip\MonitorBundle\Check\OpCacheMemory;
26
use Liip\MonitorBundle\Check\PhpExtension;
27
use Liip\MonitorBundle\Check\ReadableDirectory;
28
use Liip\MonitorBundle\Check\SecurityAdvisory;
29
use Liip\MonitorBundle\Check\StreamWrapperExists;
30
use Liip\MonitorBundle\Check\SymfonyVersion;
31
use Liip\MonitorBundle\Check\WritableDirectory;
32
use Liip\MonitorBundle\Check\XmlFile;
33
use Liip\MonitorBundle\Check\YamlFile;
34
use Liip\MonitorBundle\DependencyInjection\Compiler\AddGroupsCompilerPass;
35
use Liip\MonitorBundle\DependencyInjection\Compiler\CheckCollectionTagCompilerPass;
36
use Liip\MonitorBundle\DependencyInjection\Compiler\CheckTagCompilerPass;
37
use Liip\MonitorBundle\DependencyInjection\Compiler\GroupRunnersCompilerPass;
38
use Liip\MonitorBundle\DependencyInjection\LiipMonitorExtension;
39
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
40
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
41
42
/**
43
 * @author Kevin Bond <[email protected]>
44
 */
45
class LiipMonitorExtensionTest extends AbstractExtensionTestCase
46
{
47
    protected function setUp(): void
48
    {
49
        parent::setUp();
50
51
        $doctrineMock = $this->getMockBuilder('Doctrine\Persistence\ConnectionRegistry')->getMock();
52
        $this->container->set('doctrine', $doctrineMock);
53
        $this->container->addCompilerPass(new AddGroupsCompilerPass());
54
        $this->container->addCompilerPass(new GroupRunnersCompilerPass());
55
        $this->container->addCompilerPass(new CheckTagCompilerPass());
56
        $this->container->addCompilerPass(new CheckCollectionTagCompilerPass());
57
    }
58
59
    /**
60
     * @dataProvider checkProvider
61
     */
62
    public function testChecksLoaded($name, $config, $checkClass, $checkAlias = null, $checkCount = 1)
63
    {
64
        // skip checks for missing classes
65
        if (!class_exists($checkClass)) {
66
            $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...
67
        }
68
69
        if (!$checkAlias) {
70
            $checkAlias = $name;
71
        }
72
73
        $this->container->setParameter('kernel.project_dir', __DIR__);
74
        $this->load(['checks' => [$name => $config]]);
75
        $this->compile();
76
77
        $runner = $this->container->get('liip_monitor.runner');
78
79
        $this->assertCount($checkCount, $runner->getChecks());
80
        $this->assertInstanceOf($checkClass, $runner->getCheck($checkAlias));
81
    }
82
83
    public function testDefaultNoChecks()
84
    {
85
        $this->load();
86
        $this->compile();
87
88
        $this->assertCount(0, $this->container->get('liip_monitor.runner')->getChecks());
89
    }
90
91
    public function testDefaultGroupParameterHasNoChecks()
92
    {
93
        $this->load();
94
        $this->compile();
95
96
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
97
        $this->assertSame('default', $this->container->getParameter('liip_monitor.default_group'));
98
    }
99
100 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...
101
    {
102
        $this->load(['checks' => ['php_extensions' => ['foo']]]);
103
        $this->compile();
104
105
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
106
        $this->assertSame('default', $this->container->getParameter('liip_monitor.default_group'));
107
    }
108
109 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...
110
    {
111
        $this->load(['checks' => ['php_extensions' => ['foo']], 'default_group' => 'foo_bar']);
112
        $this->compile();
113
114
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
115
        $this->assertSame('foo_bar', $this->container->getParameter('liip_monitor.default_group'));
116
    }
117
118
    public function testEnableController()
119
    {
120
        $this->load();
121
122
        $this->assertFalse($this->container->has('liip_monitor.health_controller'));
123
124
        $this->load(['enable_controller' => true]);
125
126
        $this->assertTrue($this->container->has('liip_monitor.health_controller'));
127
    }
128
129
    public function testDisabledDefaultMailer()
130
    {
131
        $this->load();
132
133
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.enabled'));
134
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.recipient'));
135
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.sender'));
136
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.subject'));
137
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.send_on_warning'));
138
    }
139
140
    public function testDisabledMailer()
141
    {
142
        $this->load(
143
            [
144
                'mailer' => [
145
                    'enabled' => false,
146
                    'recipient' => '[email protected]',
147
                    'sender' => '[email protected]',
148
                    'subject' => 'Health Check',
149
                    'send_on_warning' => true,
150
                ],
151
            ]
152
        );
153
154
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.enabled'));
155
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.recipient'));
156
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.sender'));
157
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.subject'));
158
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.send_on_warning'));
159
    }
160
161
    public function testEnabledMailer()
162
    {
163
        $this->load(
164
            [
165
                'mailer' => [
166
                    'enabled' => true,
167
                    'recipient' => '[email protected]',
168
                    'sender' => '[email protected]',
169
                    'subject' => 'Health Check',
170
                    'send_on_warning' => true,
171
                ],
172
            ]
173
        );
174
175
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.enabled', true);
176
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.recipient', ['[email protected]']);
177
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.sender', '[email protected]');
178
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.subject', 'Health Check');
179
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.send_on_warning', true);
180
    }
181
182
    /**
183
     * @dataProvider mailerConfigProvider
184
     */
185
    public function testInvalidMailerConfig($config)
186
    {
187
        $this->expectException(InvalidConfigurationException::class);
188
189
        $this->load($config);
190
    }
191
192
    public function mailerConfigProvider()
193
    {
194
        return [
195
            [
196
                [
197
                    'mailer' => [
198
                        'recipient' => '[email protected]',
199
                    ],
200
                ],
201
            ],
202
            [
203
                [
204
                    'mailer' => [
205
                        'recipient' => '[email protected]',
206
                        'sender' => '[email protected]',
207
                        'subject' => null,
208
                    ],
209
                ],
210
            ],
211
        ];
212
    }
213
214
    /**
215
     * @dataProvider invalidCheckProvider
216
     */
217
    public function testInvalidExpressionConfig(array $config)
218
    {
219
        $this->expectException(InvalidConfigurationException::class);
220
221
        $this->load(['checks' => ['expressions' => $config]]);
222
        $this->compile();
223
    }
224
225
    public function invalidCheckProvider()
226
    {
227
        return [
228
            [['foo']],
229
            [['foo' => ['critical_expression' => 'true']]],
230
            [['foo' => ['label' => 'foo']]],
231
        ];
232
    }
233
234
    public function checkProvider()
235
    {
236
        return [
237
            ['php_extensions', ['foo', ['name' => 'bar', 'label' => 'baz']], PhpExtension::class, 'php_extension_foo', 2],
238
            ['php_extensions', ['foo', ['name' => 'bar', 'label' => 'baz']], PhpExtension::class, 'php_extension_bar', 2],
239
            ['php_extensions', [['name' => 'foo']], PhpExtension::class, 'php_extension_foo'],
240
            ['php_flags', ['foo' => 'true', 'bar' => ['value' => 'false', 'label' => 'baz']], PhpFlag::class, 'php_flag_foo', 2],
241
            ['php_flags', ['foo' => 'true', 'bar' => ['value' => 'false', 'label' => 'baz']], PhpFlag::class, 'php_flag_bar', 2],
242
            ['php_flags', ['foo' => ['value' => 'false']], PhpFlag::class, 'php_flag_foo'],
243
            ['php_version', ['5.3.3' => '=', '7.1.0' => ['operator' => '>=', 'label' => 'foo']], PhpVersion::class, 'php_version_5.3.3', 2],
244
            ['php_version', ['5.3.3' => '=', '7.1.0' => ['operator' => '>=', 'label' => 'foo']], PhpVersion::class, 'php_version_7.1.0', 2],
245
            ['php_version', ['7.1.0' => ['operator' => '>=']], PhpVersion::class, 'php_version_7.1.0'],
246
            ['process_running', 'foo', ProcessRunning::class, 'process_foo_running'],
247
            ['process_running', ['foo'], ProcessRunning::class, 'process_foo_running'],
248
            ['process_running', ['foo', ['name' => 'bar']], ProcessRunning::class, 'process_foo_running', 2],
249
            ['process_running', ['foo', ['name' => 'bar', 'label' => 'baz']], ProcessRunning::class, 'process_bar_running', 2],
250
            ['readable_directory', ['foo', ['path' => 'bar']], ReadableDirectory::class, 'readable_directory_foo', 2],
251
            ['readable_directory', ['foo', ['path' => 'bar', 'label' => 'baz']], ReadableDirectory::class, 'readable_directory_bar', 2],
252
            ['writable_directory', ['foo', ['path' => 'bar']], WritableDirectory::class, 'writable_directory_foo', 2],
253
            ['writable_directory', ['foo', ['path' => 'bar', 'label' => 'baz']], WritableDirectory::class, 'writable_directory_bar', 2],
254
            ['class_exists', ['Foo', ['name' => 'Bar']], ClassExists::class, 'class_exists_Foo', 2],
255
            ['class_exists', ['Foo', ['name' => 'Bar', 'label' => 'baz']], ClassExists::class, 'class_exists_Bar', 2],
256
            ['cpu_performance', 0.5, CpuPerformance::class],
257
            ['cpu_performance', ['performance' => 0.5, 'label' => 'foo'], CpuPerformance::class],
258
            ['disk_usage', ['path' => __DIR__], DiskUsage::class],
259
            ['disk_usage', ['path' => __DIR__, 'label' => 'foo'], DiskUsage::class],
260
            ['symfony_requirements', ['file' => __DIR__.'/../../LiipMonitorBundle.php', 'label' => 'foo'], 'Liip\MonitorBundle\Check\SymfonyRequirements'],
261
            ['opcache_memory', null, OpCacheMemory::class],
262
            ['opcache_memory', ['label' => 'foo'], OpCacheMemory::class],
263
            ['apc_memory', null, ApcMemory::class],
264
            ['apc_memory', ['label' => 'foo'], ApcMemory::class],
265
            ['apc_fragmentation', null, ApcFragmentation::class],
266
            ['apc_fragmentation', ['label' => 'foo'], ApcFragmentation::class],
267
            ['doctrine_dbal', 'foo', DoctrineDbal::class, 'doctrine_dbal_foo_connection'],
268
            ['doctrine_dbal', ['foo'], DoctrineDbal::class, 'doctrine_dbal_foo_connection'],
269
            ['doctrine_dbal', ['foo', 'bar'], DoctrineDbal::class, 'doctrine_dbal_foo_connection', 2],
270
            ['doctrine_dbal', ['foo', 'bar'], DoctrineDbal::class, 'doctrine_dbal_bar_connection', 2],
271
            ['doctrine_dbal', ['foo', ['name' => 'bar', 'label' => 'baz']], DoctrineDbal::class, 'doctrine_dbal_bar_connection', 2],
272
            ['memcache', ['foo' => null], Memcache::class, 'memcache_foo'],
273
            ['memcache', ['foo' => ['label' => 'bar']], Memcache::class, 'memcache_foo'],
274
            ['memcached', ['foo' => null], Memcached::class, 'memcached_foo'],
275
            ['memcached', ['foo' => ['label' => 'bar']], Memcached::class, 'memcached_foo'],
276
            ['redis', ['foo' => null], Redis::class, 'redis_foo'],
277
            ['redis', ['foo' => ['label' => 'bar']], Redis::class, 'redis_foo'],
278
            ['http_service', ['foo' => null], HttpService::class, 'http_service_foo'],
279
            ['http_service', ['foo' => ['label' => 'bar']], HttpService::class, 'http_service_foo'],
280
            ['guzzle_http_service', ['foo' => null], GuzzleHttpService::class, 'guzzle_http_service_foo'],
281
            ['guzzle_http_service', ['foo' => ['label' => 'bar']], GuzzleHttpService::class, 'guzzle_http_service_foo'],
282
            ['rabbit_mq', ['foo' => null], RabbitMQ::class, 'rabbit_mq_foo'],
283
            ['rabbit_mq', ['foo' => ['label' => 'bar']], RabbitMQ::class, 'rabbit_mq_foo'],
284
            ['symfony_version', null, SymfonyVersion::class],
285
            ['symfony_version', ['label' => 'foo'], SymfonyVersion::class],
286
            ['custom_error_pages', ['error_codes' => [500]], CustomErrorPages::class],
287
            ['custom_error_pages', ['error_codes' => [500], 'label' => 'foo'], CustomErrorPages::class],
288
            ['security_advisory', ['lock_file' => __DIR__.'/../../composer.lock'], SecurityAdvisory::class],
289
            ['security_advisory', ['lock_file' => __DIR__.'/../../composer.lock', 'label' => 'foo'], SecurityAdvisory::class],
290
            ['stream_wrapper_exists', ['foo'], StreamWrapperExists::class, 'stream_wrapper_exists_foo'],
291
            ['stream_wrapper_exists', ['foo', ['name' => 'bar', 'label' => 'baz']], StreamWrapperExists::class, 'stream_wrapper_exists_bar', 2],
292
            ['file_ini', ['foo.ini'], IniFile::class, 'file_ini_foo.ini'],
293
            ['file_ini', ['foo.ini', ['path' => 'bar.ini', 'label' => 'baz']], IniFile::class, 'file_ini_foo.ini', 2],
294
            ['file_json', ['foo.json'], JsonFile::class, 'file_json_foo.json'],
295
            ['file_json', ['foo.json', ['path' => 'bar.json', 'label' => 'baz']], JsonFile::class, 'file_json_foo.json', 2],
296
            ['file_xml', ['foo.xml'], XmlFile::class, 'file_xml_foo.xml'],
297
            ['file_xml', ['foo.xml', ['path' => 'bar.xml', 'label' => 'baz']], XmlFile::class, 'file_xml_foo.xml', 2],
298
            ['file_yaml', ['foo.yaml'], YamlFile::class, 'file_yaml_foo.yaml'],
299
            ['file_yaml', ['foo.yaml', ['path' => 'bar.yaml', 'label' => 'baz']], YamlFile::class, 'file_yaml_foo.yaml', 2],
300
            ['expressions', ['foo' => ['label' => 'foo', 'critical_expression' => 'true']], Expression::class, 'expression_foo'],
301
            ['pdo_connections', ['foo' => ['dsn' => 'my-dsn']], PDOCheck::class, 'pdo_foo'],
302
        ];
303
    }
304
305
    protected function getContainerExtensions(): array
306
    {
307
        return [new LiipMonitorExtension()];
308
    }
309
}
310