Passed
Push — master ( d7fd0c...0cd2cd )
by Sam
04:33
created

AbstractResolverTest::testAllowsRecursion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of PHP DNS Server.
5
 *
6
 * (c) Yif Swery <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace yswery\DNS\Tests\Resolver;
13
14
use PHPUnit\Framework\TestCase;
15
use yswery\DNS\ClassEnum;
16
use yswery\DNS\RecordTypeEnum;
17
use yswery\DNS\Resolver\JsonResolver;
18
use yswery\DNS\Resolver\ResolverInterface;
19
use yswery\DNS\ResourceRecord;
20
21
abstract class AbstractResolverTest extends TestCase
22
{
23
    /**
24
     * @var ResolverInterface
25
     */
26
    protected $resolver;
27
28
    public function testGetAnswer()
29
    {
30
        $soa = (new ResourceRecord())
31
            ->setName('example.com.')
32
            ->setClass(ClassEnum::INTERNET)
33
            ->setTtl(10800)
34
            ->setType(RecordTypeEnum::TYPE_SOA)
35
            ->setRdata([
36
                'mname' => 'example.com.',
37
                'rname' => 'postmaster.example.com.',
38
                'serial' => 2,
39
                'refresh' => 3600,
40
                'retry' => 7200,
41
                'expire' => 10800,
42
                'minimum' => 3600,
43
            ]);
44
45
        $aaaa = (new ResourceRecord())
46
            ->setName('example.com.')
47
            ->setClass(ClassEnum::INTERNET)
48
            ->setTtl(7200)
49
            ->setType(RecordTypeEnum::TYPE_AAAA)
50
            ->setRdata('2001:acad:ad::32');
51
52
        $soa_query = (new ResourceRecord())
53
            ->setName('example.com.')
54
            ->setType(RecordTypeEnum::TYPE_SOA)
55
            ->setClass(ClassEnum::INTERNET)
56
            ->setQuestion(true);
57
58
        $aaaa_query = (new ResourceRecord())
59
            ->setName('example.com.')
60
            ->setType(RecordTypeEnum::TYPE_AAAA)
61
            ->setClass(ClassEnum::INTERNET)
62
            ->setQuestion(true);
63
64
        $query = [$soa_query, $aaaa_query];
65
        $answer = [$soa, $aaaa];
66
67
        $this->assertEquals($answer, $this->resolver->getAnswer($query));
68
    }
69
70
    public function testUnconfiguredRecordDoesNotResolve()
71
    {
72
        $question[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$question was never initialized. Although not strictly required by PHP, it is generally a good practice to add $question = array(); before regardless.
Loading history...
73
            ->setName('testestestes.com.')
74
            ->setType(RecordTypeEnum::TYPE_A)
75
            ->setQuestion(true);
76
77
        $this->assertEmpty($this->resolver->getAnswer($question));
78
    }
79
80
    public function testHostRecordReturnsArray()
81
    {
82
        $question[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$question was never initialized. Although not strictly required by PHP, it is generally a good practice to add $question = array(); before regardless.
Loading history...
83
            ->setName('test2.com.')
84
            ->setType(RecordTypeEnum::TYPE_A)
85
            ->setQuestion(true);
86
87
        $expectation[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$expectation was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expectation = array(); before regardless.
Loading history...
88
            ->setName('test2.com.')
89
            ->setType(RecordTypeEnum::TYPE_A)
90
            ->setTtl(300)
91
            ->setRdata('111.111.111.111');
92
93
        $expectation[] = (new ResourceRecord())
94
            ->setName('test2.com.')
95
            ->setType(RecordTypeEnum::TYPE_A)
96
            ->setTtl(300)
97
            ->setRdata('112.112.112.112');
98
99
        $this->assertEquals($expectation, $this->resolver->getAnswer($question));
100
    }
101
102
    public function testWildcardDomains()
103
    {
104
        $question[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$question was never initialized. Although not strictly required by PHP, it is generally a good practice to add $question = array(); before regardless.
Loading history...
105
            ->setName('badcow.subdomain.example.com.')
106
            ->setType(RecordTypeEnum::TYPE_A)
107
            ->setQuestion(true);
108
109
        $expectation[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$expectation was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expectation = array(); before regardless.
Loading history...
110
            ->setName('badcow.subdomain.example.com.')
111
            ->setType(RecordTypeEnum::TYPE_A)
112
            ->setTtl(7200)
113
            ->setRdata('192.168.1.42');
114
115
        $this->assertEquals($expectation, $this->resolver->getAnswer($question));
116
    }
117
118
    /**
119
     * @throws \yswery\DNS\UnsupportedTypeException
120
     */
121
    public function testIsWildcardDomain()
122
    {
123
        $resolver = new JsonResolver([]);
124
        $this->assertTrue($resolver->isWildcardDomain('*.cat.com.'));
125
        $this->assertFalse($resolver->isWildcardDomain('github.com.'));
126
    }
127
128
    public function testAllowsRecursion()
129
    {
130
        $this->assertFalse($this->resolver->allowsRecursion());
131
    }
132
133
    public function testIsAuthority()
134
    {
135
        $this->assertTrue($this->resolver->isAuthority('example.com.'));
136
    }
137
138
    public function testSrvRdata()
139
    {
140
        $question[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$question was never initialized. Although not strictly required by PHP, it is generally a good practice to add $question = array(); before regardless.
Loading history...
141
            ->setName('_ldap._tcp.example.com.')
142
            ->setType(RecordTypeEnum::TYPE_SRV)
143
            ->setQuestion(true);
144
145
        $expectation[] = (new ResourceRecord())
0 ignored issues
show
Comprehensibility Best Practice introduced by
$expectation was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expectation = array(); before regardless.
Loading history...
146
            ->setName('_ldap._tcp.example.com.')
147
            ->setType(RecordTypeEnum::TYPE_SRV)
148
            ->setTtl(7200)
149
            ->setRdata([
150
                'priority' => 1,
151
                'weight' => 5,
152
                'port' => 389,
153
                'target' => 'ldap.example.com.',
154
            ]);
155
156
        $this->assertEquals($expectation, $this->resolver->getAnswer($question));
157
    }
158
}
159