Debian Codegraph @

tests/classes/Domains/DomainTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\DateTime;
6 use Liuch\DmarcSrg\Domains\Domain;
7 use Liuch\DmarcSrg\Exception\SoftException;
8 use Liuch\DmarcSrg\Exception\LogicException;
9 use Liuch\DmarcSrg\Database\DatabaseController;
10 use Liuch\DmarcSrg\Database\DomainMapperInterface;
11
12 class DomainTest extends \PHPUnit\Framework\TestCase
13 {
14 public function testConstructorWrongArgumentType(): void
15 {
16 $this->expectException(LogicException::class);
17 new Domain(true, $this->getDbMapperNever());
18 }
19
20 public function testConstructorEmptyFqdn(): void
21 {
22 $this->expectException(SoftException::class);
23 new Domain('', $this->getDbMapperNever());
24 }
25
26 public function testConstructorFqdnIsDot(): void
27 {
28 $this->expectException(SoftException::class);
29 new Domain('.', $this->getDbMapperNever());
30 }
31
32 public function testConstructorFqdnEndWithDot(): void
33 {
34 $this->assertSame('example.org', (new Domain('example.org.', $this->getDbMapperNever()))->fqdn());
35 $this->assertSame('example.org', (new Domain([ 'fqdn' => 'example.org.' ], $this->getDbMapperNever()))->fqdn());
36 }
37
38 public function testConstructorIncorrectArrayData(): void
39 {
40 $data = [
41 'active' => true,
42 'description' => 'Descr',
43 'created_time' => new DateTime(),
44 'updated_time' => new DateTime()
45 ];
46 $this->expectException(LogicException::class);
47 new Domain($data, $this->getDbMapperNever());
48 }
49
50 public function testExists(): void
51 {
52 $this->assertSame(false, (new Domain(1, $this->getDbMapperOnce('exists', '', false)))->exists());
53 $this->assertSame(true, (new Domain(1, $this->getDbMapperOnce('exists', '', true)))->exists());
54 }
55
56 public function testEnsureExist(): void
57 {
58 (new Domain(1, $this->getDbMapperOnce('exists', '', true)))->ensure('exist');
59 $this->expectException(SoftException::class);
60 (new Domain(1, $this->getDbMapperOnce('exists', '', false)))->ensure('exist');
61 }
62
63 public function testEnsureNonexist(): void
64 {
65 (new Domain(1, $this->getDbMapperOnce('exists', '', false)))->ensure('nonexist');
66 $this->expectException(SoftException::class);
67 (new Domain(1, $this->getDbMapperOnce('exists', '', true)))->ensure('nonexist');
68 }
69
70 public function testId(): void
71 {
72 $this->assertSame(1, (new Domain(1, $this->getDbMapperNever()))->id());
73 $this->assertSame(1, (new Domain([ 'id' => 1 ], $this->getDbMapperNever()))->id());
74 $this->assertSame(1, (new Domain([ 'fqdn' => 'example.org' ], $this->getDbMapperOnce('fetch', 'id', 1)))->id());
75 }
76
77 public function testFqdn(): void
78 {
79 $this->assertSame(
80 'example.org',
81 (new Domain('example.org', $this->getDbMapperNever()))->fqdn()
82 );
83 $this->assertSame(
84 'example.org',
85 (new Domain([ 'fqdn' => 'example.org' ], $this->getDbMapperNever()))->fqdn()
86 );
87 $this->assertSame(
88 'example.org',
89 (new Domain(1, $this->getDbMapperOnce('fetch', 'fqdn', 'example.org')))->fqdn()
90 );
91 }
92
93 public function testActive(): void
94 {
95 $this->assertSame(true, (new Domain([ 'id' => 1, 'active' => true ], $this->getDbMapperNever()))->active());
96 $this->assertSame(true, (new Domain(1, $this->getDbMapperOnce('fetch', 'active', true)))->active());
97 }
98
99 public function testDescription(): void
100 {
101 $this->assertSame(
102 'Descr',
103 (new Domain(
104 [ 'id'=> 1, 'fqdn' => 'example.org', 'description' => 'Descr' ],
105 $this->getDbMapperNever()
106 ))->description()
107 );
108 $this->assertSame(
109 'Descr',
110 (new Domain(1, $this->getDbMapperOnce('fetch', 'description', 'Descr')))->description()
111 );
112 }
113
114 public function testToArray(): void
115 {
116 $data_in = [
117 'id' => 1,
118 'fqdn' => 'example.org',
119 'active' => true,
120 'description' => 'Descr',
121 'created_time' => new DateTime(),
122 'updated_time' => new DateTime()
123 ];
124 $data_out = $data_in;
125 unset($data_out['id']);
126 $this->assertSame($data_out, (new Domain($data_in, $this->getDbMapperNever()))->toArray());
127 (new Domain([ 'id' => 1 ], $this->getDbMapperOnce('fetch', '', null)))->toArray();
128 (new Domain([ 'fqdn' => 'example.org' ], $this->getDbMapperOnce('fetch', '', null)))->toArray();
129 }
130
131 public function testSave(): void
132 {
133 (new Domain(1, $this->getDbMapperOnce('save', '', null)))->save();
134 }
135
136 public function testDelete(): void
137 {
138 (new Domain(1, $this->getDbMapperOnce('delete', '', null)))->delete(false);
139 }
140
141 private function getDbMapperNever(): object
142 {
143 $db = $this->getMockBuilder(DatabaseController::class)
144 ->disableOriginalConstructor()
145 ->onlyMethods([ 'getMapper' ])
146 ->getMock();
147 $db->expects($this->never())
148 ->method('getMapper');
149 return $db;
150 }
151
152 private function getDbMapperOnce(string $method, string $key, $value): object
153 {
154 $mapper = $this->getMockBuilder(DomainMapperInterface::class)
155 ->disableOriginalConstructor()
156 ->getMock();
157 $mr = $mapper->expects($this->once())->method($method);
158 if (empty($key)) {
159 if (!is_null($value)) {
160 $mr->willReturn($value);
161 }
162 } else {
163 $mr->willReturnCallback(function (&$data) use ($key, $value) {
164 $data[$key] = $value;
165 });
166 }
167
168 $db = $this->getMockBuilder(DatabaseController::class)
169 ->disableOriginalConstructor()
170 ->onlyMethods([ 'getMapper' ])
171 ->getMock();
172 $db->method('getMapper')
173 ->with('domain')
174 ->willReturn($mapper);
175 return $db;
176 }
177 }