Completed
Push — master ( 105040...f3c189 )
by Alejandro
04:30
created

MailOptionsAbstractFactoryTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 260
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 10
c 5
b 0
f 2
lcom 1
cbo 5
dl 40
loc 260
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B testCanCreateServiceWithName() 0 25 1
A testSomeCustomOptions() 20 20 1
A testOldConfigKey() 20 20 1
A testCreateServiceWithNonarrayOptions() 0 15 1
B testExtendOptions() 0 27 1
B testExtendWithValueNullIsIgnored() 0 24 1
B testExtendsSingleChaining() 0 39 1
A testExtendsDoubleChaining() 0 48 1
A initServiceManager() 0 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace AcMailerTest\Options;
3
4
use AcMailer\Options\Factory\MailOptionsAbstractFactory;
5
use AcMailer\Options\MailOptions;
6
use AcMailerTest\ServiceManager\ServiceManagerMock;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
use PHPUnit_Framework_TestCase as TestCase;
9
10
/**
11
 * Class MailOptionsFactoryTest
12
 * @author Alejandro Celaya Alastrué
13
 * @link http://www.alejandrocelaya.com
14
 */
15
class MailOptionsAbstractFactoryTest extends TestCase
16
{
17
    /**
18
     * @var MailOptionsAbstractFactory
19
     */
20
    private $mailOptionsFactory;
21
    /**
22
     * @var ServiceLocatorInterface
23
     */
24
    private $serviceLocator;
25
26
    public function setUp()
27
    {
28
        $this->mailOptionsFactory = new MailOptionsAbstractFactory();
29
    }
30
31
    public function testCanCreateServiceWithName()
32
    {
33
        $this->initServiceManager();
34
        $this->assertTrue($this->mailOptionsFactory->canCreateServiceWithName(
35
            $this->serviceLocator,
36
            'acmailer.mailoptions.default',
37
            ''
38
        ));
39
        $this->assertFalse($this->mailOptionsFactory->canCreateServiceWithName(
40
            $this->serviceLocator,
41
            'acmailer.mailoptions.employees',
42
            ''
43
        ));
44
        $this->assertFalse($this->mailOptionsFactory->canCreateServiceWithName($this->serviceLocator, 'foo', ''));
45
        $this->assertFalse($this->mailOptionsFactory->canCreateServiceWithName(
46
            $this->serviceLocator,
47
            'invalid.mailoptions.foobar',
48
            ''
49
        ));
50
        $this->assertFalse($this->mailOptionsFactory->canCreateServiceWithName(
51
            new ServiceManagerMock(['Config' => []]),
52
            'acmailer.mailoptions.default',
53
            ''
54
        ));
55
    }
56
57 View Code Duplication
    public function testSomeCustomOptions()
0 ignored issues
show
Duplication introduced by
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...
58
    {
59
        $services = $this->initServiceManager();
60
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
61
            $this->serviceLocator,
62
            'acmailer.mailoptions.default',
63
            ''
64
        );
65
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
66
        $this->assertEquals(
67
            [$services['Config']['acmailer_options']['default']['message_options']['to']],
68
            $mailOptions->getMessageOptions()->getTo()
69
        );
70
        $this->assertEquals(
71
            $services['Config']['acmailer_options']['default']['message_options']['from'],
72
            $mailOptions->getMessageOptions()->getFrom()
73
        );
74
        $this->assertEquals([], $mailOptions->getMessageOptions()->getCc());
75
        $this->assertEquals([], $mailOptions->getMessageOptions()->getBcc());
76
    }
77
78 View Code Duplication
    public function testOldConfigKey()
0 ignored issues
show
Duplication introduced by
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...
79
    {
80
        $services = $this->initServiceManager('mail_options');
81
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
82
            $this->serviceLocator,
83
            'acmailer.mailoptions.default',
84
            ''
85
        );
86
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
87
        $this->assertEquals(
88
            [$services['Config']['mail_options']['default']['message_options']['to']],
89
            $mailOptions->getMessageOptions()->getTo()
90
        );
91
        $this->assertEquals(
92
            $services['Config']['mail_options']['default']['message_options']['from'],
93
            $mailOptions->getMessageOptions()->getFrom()
94
        );
95
        $this->assertEquals([], $mailOptions->getMessageOptions()->getCc());
96
        $this->assertEquals([], $mailOptions->getMessageOptions()->getBcc());
97
    }
98
99
    public function testCreateServiceWithNonarrayOptions()
100
    {
101
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
102
            new ServiceManagerMock([
103
                'Config' => [
104
                    'acmailer_options' => [
105
                        'invalid' => ''
106
                    ]
107
                ]
108
            ]),
109
            'acmailer.mailoptions.invalid',
110
            ''
111
        );
