MailgunTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 10
dl 0
loc 63
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A tearDown() 0 6 1
A testSetup() 0 6 1
A testDomains() 0 8 1
A testSending() 0 19 3
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());
0 ignored issues
show
Deprecated Code introduced by
The class Swift_MailTransport has been deprecated with message: since 5.4.5 (to be removed in 6.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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();
0 ignored issues
show
Unused Code introduced by
$inst is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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