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\Factory; |
17
|
|
|
use Badcow\DNS\Rdata\KX; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
class KxTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testSetters(): void |
23
|
|
|
{ |
24
|
|
|
$target = 'foo.example.com.'; |
25
|
|
|
$preference = 10; |
26
|
|
|
$kx = new KX(); |
27
|
|
|
$kx->setExchanger($target); |
28
|
|
|
$kx->setPreference($preference); |
29
|
|
|
|
30
|
|
|
$this->assertEquals($target, $kx->getExchanger()); |
31
|
|
|
$this->assertEquals($preference, $kx->getPreference()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testOutput(): void |
35
|
|
|
{ |
36
|
|
|
$target = 'foo.example.com.'; |
37
|
|
|
$kx = new KX(); |
38
|
|
|
$kx->SetExchanger($target); |
39
|
|
|
$kx->setPreference(42); |
40
|
|
|
|
41
|
|
|
$this->assertEquals('42 foo.example.com.', $kx->toText()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testOutputThrowsExceptionWhenMissingPreference(): void |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$kx = new KX(); |
47
|
|
|
$kx->setExchanger('mail.google.com.'); |
48
|
|
|
|
49
|
|
|
$this->expectException(\InvalidArgumentException::class); |
50
|
|
|
$this->expectExceptionMessage('No preference has been set on KX object.'); |
51
|
|
|
$kx->toText(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testOutputThrowsExceptionWhenMissingExchanger(): void |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$kx = new KX(); |
57
|
|
|
$kx->setPreference(15); |
58
|
|
|
|
59
|
|
|
$this->expectException(\InvalidArgumentException::class); |
60
|
|
|
$this->expectExceptionMessage('No exchanger has been set on KX object.'); |
61
|
|
|
$kx->toText(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testFactory(): void |
65
|
|
|
{ |
66
|
|
|
$kx = Factory::KX(15, 'mx.example.com.'); |
67
|
|
|
$this->assertInstanceOf(KX::class, $kx); |
68
|
|
|
$this->assertEquals('15 mx.example.com.', $kx->toText()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function testFromText(): void |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$text = '10 mail.example.com.'; |
74
|
|
|
/** @var KX $kx */ |
75
|
|
|
$kx = new KX(); |
76
|
|
|
$kx->fromText($text); |
77
|
|
|
|
78
|
|
|
$this->assertEquals(10, $kx->getPreference()); |
79
|
|
|
$this->assertEquals('mail.example.com.', $kx->getExchanger()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testWire(): void |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$kx = new KX(); |
85
|
|
|
$kx->setExchanger('mail.example.com.'); |
86
|
|
|
$kx->setPreference(10); |
87
|
|
|
|
88
|
|
|
$expectation = pack('n', 10).chr(4).'mail'.chr(7).'example'.chr(3).'com'.chr(0); |
89
|
|
|
|
90
|
|
|
$this->assertEquals($expectation, $kx->toWire()); |
91
|
|
|
$fromWire = new KX(); |
92
|
|
|
$fromWire->fromWire($expectation); |
93
|
|
|
$this->assertEquals($kx, $fromWire); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.