|
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('theBody', 'theSubject', ['[email protected]'], null, null, null, null, '[email protected]'); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertInstanceOf('AcMailer\Result\ResultInterface', $result); |
|
88
|
|
|
$this->assertEquals('[email protected]', $this->service->getMessage()->getReplyTo()->current()->getEmail()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testBodyIsValidAsViewModel() |
|
92
|
|
|
{ |
|
93
|
|
|
$result = $this->plugin->__invoke(new ViewModel()); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertInstanceOf('AcMailer\Result\ResultInterface', $result); |
|
96
|
|
|
$this->assertEquals('ViewModel body', $this->service->getMessage()->getBody()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testMailServiceAwareness() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->assertSame($this->service, $this->plugin->getMailService()); |
|
102
|
|
|
$anotherService = new MailServiceMock(); |
|
103
|
|
|
$this->assertSame($this->plugin, $this->plugin->setMailService($anotherService)); |
|
104
|
|
|
$this->assertSame($anotherService, $this->plugin->getMailService()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|