1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Tests\Rdata; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Rdata\CAA; |
17
|
|
|
use Badcow\DNS\Rdata\Factory; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
class CaaTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testOutput(): void |
23
|
|
|
{ |
24
|
|
|
$caa = Factory::CAA(0, 'issue', 'letsencrypt.org'); |
25
|
|
|
|
26
|
|
|
$expectation = '0 issue "letsencrypt.org"'; |
27
|
|
|
|
28
|
|
|
$this->assertEquals($expectation, $caa->toText()); |
29
|
|
|
$this->assertEquals(0, $caa->getFlag()); |
30
|
|
|
$this->assertEquals('issue', $caa->getTag()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function testFlagException(): void |
37
|
|
|
{ |
38
|
|
|
$this->expectException(\InvalidArgumentException::class); |
39
|
|
|
$this->expectExceptionMessage('Flag must be an unsigned 8-bit integer.'); |
40
|
|
|
|
41
|
|
|
$srv = new CAA(); |
42
|
|
|
$srv->setFlag(256); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
|
public function testTagException(): void |
49
|
|
|
{ |
50
|
|
|
$this->expectException(\InvalidArgumentException::class); |
51
|
|
|
$this->expectExceptionMessage('Tag can be one of this type "issue", "issuewild", or "iodef".'); |
52
|
|
|
|
53
|
|
|
$srv = new CAA(); |
54
|
|
|
$srv->setTag('not_exist'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetType(): void |
58
|
|
|
{ |
59
|
|
|
$this->assertEquals('CAA', (new CAA())->getType()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testFromText(): void |
63
|
|
|
{ |
64
|
|
|
$text = '0 iodef "mailto:[email protected]"'; |
65
|
|
|
/** @var CAA $caa */ |
66
|
|
|
$caa = new CAA(); |
67
|
|
|
$caa->fromText($text); |
68
|
|
|
|
69
|
|
|
$this->assertEquals(0, $caa->getFlag()); |
70
|
|
|
$this->assertEquals(CAA::TAG_IODEF, $caa->getTag()); |
71
|
|
|
$this->assertEquals('mailto:[email protected]', $caa->getValue()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testWire(): void |
75
|
|
|
{ |
76
|
|
|
$expectation = chr(0).chr(5).'iodef'.'mailto:[email protected]'; |
77
|
|
|
$caa = new CAA(); |
78
|
|
|
$caa->setFlag(0); |
79
|
|
|
$caa->setTag(CAA::TAG_IODEF); |
80
|
|
|
$caa->setValue('mailto:[email protected]'); |
81
|
|
|
|
82
|
|
|
$fromWire = new CAA(); |
83
|
|
|
$fromWire->fromWire($expectation); |
84
|
|
|
|
85
|
|
|
$this->assertEquals($expectation, $caa->toWire()); |
86
|
|
|
$this->assertEquals($caa, $fromWire); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testToWireThrowsExceptionIfNotAllParametersAreSet(): void |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$caa = new CAA(); |
92
|
|
|
$caa->setTag(CAA::TAG_IODEF); |
93
|
|
|
$caa->setValue('mailto:[email protected]'); |
94
|
|
|
|
95
|
|
|
$this->expectException(\InvalidArgumentException::class); |
96
|
|
|
$this->expectExceptionMessage('All CAA parameters must be set.'); |
97
|
|
|
$caa->toWire(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testToTextThrowsExceptionIfNotAllParametersAreSet(): void |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$caa = new CAA(); |
103
|
|
|
$caa->setFlag(0); |
104
|
|
|
$caa->setTag(CAA::TAG_IODEF); |
105
|
|
|
|
106
|
|
|
$this->expectException(\InvalidArgumentException::class); |
107
|
|
|
$this->expectExceptionMessage('All CAA parameters must be set.'); |
108
|
|
|
$caa->toText(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.