Debian Codegraph @

tests/classes/ReportLog/ReportLogItemTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\Report\Report;
6 use Liuch\DmarcSrg\Sources\Source;
7 use Liuch\DmarcSrg\Exception\SoftException;
8 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
9 use Liuch\DmarcSrg\ReportLog\ReportLogItem;
10
11 class ReportLogItemTest extends \PHPUnit\Framework\TestCase
12 {
13 public function testForSuccess(): void
14 {
15 $db = $this->getDbMapperNever();
16 $rli = ReportLogItem::success(
17 Source::SOURCE_MAILBOX,
18 new Report([ 'domain' => 'example.org', 'report_id' => 'rrggoo' ], $db),
19 'filename.gz',
20 'Success!',
21 $db
22 );
23 $this->assertTrue($rli->toArray()['success']);
24 }
25
26 public function testForFailed(): void
27 {
28 $rli = ReportLogItem::failed(
29 Source::SOURCE_UPLOADED_FILE,
30 null,
31 null,
32 'Failed!',
33 $this->getDbMapperNever()
34 );
35 $this->assertFalse($rli->toArray()['success']);
36 }
37
38 public function testGettingById(): void
39 {
40 $callback = function (&$data) {
41 $data['source'] = Source::SOURCE_MAILBOX;
42 $this->assertSame(55, $data['id']);
43 };
44 ReportLogItem::byId(55, $this->getDbMapperOnce('fetch', $callback));
45 }
46
47 public function testGettingByIdNotFound(): void
48 {
49 $callback = function (&$data) {
50 throw new DatabaseNotFoundException();
51 };
52 $this->expectException(SoftException::class);
53 ReportLogItem::byId(55, $this->getDbMapperOnce('fetch', $callback));
54 }
55
56 public function testSourceToString(): void
57 {
58 $this->assertSame('uploaded_file', ReportLogItem::sourceToString(Source::SOURCE_UPLOADED_FILE));
59 $this->assertSame('email', ReportLogItem::sourceToString(Source::SOURCE_MAILBOX));
60 $this->assertSame('directory', ReportLogItem::sourceToString(Source::SOURCE_DIRECTORY));
61 $this->assertSame('', ReportLogItem::sourceToString(-111));
62 }
63
64 public function testToArray(): void
65 {
66 $sdata = [
67 'id' => 66,
68 'domain' => 'example.org',
69 'report_id' => 'gg44dd',
70 'event_time' => new \DateTime(),
71 'filename' => 'filename.zip',
72 'source' => Source::SOURCE_DIRECTORY,
73 'success' => true,
74 'message' => 'Message!'
75 ];
76 $callback = function (&$data) use ($sdata) {
77 foreach ($sdata as $key => $value) {
78 $data[$key] = $value;
79 }
80 };
81 $sdata['source'] = ReportLogItem::sourceToString($sdata['source']);
82 $this->assertSame($sdata, ReportLogItem::byId(66, $this->getDbMapperOnce('fetch', $callback))->toArray());
83 }
84
85 public function testSaving(): void
86 {
87 $callback1 = function ($data) {
88 $this->assertSame(
89 [
90 'id' => null,
91 'domain' => 'example.org',
92 'report_id' => 'xxvvbb',
93 'event_time' => null,
94 'filename' => 'filename.xml',
95 'source' => Source::SOURCE_MAILBOX,
96 'success' => true,
97 'message' => 'Success!'
98 ],
99 $data
100 );
101 };
102 $callback2 = function ($data) {
103 $this->assertSame(
104 [
105 'id' => null,
106 'domain' => null,
107 'report_id' => null,
108 'event_time' => null,
109 'filename' => null,
110 'source' => Source::SOURCE_UPLOADED_FILE,
111 'success' => false,
112 'message' => 'Failed!'
113 ],
114 $data
115 );
116 };
117
118 $rli = ReportLogItem::success(
119 Source::SOURCE_MAILBOX,
120 new Report([ 'domain' => 'example.org', 'report_id' => 'xxvvbb' ], $this->getDbMapperNever()),
121 'filename.xml',
122 'Success!',
123 $this->getDbMapperOnce('save', $callback1)
124 );
125 $rli->save();
126
127 $rli = ReportLogItem::failed(
128 Source::SOURCE_UPLOADED_FILE,
129 null,
130 null,
131 'Failed!',
132 $this->getDbMapperOnce('save', $callback2)
133 );
134 $rli->save();
135 }
136
137 private function getDbMapperOnce(string $method, $callback): object
138 {
139 $mapper = $this->getMockBuilder(Database\ReportLogMapperInterface::class)
140 ->disableOriginalConstructor()
141 ->getMock();
142 $mapper->expects($this->once())
143 ->method($method)
144 ->willReturnCallback($callback);
145
146 $db = $this->getMockBuilder(Database\DatabaseConnector::class)
147 ->disableOriginalConstructor()
148 ->getMock();
149 $db->method('getMapper')
150 ->with('report-log')
151 ->willReturn($mapper);
152
153 return $db;
154 }
155
156 private function getDbMapperNever(): object
157 {
158 $db = $this->getMockBuilder(Database\DatabaseConnector::class)
159 ->disableOriginalConstructor()
160 ->getMock();
161 $db->expects($this->never())
162 ->method('getMapper');
163 return $db;
164 }
165 }