Completed
Push — master ( 6fa888...1dfddd )
by Patrick
02:15
created

DsnParserTest::loadDsn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Artack\Dsn\Parser;
6
7
use Artack\Dsn\Exception\InvalidArgumentException;
8
use Artack\Dsn\Field\ActionField;
9
use Artack\Dsn\Field\AddressTypeField;
10
use Artack\Dsn\Field\DateField;
11
use Artack\Dsn\Field\DiagnosticTypeField;
12
use Artack\Dsn\Field\MtaNameTypeField;
13
use Artack\Dsn\Field\StatusField;
14
use PHPUnit\Framework\TestCase;
15
16
class DsnParserTest extends TestCase
17
{
18
    public function testDsn001(): void
19
    {
20
        $deliveryStatus = (new DsnParser())->parse($this->loadDsn('dsn001.txt'));
21
22
        $this->assertCount(1, $deliveryStatus->getRecipientFields());
23
        $this->assertSame(1, $deliveryStatus->countRecipientFields());
24
25
        $messageFields = $deliveryStatus->getMessageFields();
26
        $this->assertCount(4, $messageFields->getAll());
27
28
        /** @var MtaNameTypeField $reportingMtaField */
29
        $reportingMtaField = $messageFields->get('Reporting-MTA');
30
        $this->assertInstanceOf(MtaNameTypeField::class, $reportingMtaField);
31
        $this->assertSame('dns', $reportingMtaField->getType());
32
        $this->assertSame('googlemail.com', $reportingMtaField->getValue());
33
34
        /** @var MtaNameTypeField $receivedFromMtaField */
35
        $receivedFromMtaField = $messageFields->get('Received-From-MTA');
36
        $this->assertInstanceOf(MtaNameTypeField::class, $receivedFromMtaField);
37
        $this->assertSame('dns', $receivedFromMtaField->getType());
38
        $this->assertSame('[email protected]', $receivedFromMtaField->getValue());
39
40
        /** @var DateField $arrivalDateField */
41
        $arrivalDateField = $messageFields->get('Arrival-Date');
42
        $arrivalDateTime = new \DateTimeImmutable('2019-03-24 12:52:25 PDT');
43
        $this->assertInstanceOf(DateField::class, $arrivalDateField);
44
        $this->assertEquals($arrivalDateTime, $arrivalDateField->getDateTime());
45
46
        $recipientFields = $deliveryStatus->getRecipientFields()[0];
47
48
        /** @var AddressTypeField $finalRecipientField */
49
        $finalRecipientField = $recipientFields->get('Final-Recipient');
50
        $this->assertInstanceOf(AddressTypeField::class, $finalRecipientField);
51
        $this->assertSame('rfc822', $finalRecipientField->getType());
52
        $this->assertSame('[email protected]', $finalRecipientField->getValue());
53
54
        /** @var ActionField $actionField */
55
        $actionField = $recipientFields->get('Action');
56
        $this->assertInstanceOf(ActionField::class, $actionField);
57
        $this->assertSame('failed', $actionField->getAction());
58
59
        /** @var StatusField $statusField */
60
        $statusField = $recipientFields->get('Status');
61
        $this->assertInstanceOf(StatusField::class, $statusField);
62
        $this->assertSame(5, $statusField->getCodeClass());
63
        $this->assertSame(1, $statusField->getCodeSubject());
64
        $this->assertSame(1, $statusField->getCodeEnumerated());
65
        $this->assertSame('5.1.1', $statusField->getCode());
66
67
        /** @var MtaNameTypeField $remoteMtaField */
68
        $remoteMtaField = $recipientFields->get('Remote-MTA');
69
        $this->assertInstanceOf(MtaNameTypeField::class, $remoteMtaField);
70
        $this->assertSame('dns', $remoteMtaField->getType());
71
        $this->assertSame('mxbw.lb.bluewin.ch.', $remoteMtaField->getValue());
72
        $this->assertSame('195.186.227.50, the server for the domain bluewin.ch.', $remoteMtaField->getComment());
73
74
        /** @var DiagnosticTypeField $diagnosticCodeField */
75
        $diagnosticCodeField = $recipientFields->get('Diagnostic-Code');
76
        $this->assertInstanceOf(DiagnosticTypeField::class, $diagnosticCodeField);
77
        $this->assertSame('smtp', $diagnosticCodeField->getType());
78
        $this->assertSame('550 5.1.1 <[email protected]> recipient rejected, address unknown', $diagnosticCodeField->getValue());
79
80
        /** @var DateField $lastAttemptDateField */
81
        $lastAttemptDateField = $recipientFields->get('Last-Attempt-Date');
82
        $lastAttemptDateTime = new \DateTimeImmutable('2019-03-24 12:52:26 PDT');
83
        $this->assertInstanceOf(DateField::class, $lastAttemptDateField);
84
        $this->assertEquals($lastAttemptDateTime, $lastAttemptDateField->getDateTime());
85
    }
86
87
    public function testDsn002(): void
88
    {
89
        $deliveryStatus = (new DsnParser())->parse($this->loadDsn('dsn002.txt'));
90
91
        $this->assertCount(1, $deliveryStatus->getMessageFields()->getAll());
92
        $this->assertCount(1, $deliveryStatus->getRecipientFields());
93
        $this->assertSame(1, $deliveryStatus->countRecipientFields());
94
        $this->assertCount(6, $deliveryStatus->getRecipientFields()[0]->getAll());
95
    }
96
97
    public function testDsn003(): void
98
    {
99
        $deliveryStatus = (new DsnParser())->parse($this->loadDsn('dsn003.txt'));
100
101
        $this->assertCount(1, $deliveryStatus->getMessageFields()->getAll());
102
        $this->assertCount(3, $deliveryStatus->getRecipientFields());
103
        $this->assertSame(3, $deliveryStatus->countRecipientFields());
104
        $this->assertCount(6, $deliveryStatus->getRecipientFields()[0]->getAll());
105
        $this->assertCount(4, $deliveryStatus->getRecipientFields()[1]->getAll());
106
        $this->assertCount(6, $deliveryStatus->getRecipientFields()[2]->getAll());
107
    }
108
109
    public function testDsn004(): void
110
    {
111
        $this->expectException(InvalidArgumentException::class);
112
113
        $deliveryStatus = (new DsnParser())->parse($this->loadDsn('dsn004.txt'));
114
115
        $this->assertCount(1, $deliveryStatus->getMessageFields()->getAll());
116
    }
117
118
    public function testDsn005(): void
119
    {
120
        $deliveryStatus = (new DsnParser())->parse($this->loadDsn('dsn005.txt'));
121
122
        $this->assertCount(1, $deliveryStatus->getMessageFields()->getAll());
123
        $this->assertCount(1, $deliveryStatus->getRecipientFields());
124
        $this->assertSame(1, $deliveryStatus->countRecipientFields());
125
        $this->assertCount(3, $deliveryStatus->getRecipientFields()[0]->getAll());
126
    }
127
128
    private function loadDsn(string $filename): string
129
    {
130
        $filepath = __DIR__.'/../Resources/dsn/'.$filename;
131
        $file = new \SplFileInfo($filepath);
132
133
        if (!$file->isReadable()) {
134
            throw new \RuntimeException(sprintf('cant read file "%s"', $filepath));
135
        }
136
137
        return file_get_contents($file->getPathname());
138
    }
139
}
140