Completed
Pull Request — master (#219)
by Kevin
01:39
created

LiipMonitorExtensionTest::testMailer()   A

Complexity

Conditions 1
Paths 1

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