Completed
Push — master ( 80d7b6...36b73d )
by Antoine
32:38 queued 19:17
created

EmailServiceTest::mockRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
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->getMock(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->once())
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->getMock(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->getMock(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->getMock(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->getMock(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
}
136