EmailServiceTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * @author Antoine Hedgcock
4
 */
5
6
namespace EmailTemplatesTest\Service;
7
8
use PHPUnit_Framework_MockObject_MockObject;
9
use PHPUnit_Framework_TestCase;
10
use Roave\EmailTemplates\Options\EmailServiceOptions;
11
use Roave\EmailTemplates\Service\EmailService;
12
use Roave\EmailTemplates\Service\TemplateServiceInterface;
13
use Zend\Mail\Message;
14
use Zend\Mail\Transport\TransportInterface;
15
16
/**
17
 * Class EmailServiceTest
18
 *
19
 * @coversDefaultClass \Roave\EmailTemplates\Service\EmailService
20
 * @covers ::<!public>
21
 *
22
 * @group service
23
 */
24
class EmailServiceTest extends PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var EmailServiceOptions
28
     */
29
    protected $options;
30
31
    /**
32
     * @var TemplateServiceInterface|PHPUnit_Framework_MockObject_MockObject
33
     */
34
    protected $templateService;
35
36
    /**
37
     * @var EmailService
38
     */
39
    protected $service;
40
41
    /**
42
     * @covers ::__construct
43
     */
44
    protected function setUp()
45
    {
46
        $this->options         = new EmailServiceOptions();
47
        $this->templateService = $this->createMock(TemplateServiceInterface::class);
48
49
        $this->service = new EmailService(
50
            $this->templateService,
51
            $this->options
52
        );
53
    }
54
55
    private function mockRender()
56
    {
57
        $this->templateService
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Roave\EmailTemplates\Ser...emplateServiceInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
            ->expects($this->any())
59
            ->method('render')
60
            ->willReturn(['subject', 'html', 'text']);
61
    }
62
63
    /**
64
     * @covers ::setTransport
65
     */
66
    public function testSetTransport()
67
    {
68
        $this->service->setTransport($this->createMock(TransportInterface::class));
69
    }
70
71
    /**
72
     * @covers ::getTransport
73
     */
74
    public function testGetTransport()
75
    {
76
        $transport = $this->service->getTransport();
77
78
        $this->assertInstanceOf($this->options->getDefaultTransport(), $transport);
79
80
        $transport = $this->createMock(TransportInterface::class);
81
        $this->service->setTransport($transport);
82
83
        $this->assertSame($transport, $this->service->getTransport());
84
    }
85
86
    /**
87
     * @covers ::send
88
     */
89
    public function testSend()
90
    {
91
        $email      = '[email protected]';
92
        $locale     = 'sv_SE';
93
        $templateId = 'roave:contact';
94
        $parameters = ['name' => 'Antoine'];
95
96
        $this->mockRender();
97
98
        $transport = $this->createMock(TransportInterface::class);
99
        $transport
100
            ->expects($this->once())
101
            ->method('send')
102
            ->with($this->isInstanceOf(Message::class));
103
104
        $this->service->setTransport($transport);
105
        $this->service->send($email, $templateId, $parameters, $locale);
106
    }
107
108
    /**
109
     * @covers ::send
110
     */
111
    public function testSendHasCorrectContentType()
112
    {
113
        $email      = '[email protected]';
114
        $locale     = 'sv_SE';
115
        $templateId = 'roave:contact';
116
        $parameters = ['name' => 'Hotas'];
117
118
        $this->mockRender();
119
120
        $transport = $this->createMock(TransportInterface::class);
121
        $transport
122
            ->expects($this->once())
123
            ->method('send')
124
            ->with($this->isInstanceOf(Message::class))
125
            ->will($this->returnCallback(function(Message $message) {
126
                $contentType = $message->getHeaders()->get('content-type');
127
                $this->assertContains('multipart/alternative', $contentType->getFieldValue());
128
            }));
129
130
        $this->service->setTransport($transport);
131
        $this->service->send($email, $templateId, $parameters, $locale);
132
    }
133
134
    /**
135
     * @covers ::send
136
     */
137
    public function testSendCanOverrideReplyToHeader()
138
    {
139
        $email      = '[email protected]';
140
        $locale     = 'sv_SE';
141
        $templateId = 'roave:contact';
142
        $parameters = ['name' => 'Hotas'];
143
        $replyTo    = '[email protected]';
144
145
        $this->mockRender();
146
147
        $transport = $this->createMock(TransportInterface::class);
148
        $transport
149
            ->expects($this->at(0))
150
            ->method('send')
151
            ->with($this->isInstanceOf(Message::class))
152
            ->will($this->returnCallback(function(Message $message) {
153
                $replyToField = $message->getHeaders()->get('Reply-To');
154
                $this->assertContains($this->options->getReplyTo(), $replyToField->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Mail\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            }));
156
157
        $transport
158
            ->expects($this->at(1))
159
            ->method('send')
160
            ->with($this->isInstanceOf(Message::class))
161
            ->will($this->returnCallback(function(Message $message) use ($replyTo) {
162
                $replyToField = $message->getHeaders()->get('Reply-To');
163
                $this->assertContains($replyTo, $replyToField->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Mail\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
164
            }));
165
166
        $this->service->setTransport($transport);
167
        $this->service->send($email, $templateId, $parameters, $locale, null);
168
        $this->service->send($email, $templateId, $parameters, $locale, $replyTo);
169
    }
170
}
171