Completed
Push — master ( 0ba7dd...4699e0 )
by Dominik
11s
created

DependencyInjection/AzineEmailExtensionTest.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Azine\EmailBundle\Tests\DependencyInjection;
4
5
use Azine\EmailBundle\DependencyInjection\AzineEmailExtension;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\Yaml\Yaml;
8
9
class AzineEmailExtensionTest extends \PHPUnit\Framework\TestCase
10
{
11
    /** @var ContainerBuilder */
12
    protected $configuration;
13
14
    /**
15
     * This should not throw an exception.
16
     */
17
    public function testMinimalConfig()
18
    {
19
        $loader = new AzineEmailExtension();
20
        $config = $this->getMinimalConfig();
21
        $loader->load(array($config), new ContainerBuilder());
22
        $this->assertTrue(true, 'Without this stupid assertion, PHPUnit classifies this test as risky');
23
    }
24
25
    /**
26
     * This should not throw an exception.
27
     */
28 View Code Duplication
    public function testFullConfig()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
29
    {
30
        $loader = new AzineEmailExtension();
31
        $config = $this->getFullConfig();
32
        $loader->load(array($config), new ContainerBuilder());
33
        $this->assertTrue(true, 'Without this stupid assertion, PHPUnit classifies this test as risky');
34
    }
35
36
    /**
37
     * This should throw an exception.
38
     *
39
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
40
     */
41 View Code Duplication
    public function testConfigWithMissingRecipientClass()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
42
    {
43
        $loader = new AzineEmailExtension();
44
        $config = $this->getFullConfig();
45
        unset($config['recipient_class']);
46
        $loader->load(array($config), new ContainerBuilder());
47
    }
48
49
    /**
50
     * This should throw an exception.
51
     *
52
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
53
     */
54 View Code Duplication
    public function testConfigWithMissingTemplateProvider()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
55
    {
56
        $loader = new AzineEmailExtension();
57
        $config = $this->getFullConfig();
58
        unset($config['template_provider']);
59
        $loader->load(array($config), new ContainerBuilder());
60
    }
61
62
    /**
63
     * This should throw an exception.
64
     *
65
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
66
     */
67 View Code Duplication
    public function testConfigWithMissingEmailAddress()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
68
    {
69
        $loader = new AzineEmailExtension();
70
        $config = $this->getFullConfig();
71
        unset($config['no_reply']['email']);
72
        $loader->load(array($config), new ContainerBuilder());
73
    }
74
75
    /**
76
     * This should throw an exception.
77
     *
78
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
79
     */
80 View Code Duplication
    public function testConfigWithMissingEmailName()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
81
    {
82
        $loader = new AzineEmailExtension();
83
        $config = $this->getFullConfig();
84
        unset($config['no_reply']['name']);
85
        $loader->load(array($config), new ContainerBuilder());
86
    }
87
88
    /**
89
     * This should throw an exception.
90
     *
91
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
92
     */
93 View Code Duplication
    public function testConfigWithMissingEmail()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
94
    {
95
        $loader = new AzineEmailExtension();
96
        $config = $this->getFullConfig();
97
        unset($config['no_reply']);
98
        $loader->load(array($config), new ContainerBuilder());
99
    }
100
101
    public function testCustomConfiguration()
102
    {
103
        $this->configuration = new ContainerBuilder();
104
        $loader = new AzineEmailExtension();
105
        $config = $this->getFullConfig();
106
        $config['recipient_class'] = 'TestRecipientClass';
107
        $config['recipient_newsletter_field'] = 'some_field';
108
        $config['template_provider'] = 'TestTemplateProvider';
109
        $config['notifier_service'] = 'TestNotifierService';
110
        $config['recipient_provider'] = 'TestRecipientService';
111
        $config['template_twig_swift_mailer'] = 'TestTwigSwiftMailer';
112
        $config['no_reply']['email'] = '[email protected]';
113
        $config['no_reply']['name'] = 'test name';
114
        $config['image_dir'] = '/tmp';
115
116
        $loader->load(array($config), $this->configuration);
117
118
        $this->assertParameter('TestRecipientClass', 'azine_email_recipient_class');
119
        $this->assertParameter('some_field', 'azine_email_recipient_newsletter_field');
120
121
        $mailArray = $this->configuration->getParameter('azine_email_no_reply');
122
        $this->assertSame('test name', $mailArray['name'], 'The no-reply-name is not correct.');
123
        $this->assertSame('[email protected]', $mailArray['email'], 'The no-reply-email is not correct.');
124
125
        $this->assertParameter('/tmp', 'azine_email_image_dir');
126
127
        $this->assertAlias('testtemplateprovider', 'azine_email_template_provider');
128
        $this->assertAlias('testnotifierservice', 'azine_email_notifier_service');
129
        $this->assertAlias('testrecipientservice', 'azine_email_recipient_provider');
130
        $this->assertAlias('testtwigswiftmailer', 'azine_email_template_twig_swift_mailer');
131
    }
132
133 View Code Duplication
    protected function createEmptyConfiguration()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
134
    {
135
        $this->configuration = new ContainerBuilder();
136
        $loader = new AzineEmailExtension();
137
        $config = $this->getEmptyConfig();
138
        $loader->load(array($config), $this->configuration);
139
        $this->assertTrue($this->configuration instanceof ContainerBuilder);
140
    }
141
142 View Code Duplication
    protected function createFullConfiguration()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
143
    {
144
        $this->configuration = new ContainerBuilder();
145
        $loader = new AzineEmailExtension();
146
        $config = $this->getFullConfig();
147
        $loader->load(array($config), $this->configuration);
148
        $this->assertTrue($this->configuration instanceof ContainerBuilder);
149
    }
150
151
    /**
152
     * Get the minimal config.
153
     *
154
     * @return array
155
     */
156
    protected function getMinimalConfig()
157
    {
158
        $yaml = <<<YAML
159
recipient_class: 'Azine\\PlatformBundle\\Entity\\User'
160
template_provider: 'azine_platform.emailtemplateprovider'
161
no_reply:
162
  email: '[email protected]'
163
  name: 'azine.me notification daemon'
164
YAML;
165
166
        return Yaml::parse($yaml);
167
    }
168
169
    /**
170
     * Get a full config for this bundle.
171
     */
172
    protected function getFullConfig()
173
    {
174
        $yaml = <<<YAML
175
recipient_class: 'Acme\\SomeBundle\\Entity\\User'
176
recipient_newsletter_field: 'newsletter'
177
notifier_service: 'azine_email.example.notifier_service'
178
template_provider: 'azine_email.example.template_provider'
179
recipient_provider: 'azine_email.default.recipient_provider'
180
template_twig_swift_mailer: 'azine_email.default.template_twig_swift_mailer'
181
no_reply:
182
  email: '[email protected]'
183
  name: 'notification daemon'
184
image_dir: '%kernel.root_dir%/../vendor/azine/email-bundle/Azine/EmailBundle/Resources/htmlTemplateImages/'
185
allowed_images_folders:
186
  - '%kernel.root_dir%/../vendor/azine/email-bundle/Azine/EmailBundle/Resources/htmlTemplateImages/'
187
  - '%kernel.root_dir%/../vendor/azine/email-bundle/Azine/EmailBundle/Resources/'
188
YAML;
189
190
        return Yaml::parse($yaml);
191
    }
192
193
    /**
194
     * @param string $value
195
     * @param string $key
196
     */
197
    private function assertAlias($value, $key)
198
    {
199
        $this->assertSame(strtolower($value), strtolower((string) $this->configuration->getAlias($key)), sprintf('%s alias is correct', $key));
200
    }
201
202
    /**
203
     * @param string $value
204
     * @param string $key
205
     */
206
    private function assertParameter($value, $key)
207
    {
208
        $this->assertSame($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key));
209
    }
210
211
    /**
212
     * @param string $id
213
     */
214
    private function assertHasDefinition($id)
215
    {
216
        $this->assertTrue(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
217
    }
218
219
    /**
220
     * @param string $id
221
     */
222
    private function assertNotHasDefinition($id)
223
    {
224
        $this->assertFalse(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
225
    }
226
227
    protected function tearDown()
228
    {
229
        unset($this->configuration);
230
    }
231
}
232