Completed
Branch master (7dba2f)
by Alejandro
06:12
created

EmailBuilderTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 105
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 27 1
A requestedEmailIsProperlyBuildWhenFound() 0 8 1
A provideEmails() 0 10 1
A exceptionIsThrownWhenInvalidEmailIsRequested() 0 6 1
A emailCanBeExtended() 0 20 1
A circularExtendsThrowException() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace AcMailerTest\Model;
5
6
use AcMailer\Exception\EmailNotFoundException;
7
use AcMailer\Exception\InvalidArgumentException;
8
use AcMailer\Model\Email;
9
use AcMailer\Model\EmailBuilder;
10
use PHPUnit\Framework\TestCase;
11
12
class EmailBuilderTest extends TestCase
13
{
14
    /**
15
     * @var EmailBuilder
16
     */
17
    private $builder;
18
19
    public function setUp()
20
    {
21
        $this->builder = new EmailBuilder([
22
            'an_email' => [
23
                'fromName' => 'foobar',
24
                'cc' => [
25
                    '[email protected]',
26
                ],
27
            ],
28
            'another_email' => [
29
                'fromName' => 'something',
30
                'bcc' => [
31
                    '[email protected]',
32
                ],
33
            ],
34
            'extended_email' => [
35
                'extends' => 'an_email',
36
            ],
37
38
            'invalid_extends' => [
39
                'extends' => 'another',
40
            ],
41
            'another' => [
42
                'extends' => 'invalid_extends',
43
            ],
44
        ]);
45
    }
46
47
    /**
48
     * @test
49
     * @dataProvider provideEmails
50
     * @param string $emailName
51
     * @param string $expectedFromName
52
     * @param array $options
53
     */
54
    public function requestedEmailIsProperlyBuildWhenFound(
55
        string $emailName,
56
        string $expectedFromName,
57
        array $options = []
58
    ) {
59
        $email = $this->builder->build($emailName, $options);
60
        $this->assertEquals($expectedFromName, $email->getFromName());
61
    }
62
63
    public function provideEmails(): array
64
    {
65
        return [
66
            'an_email' => ['an_email', 'foobar'],
67
            'another_email' => ['another_email', 'something'],
68
            'another_email with option overridden' => ['another_email', 'overridden', ['fromName' => 'overridden']],
69
            'default email' => [Email::class, ''],
70
            'default email with overridden value' => [Email::class, 'me', ['fromName' => 'me']],
71
        ];
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function exceptionIsThrownWhenInvalidEmailIsRequested()
78
    {
79
        $this->expectException(EmailNotFoundException::class);
80
        $this->expectExceptionMessage('An email with name "invalid" could not be found in registered emails list');
81
        $this->builder->build('invalid');
82
    }
83
84
    /**
85
     * @test
86
     */
87
    public function emailCanBeExtended()
88
    {
89
        $email = $this->builder->build('an_email', ['extends' => 'another_email']);
90
91
        $this->assertEquals([
92
            '[email protected]',
93
        ], $email->getCc());
94
        $this->assertEquals([
95
            '[email protected]',
96
        ], $email->getBcc());
97
        $this->assertEquals('foobar', $email->getFromName());
98
99
100
        $email = $this->builder->build('extended_email');
101
102
        $this->assertEquals([
103
            '[email protected]',
104
        ], $email->getCc());
105
        $this->assertEquals('foobar', $email->getFromName());
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function circularExtendsThrowException()
112
    {
113
        $this->expectException(InvalidArgumentException::class);
114
        $this->builder->build('invalid_extends');
115
    }
116
}
117