Completed
Push — develop ( f11aab...a4697b )
by Alejandro
09:31
created

SendMailPluginTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 1
cbo 7
dl 0
loc 81
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvokeWithNoArgumentsReturnsTheService() 0 4 1
A setUp() 0 5 1
A testFirstArgumentArrayIsTreatedAsConfig() 0 12 1
A testArgumentsAreProperlyMapped() 0 21 1
A testFromIsValidAsString() 0 7 1
A testBodyIsValidAsViewModel() 0 7 1
A testMailServiceAwareness() 0 7 1
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
        );
61
62
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
63
        $this->assertEquals('theBody', $this->service->getMessage()->getBody());
64
        $this->assertEquals('theSubject', $this->service->getMessage()->getSubject());
65
        $this->assertEquals('[email protected]', $this->service->getMessage()->getTo()->current()->getEmail());
66
        $this->assertEquals('[email protected]', $this->service->getMessage()->getFrom()->current()->getEmail());
67
        $this->assertEquals('From Me', $this->service->getMessage()->getFrom()->current()->getName());
68
        $this->assertEquals('[email protected]', $this->service->getMessage()->getCc()->current()->getEmail());
69
        $this->assertEquals('[email protected]', $this->service->getMessage()->getBcc()->current()->getEmail());
70
    }
71
72
    public function testFromIsValidAsString()
73
    {
74
        $result = $this->plugin->__invoke('theBody', 'theSubject', ['[email protected]'], '[email protected]');
75
76
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
77
        $this->assertEquals('[email protected]', $this->service->getMessage()->getFrom()->current()->getEmail());
78
    }
79
80
    public function testBodyIsValidAsViewModel()
81
    {
82
        $result = $this->plugin->__invoke(new ViewModel());
83
84
        $this->assertInstanceOf('AcMailer\Result\ResultInterface', $result);
85
        $this->assertEquals('ViewModel body', $this->service->getMessage()->getBody());
86
    }
87
88
    public function testMailServiceAwareness()
89
    {
90
        $this->assertSame($this->service, $this->plugin->getMailService());
91
        $anotherService = new MailServiceMock();
92
        $this->assertSame($this->plugin, $this->plugin->setMailService($anotherService));
93
        $this->assertSame($anotherService, $this->plugin->getMailService());
94
    }
95
}
96