Completed
Push — master ( 01eb7b...2ee407 )
by Alejandro
09:40 queued 07:02
created

MailOptionsAbstractFactoryTest::testOldConfigKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 20
rs 9.4285
cc 1
eloc 15
nc 1
nop 0
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->canCreate(
35
            $this->serviceLocator,
36
            'acmailer.mailoptions.default'
37
        ));
38
        $this->assertFalse($this->mailOptionsFactory->canCreate(
39
            $this->serviceLocator,
40
            'acmailer.mailoptions.employees'
41
        ));
42
        $this->assertFalse($this->mailOptionsFactory->canCreate($this->serviceLocator, 'foo'));
43
        $this->assertFalse($this->mailOptionsFactory->canCreate(
44
            $this->serviceLocator,
45
            'invalid.mailoptions.foobar'
46
        ));
47
        $this->assertFalse($this->mailOptionsFactory->canCreate(
48
            new ServiceManagerMock(['Config' => []]),
49
            'acmailer.mailoptions.default'
50
        ));
51
    }
52
53
    public function testSomeCustomOptions()
54
    {
55
        $services = $this->initServiceManager();
56
        $mailOptions = $this->mailOptionsFactory->__invoke(
57
            $this->serviceLocator,
58
            'acmailer.mailoptions.default'
59
        );
60
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
61
        $this->assertEquals(
62
            [$services['Config']['acmailer_options']['default']['message_options']['to']],
63
            $mailOptions->getMessageOptions()->getTo()
64
        );
65
        $this->assertEquals(
66
            $services['Config']['acmailer_options']['default']['message_options']['from'],
67
            $mailOptions->getMessageOptions()->getFrom()
68
        );
69
        $this->assertEquals([], $mailOptions->getMessageOptions()->getCc());
70
        $this->assertEquals([], $mailOptions->getMessageOptions()->getBcc());
71
    }
72
73
    public function testCreateServiceWithNonarrayOptions()
74
    {
75
        $mailOptions = $this->mailOptionsFactory->__invoke(
76
            new ServiceManagerMock([
77
                'Config' => [
78
                    'acmailer_options' => [
79
                        'invalid' => ''
80
                    ]
81
                ]
82
            ]),
83
            'acmailer.mailoptions.invalid'
84
        );
85
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
86
    }
87
88
    public function testExtendOptions()
89
    {
90
        $this->serviceLocator = new ServiceManagerMock([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \AcMailerTest\Servic...ends' => 'default'))))) of type object<AcMailerTest\Serv...ger\ServiceManagerMock> is incompatible with the declared type object<Zend\ServiceManag...erviceLocatorInterface> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91
            'Config' => [
92
                'acmailer_options' => [
93
                    'default' => [
94
                        'message_options' => [
95
                            'to'    => '[email protected]',
96
                            'from'  => 'Me',
97
                        ]
98
                    ],
99
                    'another' => [
100
                        'extends' => 'default'
101
                    ]
102
                ]
103
            ]
104
        ]);
105
106
        /** @var MailOptions $mailOptions */
107
        $mailOptions = $this->mailOptionsFactory->__invoke(
108
            $this->serviceLocator,
109
            'acmailer.mailoptions.another'
110
        );
111
        $this->assertEquals(['[email protected]'], $mailOptions->getMessageOptions()->getTo());
112
        $this->assertEquals('Me', $mailOptions->getMessageOptions()->getFrom());
113
    }
114
115
    public function testExtendWithValueNullIsIgnored()
116
    {
117
        $this->serviceLocator = new ServiceManagerMock([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \AcMailerTest\Servic...', 'from' => 'Me')))))) of type object<AcMailerTest\Serv...ger\ServiceManagerMock> is incompatible with the declared type object<Zend\ServiceManag...erviceLocatorInterface> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
118
            'Config' => [
119
                'acmailer_options' => [
120
                    'default' => [
121
                        'extends' => null,
122
                        'message_options' => [
123
                            'to'    => '[email protected]',
124
                            'from'  => 'Me',
125
                        ]
126
                    ],
127
                ]
128
            ]
129
        ]);
130
131
        /** @var MailOptions $mailOptions */
132
        $mailOptions = $this->mailOptionsFactory->__invoke(
133
            $this->serviceLocator,
134
            'acmailer.mailoptions.default'
135
        );
136
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
137
    }
