1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\MonitorBundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Liip\MonitorBundle\DependencyInjection\Compiler\MailerCompilerPass; |
6
|
|
|
use Liip\MonitorBundle\Helper\SwiftMailerReporter; |
7
|
|
|
use Liip\MonitorBundle\Helper\SymfonyMailerReporter; |
8
|
|
|
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
13
|
|
|
|
14
|
|
|
class MailerCompilerPassTest extends AbstractCompilerPassTestCase |
15
|
|
|
{ |
16
|
|
|
public function testDisabledMailer() |
17
|
|
|
{ |
18
|
|
|
$this->setParameter('liip_monitor.mailer.enabled', false); |
19
|
|
|
|
20
|
|
|
$this->compile(); |
21
|
|
|
|
22
|
|
|
$this->assertContainerBuilderNotHasService('liip_monitor.reporter.symfony_mailer'); |
23
|
|
|
$this->assertContainerBuilderNotHasService('liip_monitor.reporter.swift_mailer'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testSwiftMailer() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->setParameter('liip_monitor.mailer.enabled', true); |
29
|
|
|
$this->setDefinition('mailer', new Definition(\Swift_Mailer::class)); |
30
|
|
|
|
31
|
|
|
$this->compile(); |
32
|
|
|
|
33
|
|
|
$this->assertContainerBuilderNotHasService('liip_monitor.reporter.symfony_mailer'); |
34
|
|
|
$this->assertContainerBuilderHasService('liip_monitor.reporter.swift_mailer', SwiftMailerReporter::class); |
35
|
|
|
|
36
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
37
|
|
|
'liip_monitor.reporter.swift_mailer', |
38
|
|
|
0, |
39
|
|
|
new Reference('mailer') |
40
|
|
|
); |
41
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
42
|
|
|
'liip_monitor.reporter.swift_mailer', |
43
|
|
|
1, |
44
|
|
|
'%liip_monitor.mailer.recipient%' |
45
|
|
|
); |
46
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
47
|
|
|
'liip_monitor.reporter.swift_mailer', |
48
|
|
|
2, |
49
|
|
|
'%liip_monitor.mailer.sender%' |
50
|
|
|
); |
51
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
52
|
|
|
'liip_monitor.reporter.swift_mailer', |
53
|
|
|
3, |
54
|
|
|
'%liip_monitor.mailer.subject%' |
55
|
|
|
); |
56
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
57
|
|
|
'liip_monitor.reporter.swift_mailer', |
58
|
|
|
4, |
59
|
|
|
'%liip_monitor.mailer.send_on_warning%' |
60
|
|
|
); |
61
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithTag( |
62
|
|
|
'liip_monitor.reporter.swift_mailer', |
63
|
|
|
'liip_monitor.additional_reporter', |
64
|
|
|
['alias' => 'swift_mailer'] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function testSymfonyMailer() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->setParameter('liip_monitor.mailer.enabled', true); |
71
|
|
|
$this->setDefinition('mailer', new Definition(MailerInterface::class)); |
72
|
|
|
|
73
|
|
|
$this->compile(); |
74
|
|
|
|
75
|
|
|
$this->assertContainerBuilderNotHasService('liip_monitor.reporter.swift_mailer'); |
76
|
|
|
$this->assertContainerBuilderHasService('liip_monitor.reporter.symfony_mailer', SymfonyMailerReporter::class); |
77
|
|
|
|
78
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
79
|
|
|
'liip_monitor.reporter.symfony_mailer', |
80
|
|
|
0, |
81
|
|
|
new Reference('mailer') |
82
|
|
|
); |
83
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
84
|
|
|
'liip_monitor.reporter.symfony_mailer', |
85
|
|
|
1, |
86
|
|
|
'%liip_monitor.mailer.recipient%' |
87
|
|
|
); |
88
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
89
|
|
|
'liip_monitor.reporter.symfony_mailer', |
90
|
|
|
2, |
91
|
|
|
'%liip_monitor.mailer.sender%' |
92
|
|
|
); |
93
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
94
|
|
|
'liip_monitor.reporter.symfony_mailer', |
95
|
|
|
3, |
96
|
|
|
'%liip_monitor.mailer.subject%' |
97
|
|
|
); |
98
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument( |
99
|
|
|
'liip_monitor.reporter.symfony_mailer', |
100
|
|
|
4, |
101
|
|
|
'%liip_monitor.mailer.send_on_warning%' |
102
|
|
|
); |
103
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithTag( |
104
|
|
|
'liip_monitor.reporter.symfony_mailer', |
105
|
|
|
'liip_monitor.additional_reporter', |
106
|
|
|
['alias' => 'symfony_mailer'] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testMailerWithoutPackage() |
111
|
|
|
{ |
112
|
|
|
$this->setParameter('liip_monitor.mailer.enabled', true); |
113
|
|
|
$this->expectExceptionMessage('To enable mail reporting you have to install the "swiftmailer/swiftmailer" or "symfony/mailer".'); |
114
|
|
|
$this->expectException(\InvalidArgumentException::class); |
115
|
|
|
|
116
|
|
|
$this->assertContainerBuilderNotHasService('mailer'); |
117
|
|
|
$this->compile(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function registerCompilerPass(ContainerBuilder $container): void |
121
|
|
|
{ |
122
|
|
|
$container->addCompilerPass(new MailerCompilerPass()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.