MandrillTest::testSetup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 3
b 1
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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);
0 ignored issues
show
Bug introduced by
$mailer of type LeKoala\Mandrill\MandrillApiTransport is incompatible with the type Symfony\Component\Mailer\MailerInterface expected by parameter $mailer of LeKoala\Mandrill\Mandril...etTransportFromMailer(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        $transport = MandrillHelper::getTransportFromMailer(/** @scrutinizer ignore-type */ $mailer);
Loading history...
78
        $result = $transport->getApiResult();
0 ignored issues
show
Bug introduced by
The method getApiResult() does not exist on Symfony\Component\Mailer...sport\AbstractTransport. It seems like you code against a sub-type of Symfony\Component\Mailer...sport\AbstractTransport such as LeKoala\Mandrill\MandrillApiTransport. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        /** @scrutinizer ignore-call */ 
79
        $result = $transport->getApiResult();
Loading history...
79
80
        $firstMail = $result[0] ?? [];
81
82
        $this->assertEquals($test_to, $firstMail['email']);
83
        $this->assertEquals("sent", $firstMail['status']);
84
    }
85
}
86