Completed
Pull Request — master (#219)
by Kevin
01:39
created

SymfonyMailerReporterTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 2
A testSendMail() 0 22 1
A getTestData() 0 10 1
1
<?php
2
3
namespace Liip\MonitorBundle\Tests\Helper;
4
5
use Liip\MonitorBundle\Helper\SymfonyMailerReporter;
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Symfony\Component\Mailer\MailerInterface;
9
use Symfony\Component\Mime\Address;
10
use Symfony\Component\Mime\Email;
11
use ZendDiagnostics\Check\CheckInterface;
12
use ZendDiagnostics\Result\Collection;
13
use ZendDiagnostics\Result\Failure;
14
15
class SymfonyMailerReporterTest extends TestCase
16
{
17
    /**
18
     * @var \Prophecy\Prophecy\ObjectProphecy|MailerInterface
19
     */
20
    private $mailer;
21
22
    protected function setUp(): void
23
    {
24
        if (!interface_exists(MailerInterface::class)) {
25
            $this->markTestSkipped('Symfony Mailer not available.');
26
        }
27
28
        $this->mailer = $this->prophesize(MailerInterface::class);
29
    }
30
31
    /**
32
     * @dataProvider getTestData
33
     */
34
    public function testSendMail(array $recipients, string $sender, string $subject)
35
    {
36
        $reporter = new SymfonyMailerReporter($this->mailer->reveal(), $recipients, $sender, $subject);
37
38
        $check = $this->prophesize(CheckInterface::class);
39
        $check->getLabel()->willReturn('Some Label');
40
41
        $checks = new Collection();
42
        $checks[$check->reveal()] = new Failure('Something goes wrong');
43
44
        $this->mailer->send(Argument::that(function(Email $message) use ($recipients, $sender, $subject): bool {
45
            $this->assertEquals(Address::createArray($recipients), $message->getTo(), 'Check if Recipient is sent correctly.');
46
            $this->assertEquals([Address::create($sender)], $message->getFrom(), 'Check that the from header is set correctly.');
47
            $this->assertSame($subject, $message->getSubject(), 'Check that the subject has been set.');
48
49
            $this->assertSame('[Some Label] Something goes wrong', $message->getTextBody(), 'Check if the text body has been set.');
50
51
            return true;
52
        }))->shouldBeCalled();
53
54
        $reporter->onFinish($checks);
55
    }
56
57
    public static function getTestData(): iterable
58
    {
59
        return [
60
            [
61
                ['[email protected]'],
62
                '[email protected]',
63
                'Something went wrogin'
64
            ]
65
        ];
66
    }
67
}
68