Passed
Pull Request — master (#547)
by butschster
07:04
created

MessageTest::testSetDelayInDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Mailer;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Mailer\Message;
16
17
class MessageTest extends TestCase
18
{
19
    public function testDefaults(): void
20
    {
21
        $m = new Message('test', '[email protected]');
22
        $this->assertSame('test', $m->getSubject());
23
        $this->assertSame(['[email protected]'], $m->getTo());
24
        $this->assertSame([], $m->getData());
25
        $this->assertSame([], $m->getCC());
26
        $this->assertSame([], $m->getBCC());
27
        $this->assertNull($m->getFrom());
28
        $this->assertNull($m->getReplyTo());
29
    }
30
31
    public function testData(): void
32
    {
33
        $m = new Message('test', '[email protected]');
34
        $this->assertSame([], $m->getData());
35
36
        $m->setData(['hello' => 'world']);
37
        $this->assertSame(['hello' => 'world'], $m->getData());
38
    }
39
40
    public function testOptions(): void
41
    {
42
        $m = new Message('test', '[email protected]');
43
        $this->assertSame([], $m->getOptions());
44
45
        $m->setOptions(['hello' => 'world']);
46
        $this->assertSame(['hello' => 'world'], $m->getOptions());
47
48
        $m->setOption('k', 'v');
49
        $this->assertSame(['hello' => 'world', 'k' => 'v'], $m->getOptions());
50
    }
51
52
    public function testTo(): void
53
    {
54
        $m = new Message('test', ['[email protected]', '[email protected]']);
55
        $this->assertSame(['[email protected]', '[email protected]'], $m->getTo());
56
57
        $m->setTo('[email protected]');
58
        $this->assertSame(['[email protected]'], $m->getTo());
59
60
        $m->setTo('[email protected]', '[email protected]');
61
        $this->assertSame(['[email protected]', '[email protected]'], $m->getTo());
62
63
        $m->setTo();
64
        $this->assertSame([], $m->getTo());
65
    }
66
67
    public function testCC(): void
68
    {
69
        $m = new Message('test', '[email protected]');
70
71
        $this->assertSame([], $m->getCC());
72
        $m->setCC('[email protected]');
73
        $this->assertSame(['[email protected]'], $m->getCC());
74
75
        $m->setCC('[email protected]', '[email protected]');
76
        $this->assertSame(['[email protected]', '[email protected]'], $m->getCC());
77
78
        $m->setCC();
79
        $this->assertSame([], $m->getCC());
80
    }
81
82
    public function testBCC(): void
83
    {
84
        $m = new Message('test', '[email protected]');
85
86
        $this->assertSame([], $m->getBCC());
87
        $m->setBCC('[email protected]');
88
        $this->assertSame(['[email protected]'], $m->getBCC());
89
90
        $m->setBCC('[email protected]', '[email protected]');
91
        $this->assertSame(['[email protected]', '[email protected]'], $m->getBCC());
92
93
        $m->setBCC();
94
        $this->assertSame([], $m->getBCC());
95
    }
96
97
    public function testFrom(): void
98
    {
99
        $m = new Message('test', '[email protected]');
100
101
        $this->assertNull($m->getFrom());
102
103
        $m->setFrom('[email protected]');
104
        $this->assertSame('[email protected]', $m->getFrom());
105
106
        $m->setFrom(null);
107
        $this->assertNull($m->getFrom());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $m->getFrom() targeting Spiral\Mailer\Message::getFrom() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
108
    }
109
110
    public function testReplyTo(): void
111
    {
112
        $m = new Message('test', '[email protected]');
113
114
        $this->assertNull($m->getReplyTo());
115
116
        $m->setReplyTo('[email protected]');
117
        $this->assertSame('[email protected]', $m->getReplyTo());
118
119
        $m->setReplyTo(null);
120
        $this->assertNull($m->getReplyTo());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $m->getReplyTo() targeting Spiral\Mailer\Message::getReplyTo() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121
    }
122
123
    public function testSetDelayInSeconds()
124
    {
125
        $m = new Message('test', '[email protected]');
126
        $m->setDelay(100);
127
128
        $this->assertSame([
129
            'delay' => 100
130
        ], $m->getOptions());
131
    }
132
133
    public function testSetDelayInDateInterval()
134
    {
135
        $m = new Message('test', '[email protected]');
136
        $m->setDelay(new \DateInterval('PT56S'));
137
138
        $this->assertSame([
139
            'delay' => 56
140
        ], $m->getOptions());
141
    }
142
143
    public function testSetDelayInDateTime()
144
    {
145
        $m = new Message('test', '[email protected]');
146
        $m->setDelay(new \DateTimeImmutable('+ 123 second'));
147
148
        $this->assertSame([
149
            'delay' => 123
150
        ], $m->getOptions());
151
    }
152
153
    public function testSetDelayInDateTimeWithPastTime()
154
    {
155
        $m = new Message('test', '[email protected]');
156
        $m->setDelay(new \DateTimeImmutable('- 123 second'));
157
158
        $this->assertSame([
159
            'delay' => 0
160
        ], $m->getOptions());
161
    }
162
}
163