Completed
Push — check-assets-enabled ( a63d74...8c0039 )
by Lukas Kahwe
01:26
created

LiipMonitorExtensionTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 16
lcom 1
cbo 8
dl 0
loc 199
rs 10
c 7
b 0
f 3

14 Methods

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