138
139
    public function testExtendsSingleChaining()
140
    {
141
        $this->serviceLocator = new ServiceManagerMock([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \AcMailerTest\Servic... => '[email protected]')))))) of type object<AcMailerTest\Serv...ger\ServiceManagerMock> is incompatible with the declared type object<Zend\ServiceManag...erviceLocatorInterface> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
142
            'Config' => [
143
                'acmailer_options' => [
144
                    'default' => [
145
                        'extends' => null,
146
                        'message_options' => [
147
                            'to'    => '[email protected]'
148
                        ]
149
                    ],
150
                    'foo' => [
151
                        'extends' => 'default',
152
                        'message_options' => [
153
                            'from' => '[email protected]'
154
                        ]
155
                    ]
156
                ]
157
            ]
158
        ]);
159
160
        /** @var MailOptions $mailOptions */
161
        $mailOptions = $this->mailOptionsFactory->__invoke(
162
            $this->serviceLocator,
163
            'acmailer.mailoptions.foo'
164
        );
165
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
166
        $this->assertEquals(
167
            [
168
                'to'   => [['[email protected]']],
169
                'from' => '[email protected]',
170
            ],
171
            [
172
                'to' => [$mailOptions->getMessageOptions()->getTo()],
173
                'from' => $mailOptions->getMessageOptions()->getFrom(),
174
            ]
175
        );
176
    }
177
178
    public function testExtendsDoubleChaining()
179
    {
180
        $this->serviceLocator = new ServiceManagerMock([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \AcMailerTest\Servic... 'Foobar subject')))))) of type object<AcMailerTest\Serv...ger\ServiceManagerMock> is incompatible with the declared type object<Zend\ServiceManag...erviceLocatorInterface> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
181
            'Config' => [
182
                'acmailer_options' => [
183
                    'default' => [
184
                        'extends' => null,
185
                        'message_options' => [
186
                            'to'    => '[email protected]',
187
                        ]
188
                    ],
189
                    'foo' => [
190
                        'extends' => 'default',
191
                        'message_options' => [
192
                            'from' => '[email protected]'
193
                        ]
194
                    ],
195
                    'bar' => [
196
                        'extends' => 'foo',
197
                        'message_options' => [
198
                            'to' => '[email protected]',
199
                            'subject' => 'Foobar subject'
200
                        ]
201
                    ]
202
                ]
203
            ]
204
        ]);
205
206
        /** @var MailOptions $mailOptions */
207
        $mailOptions = $this->mailOptionsFactory->__invoke(
208
            $this->serviceLocator,
209
            'acmailer.mailoptions.bar'
210
        );
211
        $this->assertInstanceOf('AcMailer\Options\MailOptions', $mailOptions);
212
        $this->assertEquals(
213
            [
214
                'to' => [['[email protected]']],
215
                'from' => '[email protected]',
216
                'subject' => 'Foobar subject'
217
            ],
218
            [
219
                'to' => [$mailOptions->getMessageOptions()->getTo()],
220
                'from' => $mailOptions->getMessageOptions()->getFrom(),
221
                'subject' => $mailOptions->getMessageOptions()->getSubject()
222
            ]
223
        );
224
    }
225
226
    protected function initServiceManager($mailConfigKey = 'acmailer_options', $serviceName = 'default')
227
    {
228
        $services = [
229
            'Config' => [
230
                $mailConfigKey => [
231
                    $serviceName => [
232
                        'message_options' => [
233
                            'to'    => '[email protected]',
234
                            'from'  => 'Me',
235
                        ]
236
                    ]
237
                ]
238
            ]
239
        ];
240
        $this->serviceLocator = new ServiceManagerMock($services);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \AcMailerTest\Servic...eManagerMock($services) of type object<AcMailerTest\Serv...ger\ServiceManagerMock> is incompatible with the declared type object<Zend\ServiceManag...erviceLocatorInterface> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
241
        return $services;
242
    }
243
}
244