Completed
Push — master ( 62452e...4a089c )
by Kevin
22s queued 10s
created

LiipMonitorExtensionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
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(array('checks' => array($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
    public function testDefaultGroupParameter()
71
    {
72
        $this->load(array('checks' => array('php_extensions' => array('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
    public function testDefaultGroupParameterCustom()
80
    {
81
        $this->load(array('checks' => array('php_extensions' => array('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(array('enable_controller' => true));
95
96
        $this->assertTrue($this->container->has('liip_monitor.health_controller'));
97
    }
98
99
    public function testMailer()
100
    {
101
        $this->load();
102
103
        $this->assertEquals(false, $this->container->has('liip_monitor.reporter.swift_mailer'));
104
105
        $this->load(
106
            array(
107
                'mailer' => array(
108
                    'recipient' => '[email protected]',
109
                    'sender' => '[email protected]',
110
                    'subject' => 'Health Check',
111
                    'send_on_warning' => true,
112
                ),
113
            )
114
        );
115
116
        $this->assertContainerBuilderHasService('liip_monitor.reporter.swift_mailer');
117
    }
118
119
    /**
120
     * @dataProvider mailerConfigProvider
121
     */
122
    public function testInvalidMailerConfig($config)
123
    {
124
        $this->expectException(InvalidConfigurationException::class);
125
126
        $this->load($config);
127
    }
128
129
    public function mailerConfigProvider()
130
    {
131
        return array(
132
            array(
133
                array(
134
                    'mailer' => array(
135
                        'recipient' => '[email protected]',
136
                    ),
137
                ),
138
            ),
139
            array(
140
                array(
141
                    'mailer' => array(
142
                        'recipient' => '[email protected]',
143
                        'sender' => '[email protected]',
144
                        'subject' => null,
145
                    ),
146
                ),
147
            ),
148
        );
149
    }
150
151
    /**
152
     * @dataProvider invalidCheckProvider
153
     */
154
    public function testInvalidExpressionConfig(array $config)
155
    {
156
        $this->expectException(InvalidConfigurationException::class);
157
158
        $this->load(array('checks' => array('expressions' => $config)));
159
        $this->compile();
160
    }
161
162
    public function invalidCheckProvider()
163
    {
164
        return array(
165
            array(array('foo')),
166
            array(array('foo' => array('critical_expression' => 'true'))),
167
            array(array('foo' => array('label' => 'foo'))),
168
        );
169
    }
170
171
    public function checkProvider()
172
    {
173
        return array(
174
            array('php_extensions', array('foo'), 'ZendDiagnostics\Check\ExtensionLoaded'),
175
            array('php_flags', array('foo' => 'true'), 'ZendDiagnostics\Check\PhpFlag', 'php_flag_foo'),
176
            array('php_version', array('5.3.3' => '='), 'ZendDiagnostics\Check\PhpVersion', 'php_version_5.3.3'),
177
            array('process_running', 'foo', 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running'),
178
            array('process_running', array('foo'), 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running'),
179
            array('process_running', array('foo', 'bar'), 'ZendDiagnostics\Check\ProcessRunning', 'process_foo_running', 2),
180
            array('process_running', array('foo', 'bar'), 'ZendDiagnostics\Check\ProcessRunning', 'process_bar_running', 2),
181
            array('readable_directory', array('foo'), 'ZendDiagnostics\Check\DirReadable'),
182
            array('writable_directory', array('foo'), 'ZendDiagnostics\Check\DirWritable'),
183
            array('class_exists', array('Foo'), 'ZendDiagnostics\Check\ClassExists'),
184
            array('cpu_performance', 0.5, 'ZendDiagnostics\Check\CpuPerformance'),
185
            array('disk_usage', array('path' => __DIR__), 'ZendDiagnostics\Check\DiskUsage'),
186
            array('symfony_requirements', array('file' => __DIR__.'/../../LiipMonitorBundle.php'), 'Liip\MonitorBundle\Check\SymfonyRequirements'),
187
            array('opcache_memory', null, 'ZendDiagnostics\Check\OpCacheMemory'),
188
            array('apc_memory', null, 'ZendDiagnostics\Check\ApcMemory'),
189
            array('apc_fragmentation', null, 'ZendDiagnostics\Check\ApcFragmentation'),
190
            array('doctrine_dbal', 'foo', 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection'),
191
            array('doctrine_dbal', array('foo'), 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection'),
192
            array('doctrine_dbal', array('foo', 'bar'), 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_foo_connection', 2),
193
            array('doctrine_dbal', array('foo', 'bar'), 'Liip\MonitorBundle\Check\DoctrineDbal', 'doctrine_dbal_bar_connection', 2),
194
            array('memcache', array('foo' => null), 'ZendDiagnostics\Check\Memcache', 'memcache_foo'),
195
            array('redis', array('foo' => null), 'ZendDiagnostics\Check\Redis', 'redis_foo'),
196
            array('http_service', array('foo' => null), 'ZendDiagnostics\Check\HttpService', 'http_service_foo'),
197
            array('guzzle_http_service', array('foo' => null), 'ZendDiagnostics\Check\GuzzleHttpService', 'guzzle_http_service_foo'),
198
            array('rabbit_mq', array('foo' => null), 'ZendDiagnostics\Check\RabbitMQ', 'rabbit_mq_foo'),
199
            array('symfony_version', null, 'Liip\MonitorBundle\Check\SymfonyVersion'),
200
            array('custom_error_pages', array('error_codes' => array(500), 'path' => __DIR__, 'controller' => 'foo'), 'Liip\MonitorBundle\Check\CustomErrorPages'),
201
            array('security_advisory', array('lock_file' => __DIR__.'/../../composer.lock'), 'ZendDiagnostics\Check\SecurityAdvisory'),
202
            array('stream_wrapper_exists', array('foo'), 'ZendDiagnostics\Check\StreamWrapperExists'),
203
            array('file_ini', array('foo.ini'), 'ZendDiagnostics\Check\IniFile'),
204
            array('file_json', array('foo.json'), 'ZendDiagnostics\Check\JsonFile'),
205
            array('file_xml', array('foo.xml'), 'ZendDiagnostics\Check\XmlFile'),
206
            array('file_yaml', array('foo.yaml'), 'ZendDiagnostics\Check\YamlFile'),
207
            array('expressions', array('foo' => array('label' => 'foo', 'critical_expression' => 'true')), 'Liip\MonitorBundle\Check\Expression', 'expression_foo'),
208
            array('pdo_connections', array('foo' => array('dsn' => 'my-dsn')), 'ZendDiagnostics\Check\PDOCheck', 'pdo_foo'),
209
        );
210
    }
211
212
    protected function getContainerExtensions(): array
213
    {
214
        return array(new LiipMonitorExtension());
215
    }
216
}
217