Completed
Push — develop ( f3c189...e92436 )
by Alejandro
08:55
created

MailServiceMock::setSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace AcMailer\Service;
3
4
use AcMailer\Result\MailResult;
5
use AcMailer\View\DefaultLayoutInterface;
6
use Zend\Mail\Message;
7
use Zend\Mail\Transport\TransportInterface;
8
use Zend\View\Model\ViewModel;
9
use Zend\View\Renderer\RendererInterface;
10
use AcMailer\Result\ResultInterface;
11
use AcMailer\Exception\InvalidArgumentException;
12
13
/**
14
 * This class is meant to supplant MailService when unit testing elements that depend on a MailServiceInterface.
15
 * Remember to always program to abstractions, never concretions.
16
 * @author Alejandro Celaya Alastrué
17
 * @link http://www.alejandrocelaya.com
18
 */
19
class MailServiceMock implements MailServiceInterface
20
{
21
    /**
22
     * @var bool
23
     */
24
    private $sendMethodCalled = false;
25
    /**
26
     * @var bool
27
     */
28
    private $forceError = false;
29
30
    /**
31
     * @var Message
32
     */
33
    private $message;
34
    /**
35
     * @var RendererInterface
36
     */
37
    private $renderer;
38
    /**
39
     * @var TransportInterface
40
     */
41
    private $transport;
42
43
    /**
44
     * @var array
45
     */
46
    private $attachments = [];
47
48
    public function __construct()
49
    {
50
        $this->message = new Message();
51
    }
52
53
    /**
54
     * Tries to send the message, returning a MailResult object
55
     * @return ResultInterface
56
     */
57
    public function send()
58
    {
59
        $this->sendMethodCalled = true;
60
        if ($this->forceError) {
61
            return new MailResult(false, 'Error!!');
62
        } else {
63
            return new MailResult();
64
        }
65
    }
66
67
    /**
68
     * Sets the message body
69
     * @param \Zend\Mime\Part|\Zend\Mime\Message|string $body
70
     * @param string $charset
71
     * @throws InvalidArgumentException
72
     * @return $this
73
     */
74
    public function setBody($body, $charset = null)
75
    {
76
        $this->message->setBody($body);
77
        return $this;
78
    }
79
    /**
80
     * Sets the template to be used to create the body of the email
81
     * @param string|\Zend\View\Model\ViewModel $template
82
     * @param array $params
83
     * @return $this
84
     */
85
    public function setTemplate($template, array $params = [])
86
    {
87
        $this->message->setBody($template instanceof ViewModel ? 'ViewModel body' : $template);
88
        return $this;
89
    }
90
    /**
91
     * Sets the message subject
92
     * @param string $subject
93
     * @deprecated Use $mailService->getMessage()->setSubject() instead
94
     */
95
    public function setSubject($subject)
96
    {
97
        $this->message->setSubject($subject);
98
    }
99
    /**
100
     * Returns the message that is going to be sent when method send is called
101
     * @see \AcMailer\Service\MailServiceInterface::send()
102
     * @return \Zend\Mail\Message
103
     */
104
    public function getMessage()
105
    {
106
        return $this->message;
107
    }
108
109
    /**
110
     * Tells if send() method was previously called
111
     * @see \AcMailer\Service\MailServiceMock::send()
112
     * @return boolean True if send() was called, false otherwise
113
     */
114
    public function isSendMethodCalled()
115
    {
116
        return $this->sendMethodCalled;
117
    }
118
119
    /**
120
     * Sets the type of result produced when send method is called
121
     * @see \AcMailer\Service\MailServiceMock::send()
122
     * @param boolean $forceError True if an error should occur. False otherwise
123
     */
124
    public function setForceError($forceError)
125
    {
126
        $this->forceError = (bool) $forceError;
127
    }
128
129
    /**
130
     * @param string $path
131
     * @param null $filename
132
     * @return $this
133
     */
134 View Code Duplication
    public function addAttachment($path, $filename = null)
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...
135
    {
136
        if (isset($filename)) {
137
            $this->attachments[$filename] = $path;
138
        } else {
139
            $this->attachments[] = $path;
140
        }
141
        return $this;
142
    }
143
144
    /**
145
     * @param array $paths
146
     * @return $this
147
     */
148
    public function addAttachments(array $paths)
149
    {
150
        return $this->setAttachments(array_merge($this->attachments, $paths));
151
    }
152
153
    /**
154
     * @param array $paths
155
     * @return $this
156
     */
157
    public function setAttachments(array $paths)
158
    {
159
        $this->attachments = $paths;
160
        return $this;
161
    }
162
163
    /**
164
     * Returns the list of attachments
165
     * @return array
166
     */
167
    public function getAttachments()
168
    {
169
        return $this->attachments;
170
    }
171
172
    /**
173
     * Returns the transport object that will be used to send the wrapped message
174
     * @return TransportInterface
175
     */
176
    public function getTransport()
177
    {
178
        return $this->transport;
179
    }
180
181
    /**
182
     * Returns the renderer object that will be used to render templates
183
     * @return RendererInterface
184
     */
185
    public function getRenderer()
186
    {
187
        return $this->renderer;
188
    }
189
190
    /**
191
     * @param RendererInterface $renderer
192
     * @return mixed
193
     */
194
    public function setRenderer(RendererInterface $renderer)
195
    {
196
        $this->renderer = $renderer;
197
        return $this;
198
    }
199
200
    /**
201
     * @param TransportInterface $transport
202
     * @return mixed
203
     */
204
    public function setTransport(TransportInterface $transport)
205
    {
206
        $this->transport = $transport;
207
        return $this;
208
    }
209
210
    /**
211
     * Sets the default layout to be used with all the templates set when calling setTemplate.
212
     *
213
     * @param DefaultLayoutInterface $layout
214
     * @return mixed
215
     */
216
    public function setDefaultLayout(DefaultLayoutInterface $layout = null)
217
    {
218
        // Do nothing
219
    }
220
}
221