Completed
Pull Request — master (#227)
by Carlos
18:43
created

LiipMonitorExtensionTest::testChecksLoaded()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 5
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
use Symfony\Component\Mailer\MailerInterface;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
class LiipMonitorExtensionTest extends AbstractExtensionTestCase
18
{
19
    protected function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $doctrineMock = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry')->getMock();
24
        $this->container->set('doctrine', $doctrineMock);
25
        $this->container->addCompilerPass(new AddGroupsCompilerPass());
26
        $this->container->addCompilerPass(new GroupRunnersCompilerPass());
27
        $this->container->addCompilerPass(new CheckTagCompilerPass());
28
        $this->container->addCompilerPass(new CheckCollectionTagCompilerPass());
29
    }
30
31
    /**
32
     * @dataProvider checkProvider
33
     */
34
    public function testChecksLoaded($name, $config, $checkClass, $checkAlias = null, $checkCount = 1)
35
    {
36
        // skip checks for missing classes
37
        if (!class_exists($checkClass)) {
38
            $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...
39
        }
40
41
        if (!$checkAlias) {
42
            $checkAlias = $name;
43
        }
44
45
        $this->load(['checks' => [$name => $config]]);
46
        $this->compile();
47
48
        $runner = $this->container->get('liip_monitor.runner');
49
50
        $this->assertCount($checkCount, $runner->getChecks());
51
        $this->assertInstanceOf($checkClass, $runner->getCheck($checkAlias));
52
    }
53
54
    public function testDefaultNoChecks()
55
    {
56
        $this->load();
57
        $this->compile();
58
59
        $this->assertCount(0, $this->container->get('liip_monitor.runner')->getChecks());
60
    }
61
62
    public function testDefaultGroupParameterHasNoChecks()
63
    {
64
        $this->load();
65
        $this->compile();
66
67
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
68
        $this->assertSame('default', $this->container->getParameter('liip_monitor.default_group'));
69
    }
70
71 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...
72
    {
73
        $this->load(['checks' => ['php_extensions' => ['foo']]]);
74
        $this->compile();
75
76
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
77
        $this->assertSame('default', $this->container->getParameter('liip_monitor.default_group'));
78
    }
79
80 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...
81
    {
82
        $this->load(['checks' => ['php_extensions' => ['foo']], 'default_group' => 'foo_bar']);
83
        $this->compile();
84
85
        $this->assertTrue($this->container->hasParameter('liip_monitor.default_group'));
86
        $this->assertSame('foo_bar', $this->container->getParameter('liip_monitor.default_group'));
87
    }
88
89
    public function testEnableController()
90
    {
91
        $this->load();
92
93
        $this->assertFalse($this->container->has('liip_monitor.health_controller'));
94
95
        $this->load(['enable_controller' => true]);
96
97
        $this->assertTrue($this->container->has('liip_monitor.health_controller'));
98
    }
99
100
    public function testDisabledDefaultMailer()
101
    {
102
        $this->load();
103
104
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.enabled', false);
105
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.recipient'));
106
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.sender'));
107
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.subject'));
108
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.send_on_warning'));
109
    }
110
111
    public function testDisabledMailer()
112
    {
113
        $this->load(
114
            [
115
                'mailer' => [
116
                    'enabled'   => false,
117
                    'recipient' => '[email protected]',
118
                    'sender' => '[email protected]',
119
                    'subject' => 'Health Check',
120
                    'send_on_warning' => true,
121
                ],
122
            ]
123
        );
124
125
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.enabled', false);
126
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.recipient'));
127
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.sender'));
128
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.subject'));
129
        $this->assertFalse($this->container->hasParameter('liip_monitor.mailer.send_on_warning'));
130
    }
131
132
    public function testEnabledMailer()
133
    {
134
        $this->load(
135
            [
136
                'mailer' => [
137
                    'enabled'   => true,
138
                    'recipient' => '[email protected]',
139
                    'sender' => '[email protected]',
140
                    'subject' => 'Health Check',
141
                    'send_on_warning' => true,
142
                ],
143
            ]
144
        );
145
146
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.enabled', true);
147
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.recipient', ['[email protected]']);
148
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.sender', '[email protected]');
149
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.subject', 'Health Check');
150
        $this->assertContainerBuilderHasParameter('liip_monitor.mailer.send_on_warning', true);
151
    }
152
153
    /**
154
     * @dataProvider mailerConfigProvider
155
     */
156
    public function testInvalidMailerConfig($config)
157
    {
158
        $this->expectException(InvalidConfigurationException::class);
159
160
        $this->load($config);
161
    }
162
163
    public function mailerConfigProvider()
164
    {
165
        return [
166
            [
167
                [
168
                    'mailer' => [
169
                        'recipient' => '[email protected]',
170
                    ],
171
                ],
172
            ],
173
            [
174
                [
175
                    'mailer' => [
176
                        'recipient' => '[email protected]',
177
                        'sender' => '[email protected]',
178
                        'subject' => null,
179
                    ],
180
                ],
181
            ],
182
        ];
183
    }
184
185
    /**
186
     * @dataProvider invalidCheckProvider
187
     */
188
    public function testInvalidExpressionConfig(array $config)
189
    {
190
        $this->expectException(InvalidConfigurationException::class);
191
192
        $this->load(['checks' => ['expressions' => $config]]);
193
        $this->compile();
194
    }
195
196
    public function invalidCheckProvider()
197
    {
198
        return [
199
            [['foo']],
200
            [['foo' => ['critical_expression' => 'true']]],
201
            [['foo' => ['label' => 'foo']]],
202
        ];
203
    }
204
205
    public function checkProvider()
206
    {
207
        return [
208
            ['php_extensions', ['foo'], 'ZendDiagnostics\Check\ExtensionLoaded'],
209
            ['php_flags', ['foo' => 'true'], 'ZendDiagnostics\Check\PhpFlag', 'php_flag_foo'],
210
            ['php_version', ['5.3.3' => '='], 'ZendDiagnostics\Check\PhpVersion', 'php_version_5.3.3'],
211
            ['process_running', 'foo', 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running'],
212
            ['process_running', ['foo'], 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running'],
213
            ['process_running', ['foo', 'bar'], 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running', 2],
214
            ['process_running', ['foo', 'bar'], 'ZendDiagnostics\Check\ProcessRunning', 'process_bar_running', 2],
215
            ['readable_directory', ['foo'], 'ZendDiagnostics\Check\DirReadable'],
216
            ['writable_directory', ['foo'], 'ZendDiagnostics\Check\DirWritable'],
217
            ['class_exists', ['Foo'], 'ZendDiagnostics\Check\ClassExists'],
218
            ['cpu_performance', 0.5, 'ZendDiagnostics\Check\CpuPerformance'],
219
            ['disk_usage', ['path' => __DIR__], 'ZendDiagnostics\Check\DiskUsage'],
220
            ['symfony_requirements', ['file' => __DIR__.'/../../LiipMonitorBundle.php'], 'Liip\MonitorBundle\Check\SymfonyRequirements'],
221
            ['opcache_memory', null, 'ZendDiagnostics\Check\OpCacheMemory'],
222
            ['apc_memory', null, 'ZendDiagnostics\Check\ApcMemory'],
223
            ['apc_fragmentation', null, 'ZendDiagnostics\Check\ApcFragmentation'],
224
            ['doctrine_dbal', 'foo', 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection'],
225
            ['doctrine_dbal', ['foo'], 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection'],
226
            ['doctrine_dbal', ['foo', 'bar'], 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection', 2],
227
            ['doctrine_dbal', ['foo', 'bar'], 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_bar_connection', 2],
228
            ['memcache', ['foo' => null], 'ZendDiagnostics\Check\Memcache', 'memcache_foo'],
229
            ['redis', ['foo' => null], 'ZendDiagnostics\Check\Redis', 'redis_foo'],
230
            ['http_service', ['foo' => null], 'ZendDiagnostics\Check\HttpService', 'http_service_foo'],
231
            ['guzzle_http_service', ['foo' => null], 'ZendDiagnostics\Check\GuzzleHttpService', 'guzzle_http_service_foo'],
232
            ['rabbit_mq', ['foo' => null], 'ZendDiagnostics\Check\RabbitMQ', 'rabbit_mq_foo'],
233
            ['symfony_version', null, 'Liip\MonitorBundle\Check\SymfonyVersion'],
234
            ['custom_error_pages', ['error_codes' => [500], 'path' => __DIR__, 'controller' => 'foo'], 'Liip\MonitorBundle\Check\CustomErrorPages'],
235
            ['security_advisory', ['lock_file' => __DIR__.'/../../composer.lock'], 'ZendDiagnostics\Check\SecurityAdvisory'],
236
            ['stream_wrapper_exists', ['foo'], 'ZendDiagnostics\Check\StreamWrapperExists'],
237
            ['file_ini', ['foo.ini'], 'ZendDiagnostics\Check\IniFile'],
238
            ['file_json', ['foo.json'], 'ZendDiagnostics\Check\JsonFile'],
239
            ['file_xml', ['foo.xml'], 'ZendDiagnostics\Check\XmlFile'],
240
            ['file_yaml', ['foo.yaml'], 'ZendDiagnostics\Check\YamlFile'],
241
            ['expressions', ['foo' => ['label' => 'foo', 'critical_expression' => 'true']], 'Liip\MonitorBundle\Check\Expression', 'expression_foo'],
242
            ['pdo_connections', ['foo' => ['dsn' => 'my-dsn']], 'ZendDiagnostics\Check\PDOCheck', 'pdo_foo'],
243
        ];
244
    }
245
246
    protected function getContainerExtensions(): array
247
    {
248
        return [new LiipMonitorExtension()];
249
    }
250
}
251