attachmentsAreProperlyAddedToMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace AcMailerTest\Service;
5
6
use AcMailer\Attachment\AttachmentParserManagerInterface;
7
use AcMailer\Attachment\Parser\AttachmentParserInterface;
8
use AcMailer\Event\MailListenerInterface;
9
use AcMailer\Exception\InvalidArgumentException;
10
use AcMailer\Exception\MailException;
11
use AcMailer\Model\Email;
12
use AcMailer\Model\EmailBuilderInterface;
13
use AcMailer\Service\MailService;
14
use PHPUnit\Framework\TestCase;
15
use Prophecy\Argument;
16
use Prophecy\Prophecy\ObjectProphecy;
17
use Zend\EventManager\EventManagerInterface;
18
use Zend\EventManager\ResponseCollection;
19
use Zend\Expressive\Template\TemplateRendererInterface;
20
use Zend\Mail\Message;
21
use Zend\Mail\Transport\TransportInterface;
22
use Zend\Mime\Part;
23
24
class MailServiceTest extends TestCase
25
{
26
    /**
27
     * @var MailService
28
     */
29
    private $mailService;
30
    /**
31
     * @var ObjectProphecy
32
     */
33
    private $transport;
34
    /**
35
     * @var ObjectProphecy
36
     */
37
    private $renderer;
38
    /**
39
     * @var ObjectProphecy
40
     */
41
    private $emailBuilder;
42
    /**
43
     * @var ObjectProphecy
44
     */
45
    private $eventManager;
46
    /**
47
     * @var ObjectProphecy
48
     */
49
    private $attachmentParsers;
50
51
    public function setUp()
52
    {
53
        $this->transport = $this->prophesize(TransportInterface::class);
54
        $this->renderer = $this->prophesize(TemplateRendererInterface::class);
55
        $this->emailBuilder = $this->prophesize(EmailBuilderInterface::class);
56
        $this->attachmentParsers = $this->prophesize(AttachmentParserManagerInterface::class);
57
        $this->eventManager = $this->prophesize(EventManagerInterface::class);
58
59
        $this->eventManager->setIdentifiers(Argument::cetera())->willReturn(null);
60
61
        $this->mailService = new MailService(
62
            $this->transport->reveal(),
63
            $this->renderer->reveal(),
64
            $this->emailBuilder->reveal(),
65
            $this->attachmentParsers->reveal(),
66
            $this->eventManager->reveal()
67
        );
68
    }
69
70
    /**
71
     * @test
72
     * @dataProvider provideInvalidEmails
73
     * @param $email
74
     */
75
    public function sendInvalidEmailThrowsException($email)
76
    {
77
        $this->expectException(InvalidArgumentException::class);
78
        $this->mailService->send($email);
79
    }
80
81
    public function provideInvalidEmails(): array
82
    {
83
        return [
84
            [null],
85
            [new \stdClass()],
86
            [50],
87
        ];
88
    }
89
90
    /**
91
     * @test
92
     * @dataProvider provideValidEmails
93
     * @param $email
94
     */
95 View Code Duplication
    public function validEmailIsProperlySent($email)
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...
96
    {
97
        $buildEmail = $this->emailBuilder->build(Argument::cetera())->willReturn(new Email());
98
        $send = $this->transport->send(Argument::type(Message::class))->willReturn(null);
99
        $trigger = $this->eventManager->triggerEvent(Argument::cetera())->willReturn(new ResponseCollection());
100
101
        $this->mailService->send($email);
102
103
        $buildEmail->shouldHaveBeenCalledTimes(\is_object($email) ? 0 : 1);
104
        $send->shouldHaveBeenCalled();
105
        $trigger->shouldHaveBeenCalledTimes(2);
106
    }
107
108
    public function provideValidEmails(): array
109
    {
110
        return [
111
            ['the_email'],
112
            [[]],
113
            [new Email()],
114
        ];
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function exceptionIsThrownInCaseOfError()
121
    {
122
        $this->transport->send(Argument::type(Message::class))->willThrow(\Exception::class)
123
                                                              ->shouldBeCalled();
124
        $this->eventManager->triggerEvent(Argument::cetera())->willReturn(new ResponseCollection())
125
                                                             ->shouldBeCalled();
126
127
        $this->expectException(MailException::class);
128
        $this->mailService->send(new Email());
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function whenPreSendReturnsFalseEmailsSendingIsCancelled()
135
    {
136
        $collections = new ResponseCollection();
137
        $collections->add(0, false);
0 ignored issues
show
Bug introduced by
The method add() does not seem to exist on object<Zend\EventManager\ResponseCollection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
139
        $send = $this->transport->send(Argument::type(Message::class))->willReturn(null);
140
        $trigger = $this->eventManager->triggerEvent(Argument::cetera())->willReturn($collections);
141
142
        $this->mailService->send(new Email());
143
144
        $send->shouldNotHaveBeenCalled();
145
        $trigger->shouldHaveBeenCalledTimes(1);
146
    }
147
148
    /**
149
     * @test
150
     */
151
    public function attachListeners()
152
    {
153
        $listener = $this->prophesize(MailListenerInterface::class);
154
155
        $listener->attach(Argument::cetera())->shouldBeCalled();
156
        $listener->detach(Argument::cetera())->shouldBeCalled();
157
158
        $this->mailService->attachMailListener($listener->reveal());
159
        $this->mailService->detachMailListener($listener->reveal());
160
    }
161
162
    /**
163
     * @test
164
     */
165 View Code Duplication
    public function templateIsRendererIfProvided()
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...
166
    {
167
        $send = $this->transport->send(Argument::type(Message::class))->willReturn(null);
168
        $trigger = $this->eventManager->triggerEvent(Argument::cetera())->willReturn(new ResponseCollection());
169
        $render = $this->renderer->render(Argument::cetera())->willReturn('');
170
171
        $this->mailService->send((new Email())->setTemplate('some/template'));
172
173
        $send->shouldHaveBeenCalledTimes(1);
174
        $trigger->shouldHaveBeenCalled();
175
        $render->shouldHaveBeenCalledTimes(1);
176
    }
177
178
    /**
179
     * @test
180
     */
181
    public function attachmentsAreProperlyAddedToMessage()
182
    {
183
        $attachmentParser = $this->prophesize(AttachmentParserInterface::class);
184
        $parse = $attachmentParser->parse(Argument::cetera())->willReturn(new Part());
185
186
        $hasStringParser = $this->attachmentParsers->has('string')->willReturn(true);
187
        $hasArrayParser = $this->attachmentParsers->has('array')->willReturn(false);
188
        $getStringParser = $this->attachmentParsers->get('string')->willReturn($attachmentParser->reveal());
189
190
        $send = $this->transport->send(Argument::type(Message::class))->willReturn(null);
191
        $trigger = $this->eventManager->triggerEvent(Argument::cetera())->willReturn(new ResponseCollection());
192
193
        $this->mailService->send((new Email())->setAttachments(['', '', '', []]));
194
195
        $send->shouldHaveBeenCalled();
196
        $trigger->shouldHaveBeenCalled();
197
        $hasStringParser->shouldHaveBeenCalled();
198
        $hasArrayParser->shouldHaveBeenCalled();
199
        $getStringParser->shouldHaveBeenCalled();
200
        $parse->shouldHaveBeenCalledTimes(3);
201
    }
202
}
203