Completed
Pull Request — master (#219)
by Markus
11:05
created

testInvalidExpressionConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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