Completed
Push — develop ( e2b887...c09155 )
by
unknown
12:13
created

SendMailTest::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2015 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace CoreTest\Mail\MailService;
12
13
use Core\Mail\MailService;
14
use Core\Mail\Message;
15
use Zend\Mail\AddressList;
16
17
/**
18
 * Tests sending mails via MailService
19
 *
20
 * @covers \Core\Mail\MailService
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @group Core
23
 * @group Core.Mail
24
 * @group Core.Mail.MailService
25
 */
26
class SendMailTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * Class under Test
30
     *
31
     * @var MailService
32
     */
33
    private $target;
34
35
    public $expectedMail;
36
37
    public function setup()
38
    {
39
        $test = $this;
40
        $sendCallback = function($value) use ($test) {
41
            return $value === $test->expectedMail;
42
        };
43
        $transport = $this->getMockForAbstractClass('\Zend\Mail\Transport\TransportInterface');
44
        $transport->expects($this->once())->method('send')->with($this->callback($sendCallback));
45
46
        $target = new MailService();
47
        $target->setTransport($transport);
48
49
        $this->target = $target;
50
    }
51
52
    /**
53
     * @testdox Gets mail service when passed a string to send()
54
     */
55
    public function testRetrieveMailBeforeSending()
56
    {
57
        $mail = new \Core\Mail\Message();
58
59
        $this->target->setService('retrieveme', $mail);
60
        $this->expectedMail = $mail;
61
62
        $this->target->send('retrieveme');
63
    }
64
65
    public function provideFromAddresses()
66
    {
67
        return array(
68
            array(null),
69
            array('ownfrowm@address'),
70
        );
71
    }
72
73
    /**
74
     * @dataProvider provideFromAddresses
75
     *
76
     * @param string $from
77
     */
78
    public function testSetsDefaultFromAddressOrUsesFromAddressSetInMail($from)
79
    {
80
        $mail = new Message();
81
82
        $defaultFrom = 'default@from';
83
        if (null !== $from) {
84
            $mail->setFrom($from);
85
            $expectedFrom = $from;
86
        } else {
87
            $expectedFrom = $defaultFrom;
88
        }
89
90
        $this->expectedMail = $mail;
91
92
        $this->target->setFrom('default@from');
93
        $this->target->send($mail);
94
95
        $mailFrom = $mail->getFrom();
96
        $this->assertInstanceOf('\Zend\Mail\AddressList', $mailFrom);
97
        $mailFrom = $mailFrom->get($expectedFrom);
98
        $this->assertInstanceOf('\Zend\Mail\Address', $mailFrom);
99
        $mailFrom = $mailFrom->getEmail();
100
        $this->assertEquals($expectedFrom, $mailFrom);
101
    }
102
103
    /**
104
     * @testdox recipients gets overidden if override recipients are set
105
     */
106
    public function testOverrideRecipient()
107
    {
108
        $overrideEmail = 'overidden@email';
109
        $ccEmail="cc@email";
110
        $bccEmail="bcc@email";
111
        $toEmail="to@email";
112
        
113
        $recipients = new AddressList();
114
        $recipients->add($overrideEmail);
115
116
        $this->target->setOverrideRecipient($recipients);
117
118
        $mail = new Message();
119
        $mail->addTo($toEmail);
120
        $mail->addCc($ccEmail);
121
        $mail->addBcc($bccEmail);
122
123
        $this->expectedMail = $mail;
124
125
        $this->target->send($mail);
126
127
        $headers = $mail->getHeaders();
128
        $expectedTo = 'To: ' . $overrideEmail;
129
        $this->assertFalse($headers->has('cc'));
130
        $this->assertFalse($headers->has('bcc'));
131
        $this->assertTrue($headers->has('X-Original-Recipients'));
132
133
        $this->assertEquals($expectedTo, $headers->get('to')->toString());
0 ignored issues
show
Bug introduced by
The method toString 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...
134
        $this->assertEquals('X-Original-Recipients: To: ' . $toEmail . '; Cc: ' . $ccEmail . '; Bcc: ' . $bccEmail, $headers->get('X-Original-Recipients')->toString());
135
    }
136
137
    public function testSetsXMailerHeader()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
138
    {
139
        $mail = new Message();
140
141
        $this->expectedMail = $mail;
142
        $mailer = 'test/mailer';
143
144
        $this->target->setMailer($mailer);
145
        $this->target->send($mail);
146
147
        $headers = $mail->getHeaders();
148
149
        $this->assertTrue($headers->has('X-Mailer'));
150
        $this->assertEquals("X-Mailer: $mailer", $headers->get('X-Mailer')->toString());
0 ignored issues
show
Bug introduced by
The method toString 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...
151
    }
152
}