Completed
Push — master ( 12bac9...cd01bf )
by Alejandro
9s
created

testArgumentsAreProperlyMapped()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 24
rs 8.9713
c 2
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
namespace AcMailerTest\Controller\Plugin;
3
4
use AcMailer\Controller\Plugin\SendMailPlugin;
5
use AcMailer\Service\MailServiceInterface;
6
use AcMailer\Service\MailServiceMock;
7
use PHPUnit_Framework_TestCase as TestCase;
8
use Zend\View\Model\ViewModel;
9
10
/**
11
 * Class SendMailPluginTest
12
 * @author Alejandro Celaya Alastrué
13
 * @link http://www.alejandrocelaya.com
14
 */
15
class SendMailPluginTest extends TestCase
16
{
17
    /**
18
     * @var SendMailPlugin
19
     */
20
    private $plugin;
21
    /**
22
     * @var MailServiceInterface
23
     */
24
    private $service;
25
26
    public function setUp()
27
    {
28
        $this->service = new MailServiceMock();
29
        $this->plugin = new SendMailPlugin($this->service);
30
    }
31
32
    public function testInvokeWithNoArgumentsReturnsTheService()
33
    {
34
        $this->assertInstanceOf('AcMailer\Service\MailServiceInterface', $this->plugin->__invoke());
35
    }
36
37
    public function testFirstArgumentArrayIsTreatedAsConfig()
38
    {
39
        $config = [
40
            'body' => 'foobar',
41
            'subject' => 'barfoo'
42
        ];
43
44
        $result = $this->plugin->__invoke($config);
45
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
46
        $this->assertEquals($config['body'], $this->service->getMessage()->getBody());
47
        $this->assertEquals($config['subject'], $this->service->getMessage()->getSubject());
48
    }
49
50
    public function testArgumentsAreProperlyMapped()
51
    {
52
        $result = $this->plugin->__invoke(
53
            'theBody',
54
            'theSubject',
55
            ['[email protected]'],
56
            ['[email protected]' => 'From Me'],
57
            ['[email protected]'],
58
            ['[email protected]'],
59
            ['attachments/attachment1.zip', 'attachments/attachment2.zip'],
60
            ['[email protected]' => 'Reply To Me']
61
        );
62
63
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
64
        $this->assertEquals('theBody', $this->service->getMessage()->getBody());
65
        $this->assertEquals('theSubject', $this->service->getMessage()->getSubject());
66
        $this->assertEquals('[email protected]', $this->service->getMessage()->getTo()->current()->getEmail());
67
        $this->assertEquals('[email protected]', $this->service->getMessage()->getFrom()->current()->getEmail());
68
        $this->assertEquals('From Me', $this->service->getMessage()->getFrom()->current()->getName());
69
        $this->assertEquals('[email protected]', $this->service->getMessage()->getCc()->current()->getEmail());
70
        $this->assertEquals('[email protected]', $this->service->getMessage()->getBcc()->current()->getEmail());
71
        $this->assertEquals('[email protected]', $this->service->getMessage()->getReplyTo()->current()->getEmail());
72
        $this->assertEquals('Reply To Me', $this->service->getMessage()->getReplyTo()->current()->getName());
73
    }
74
75
    public function testFromIsValidAsString()
76
    {
77
        $result = $this->plugin->__invoke('theBody', 'theSubject', ['[email protected]'], '[email protected]');
78
79
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
80
        $this->assertEquals('[email protected]', $this->service->getMessage()->getFrom()->current()->getEmail());
81
    }
82
    
83
    public function testReplyToIsValidAsString()
84
    {
85
        $result = $this->plugin->__invoke(
86
            'theBody',
87
            'theSubject',
88
            ['[email protected]'],
89
            null,
90
            null,
91
            null,
92
            null,
93
            '[email protected]'
94
        );
95
96
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
97
        $this->assertEquals('[email protected]', $this->service->getMessage()->getReplyTo()->current()->getEmail());
98
    }
99
100
    public function testBodyIsValidAsViewModel()
101
    {
102
        $result = $this->plugin->__invoke(new ViewModel());
103
104
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
105
        $this->assertEquals('ViewModel body', $this->service->getMessage()->getBody());
106
    }
107
108
    public function testMailServiceAwareness()
109
    {
110
        $this->assertSame($this->service, $this->plugin->getMailService());
111
        $anotherService = new MailServiceMock();
112
        $this->assertSame($this->plugin, $this->plugin->setMailService($anotherService));
113
        $this->assertSame($anotherService, $this->plugin->getMailService());
114
    }
115
}
116