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; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Question; |
17
|
|
|
use Badcow\DNS\Rdata\UnsupportedTypeException; |
18
|
|
|
use PHPStan\Testing\TestCase; |
19
|
|
|
|
20
|
|
|
class QuestionTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @throws UnsupportedTypeException |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function testToWire(): void |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$q = new Question(); |
28
|
|
|
$q->setName('example.com.'); |
29
|
|
|
$q->setType('NS'); |
30
|
|
|
$q->setClass('IN'); |
31
|
|
|
|
32
|
|
|
$expectation = chr(7).'example'.chr(3).'com'.chr(0).pack('nn', 2, 1); |
33
|
|
|
|
34
|
|
|
$this->assertEquals($expectation, $q->toWire()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSetClassThrowsException(): void |
38
|
|
|
{ |
39
|
|
|
$this->expectException(\InvalidArgumentException::class); |
40
|
|
|
$this->expectExceptionMessage('Invalid class: "65536".'); |
41
|
|
|
$q = new Question(); |
42
|
|
|
$q->setClassId(65536); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testSetNameThrowsException(): void |
46
|
|
|
{ |
47
|
|
|
$this->expectException(\InvalidArgumentException::class); |
48
|
|
|
$this->expectExceptionMessage('"abc123.com" is not a fully qualified domain name.'); |
49
|
|
|
$q = new Question(); |
50
|
|
|
$q->setName('abc123.com'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testFromWire(): void |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$wireFormat = chr(7).'example'.chr(3).'com'.chr(0).pack('nn', 2, 1); |
56
|
|
|
|
57
|
|
|
$q = Question::fromWire($wireFormat); |
58
|
|
|
|
59
|
|
|
$this->assertEquals('example.com.', $q->getName()); |
60
|
|
|
$this->assertEquals('NS', $q->getType()); |
61
|
|
|
$this->assertEquals('IN', $q->getClass()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testSetTypeCodeThrowsException(): void |
65
|
|
|
{ |
66
|
|
|
$this->expectException(\DomainException::class); |
67
|
|
|
$this->expectExceptionMessage('TypeCode must be an unsigned 16-bit integer. "65536" given.'); |
68
|
|
|
$q = new Question(); |
69
|
|
|
$q->setTypeCode(65536); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.