1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Mailgun\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\Core\Environment; |
7
|
|
|
use SilverStripe\Control\Email\Email; |
8
|
|
|
use LeKoala\Mailgun\MailgunHelper; |
9
|
|
|
use Mailgun\Model\Domain\IndexResponse; |
10
|
|
|
use SilverStripe\Control\Email\Mailer; |
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
12
|
|
|
use SilverStripe\Control\Email\SwiftMailer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Test for Mailgun |
16
|
|
|
* |
17
|
|
|
* Make sure you have an api key set in yml for this to work |
18
|
|
|
* |
19
|
|
|
* @group Mailgun |
20
|
|
|
*/ |
21
|
|
|
class MailgunTest extends SapphireTest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Mailer |
25
|
|
|
*/ |
26
|
|
|
protected $testMailer; |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->testMailer = Injector::inst()->get(Mailer::class); |
33
|
|
|
|
34
|
|
|
// Ensure we have the right mailer |
35
|
|
|
$mailer = new SwiftMailer(); |
36
|
|
|
$swiftMailer = new \Swift_Mailer(new \Swift_MailTransport()); |
37
|
|
|
$mailer->setSwiftMailer($swiftMailer); |
38
|
|
|
Injector::inst()->registerService($mailer, Mailer::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function tearDown() |
42
|
|
|
{ |
43
|
|
|
parent::tearDown(); |
44
|
|
|
|
45
|
|
|
Injector::inst()->registerService($this->testMailer, Mailer::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testSetup() |
49
|
|
|
{ |
50
|
|
|
$inst = MailgunHelper::registerTransport(); |
51
|
|
|
$mailer = MailgunHelper::getMailer(); |
52
|
|
|
$this->assertTrue($inst === $mailer); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testDomains() |
56
|
|
|
{ |
57
|
|
|
$client = MailgunHelper::getClient(); |
58
|
|
|
$result = $client->domains()->index(); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($result instanceof IndexResponse); |
61
|
|
|
$this->assertNotEmpty($result->getDomains()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testSending() |
65
|
|
|
{ |
66
|
|
|
$test_to = Environment::getEnv('MAILGUN_TEST_TO'); |
67
|
|
|
$test_from = Environment::getEnv('MAILGUN_TEST_FROM'); |
68
|
|
|
if (!$test_from || !$test_to) { |
69
|
|
|
$this->markTestIncomplete("You must define tests environement variable: MAILGUN_TEST_TO, MAILGUN_TEST_FROM"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$inst = MailgunHelper::registerTransport(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$email = new Email(); |
75
|
|
|
$email->setTo($test_to); |
76
|
|
|
$email->setSubject('Test email'); |
77
|
|
|
$email->setBody("Body of my email"); |
78
|
|
|
$email->setFrom($test_from); |
79
|
|
|
$sent = $email->send(); |
80
|
|
|
|
81
|
|
|
$this->assertTrue(!!$sent); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.