|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Mandrill\Test; |
|
4
|
|
|
|
|
5
|
|
|
use LeKoala\Mandrill\MandrillApiTransport; |
|
6
|
|
|
use SilverStripe\Core\Environment; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
use LeKoala\Mandrill\MandrillHelper; |
|
9
|
|
|
use Mandrill; |
|
10
|
|
|
use SilverStripe\Control\Email\Email; |
|
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
12
|
|
|
use Symfony\Component\Mailer\Mailer; |
|
13
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Test for Mandrill |
|
17
|
|
|
* |
|
18
|
|
|
* @group Mandrill |
|
19
|
|
|
*/ |
|
20
|
|
|
class MandrillTest extends SapphireTest |
|
21
|
|
|
{ |
|
22
|
|
|
protected $testMailer; |
|
23
|
|
|
protected $isDummy = false; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
parent::setUp(); |
|
28
|
|
|
|
|
29
|
|
|
// add dummy api key |
|
30
|
|
|
if (!MandrillHelper::getAPIKey()) { |
|
31
|
|
|
$this->isDummy = true; |
|
32
|
|
|
Environment::setEnv('MANDRILL_API_KEY', 'dummy'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->testMailer = Injector::inst()->get(MailerInterface::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function tearDown(): void |
|
39
|
|
|
{ |
|
40
|
|
|
parent::tearDown(); |
|
41
|
|
|
|
|
42
|
|
|
Injector::inst()->registerService($this->testMailer, MailerInterface::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testSetup() |
|
46
|
|
|
{ |
|
47
|
|
|
$inst = MandrillHelper::registerTransport(); |
|
48
|
|
|
$mailer = MandrillHelper::getMailer(); |
|
49
|
|
|
$instClass = get_class($inst); |
|
50
|
|
|
$instMailer = get_class($mailer); |
|
51
|
|
|
$this->assertEquals($instClass, $instMailer); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testSending() |
|
55
|
|
|
{ |
|
56
|
|
|
$test_to = Environment::getEnv('MANDRILL_TEST_TO'); |
|
57
|
|
|
$test_from = Environment::getEnv('MANDRILL_TEST_FROM'); |
|
58
|
|
|
|
|
59
|
|
|
$mailer = MandrillHelper::registerTransport(); |
|
60
|
|
|
|
|
61
|
|
|
$email = new Email(); |
|
62
|
|
|
$email->setSubject('Test email'); |
|
63
|
|
|
$email->setBody("Body of my email"); |
|
64
|
|
|
|
|
65
|
|
|
if (!$test_from || !$test_to || $this->isDummy) { |
|
66
|
|
|
$test_to = "example@localhost"; |
|
67
|
|
|
$test_from = "sender@localhost"; |
|
68
|
|
|
// don't try to send it for real |
|
69
|
|
|
$email->getHeaders()->addTextHeader('X-SendingDisabled', "true"); |
|
70
|
|
|
} |
|
71
|
|
|
$email->setTo($test_to); |
|
72
|
|
|
$email->setFrom($test_from); |
|
73
|
|
|
|
|
74
|
|
|
// This is async, therefore it does not return anything anymore |
|
75
|
|
|
$email->send(); |
|
76
|
|
|
|
|
77
|
|
|
$transport = MandrillHelper::getTransportFromMailer($mailer); |
|
|
|
|
|
|
78
|
|
|
$result = $transport->getApiResult(); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$firstMail = $result[0] ?? []; |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals($test_to, $firstMail['email']); |
|
83
|
|
|
$this->assertEquals("sent", $firstMail['status']); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|