112
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
113
    }
114
115
    public function testExtendOptions()
116
    {
117
        $this->serviceLocator = new ServiceManagerMock([
118
            'Config' => [
119
                'acmailer_options' => [
120
                    'default' => [
121
                        'message_options' => [
122
                            'to'    => '[email protected]',
123
                            'from'  => 'Me',
124
                        ]
125
                    ],
126
                    'another' => [
127
                        'extends' => 'default'
128
                    ]
129
                ]
130
            ]
131
        ]);
132
133
        /** @var MailOptions $mailOptions */
134
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
135
            $this->serviceLocator,
136
            'acmailer.mailoptions.another',
137
            ''
138
        );
139
        $this->assertEquals(['[email protected]'], $mailOptions->getMessageOptions()->getTo());
140
        $this->assertEquals('Me', $mailOptions->getMessageOptions()->getFrom());
141
    }
142
143
    public function testExtendWithValueNullIsIgnored()
144
    {
145
        $this->serviceLocator = new ServiceManagerMock([
146
            'Config' => [
147
                'acmailer_options' => [
148
                    'default' => [
149
                        'extends' => null,
150
                        'message_options' => [
151
                            'to'    => '[email protected]',
152
                            'from'  => 'Me',
153
                        ]
154
                    ],
155
                ]
156
            ]
157
        ]);
158
159
        /** @var MailOptions $mailOptions */
160
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
161
            $this->serviceLocator,
162
            'acmailer.mailoptions.default',
163
            ''
164
        );
165
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
166
    }
167
168
    public function testExtendsSingleChaining()
169
    {
170
        $this->serviceLocator = new ServiceManagerMock([
171
            'Config' => [
172
                'acmailer_options' => [
173
                    'default' => [
174
                        'extends' => null,
175
                        'message_options' => [
176
                            'to'    => '[email protected]'
177
                        ]
178
                    ],
179
                    'foo' => [
180
                        'extends' => 'default',
181
                        'message_options' => [
182
                            'from' => '[email protected]'
183
                        ]
184
                    ]
185
                ]
186
            ]
187
        ]);
188
189
        /** @var MailOptions $mailOptions */
190
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
191
            $this->serviceLocator,
192
            'acmailer.mailoptions.foo',
193
            ''
194
        );
195
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
196
        $this->assertEquals(
197
            [
198
                'to'   => [['[email protected]']],
199
                'from' => '[email protected]',
200
            ],
201
            [
202
                'to' => [$mailOptions->getMessageOptions()->getTo()],
203
                'from' => $mailOptions->getMessageOptions()->getFrom(),
204
            ]
205
        );
206
    }
207
208
    public function testExtendsDoubleChaining()
209
    {
210
        $this->serviceLocator = new ServiceManagerMock([
211
            'Config' => [
212
                'acmailer_options' => [
213
                    'default' => [
214
                        'extends' => null,
215
                        'message_options' => [
216
                            'to'    => '[email protected]',
217
                        ]
218
                    ],
219
                    'foo' => [
220
                        'extends' => 'default',
221
                        'message_options' => [
222
                            'from' => '[email protected]'
223
                        ]
224
                    ],
225
                    'bar' => [
226
                        'extends' => 'foo',
227
                        'message_options' => [
228
                            'to' => '[email protected]',
229
                            'subject' => 'Foobar subject'
230
                        ]
231
                    ]
232
                ]
233
            ]
234
        ]);
235
236
        /** @var MailOptions $mailOptions */
237
        $mailOptions = $this->mailOptionsFactory->createServiceWithName(
238
            $this->serviceLocator,
239
            'acmailer.mailoptions.bar',
240
            ''
241
        );
242
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
243
        $this->assertEquals(
244
            [
245
                'to' => [['[email protected]']],
246
                'from' => '[email protected]',
247
                'subject' => 'Foobar subject'
248
            ],
249
            [
250
                'to' => [$mailOptions->getMessageOptions()->getTo()],
251
                'from' => $mailOptions->getMessageOptions()->getFrom(),
252
                'subject' => $mailOptions->getMessageOptions()->getSubject()
253
            ]
254
        );
255
    }
256
257
    protected function initServiceManager($mailConfigKey = 'acmailer_options', $serviceName = 'default')
258
    {
259
        $services = [
260
            'Config' => [
261
                $mailConfigKey => [
262
                    $serviceName => [
263
                        'message_options' => [
264
                            'to'    => '[email protected]',
265
                            'from'  => 'Me',
266
                        ]
267
                    ]
268
                ]
269
            ]
270
        ];
271
        $this->serviceLocator = new ServiceManagerMock($services);
272
        return $services;
273
    }
274
}
275