-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathDnsTest.php
221 lines (175 loc) · 5.81 KB
/
DnsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Spatie\Dns\Test;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Spatie\Dns\Dns;
use Spatie\Dns\Exceptions\CouldNotFetchDns;
use Spatie\Dns\Exceptions\InvalidArgument;
use Spatie\Dns\Records\A;
use Spatie\Dns\Records\MX;
use Spatie\Dns\Records\NS;
use Spatie\Dns\Records\Record;
use Spatie\Dns\Records\SOA;
use Spatie\Dns\Support\Collection;
use Spatie\Dns\Test\TestClasses\CustomHandler;
class DnsTest extends TestCase
{
protected Dns $dns;
protected function setUp(): void
{
parent::setUp();
ray()->newScreen($this->getName());
$this->dns = new Dns();
}
/** @test */
public function it_throws_an_exception_if_an_empty_string_is_passed()
{
$this->expectException(InvalidArgument::class);
$this->dns->getRecords('');
}
/** @test */
public function it_fetches_all_records_by_default()
{
$records = $this->dns->getRecords('spatie.be');
$this->assertSeeRecordTypes(
$records,
[A::class, NS::class, SOA::class, MX::class]
);
}
/** @test */
public function it_has_a_static_constructor()
{
$records = DNS::query()->getRecords('spatie.be');
$this->assertSeeRecordTypes(
$records,
[A::class, NS::class, SOA::class, MX::class]
);
}
/** @test */
public function it_fetches_all_records_with_asterisk()
{
$records = $this->dns->getRecords('spatie.be', '*');
$this->assertSeeRecordTypes(
$records,
[A::class, NS::class, SOA::class, MX::class]
);
}
/** @test */
public function it_fetches_records_for_a_single_type_via_flag()
{
$records = $this->dns->getRecords('spatie.be', DNS_NS);
$this->assertOnlySeeRecordTypes($records, [NS::class]);
}
/** @test */
public function it_fetches_records_for_a_single_type_via_name()
{
$records = $this->dns->getRecords('spatie.be', 'NS');
$this->assertOnlySeeRecordTypes($records, [NS::class]);
}
/** @test */
public function it_fetches_records_for_multiple_types_via_flags()
{
$records = $this->dns->getRecords('spatie.be', DNS_NS | DNS_SOA);
$this->assertOnlySeeRecordTypes($records, [NS::class, SOA::class]);
}
/** @test */
public function it_fetches_records_for_multiple_types_via_names()
{
$records = $this->dns->getRecords('spatie.be', ['NS', 'SOA']);
$this->assertOnlySeeRecordTypes($records, [NS::class, SOA::class]);
}
/** @test */
public function it_fetches_records_via_name_and_ignores_casing()
{
$records = $this->dns->getRecords('spatie.be', 'ns');
$this->assertOnlySeeRecordTypes($records, [NS::class]);
}
/** @test */
public function it_fetches_records_for_given_type_and_ignores_record_chain()
{
$records = $this->dns->getRecords('www.opendor.me', DNS_A);
$this->assertOnlySeeRecordTypes($records, [A::class]);
}
/** @test */
public function it_throws_an_exception_if_an_invalid_record_type_is_passed()
{
$this->expectException(InvalidArgument::class);
$this->dns->getRecords('spatie.be', 'xyz');
}
/** @test */
public function it_uses_provided_nameserver_if_set()
{
$this->dns->useNameserver('ns1.openminds.be');
$this->assertEquals('ns1.openminds.be', $this->dns->getNameserver());
}
/** @test */
public function it_uses_default_nameserver_if_not_set()
{
$this->assertNull($this->dns->getNameserver());
}
/** @test */
public function it_throws_exception_on_failed_to_fetch_dns_record()
{
$this->expectException(CouldNotFetchDns::class);
$this->dns
->useNameserver('dns.spatie.be')
->getRecords('spatie.be', DNS_A);
}
/** @test */
public function it_can_use_custom_handlers()
{
$result = $this->dns
->useHandlers([new CustomHandler()])
->getRecords('spatie.be');
$handlers = [
'custom-handler-results-A',
'custom-handler-results-AAAA',
'custom-handler-results-CNAME',
'custom-handler-results-NS',
'custom-handler-results-SOA',
'custom-handler-results-MX',
'custom-handler-results-SRV',
'custom-handler-results-TXT',
];
if (defined('DNS_CAA')) {
$handlers[] = 'custom-handler-results-CAA';
}
$this->assertEquals($handlers, $result);
}
protected function assertSeeRecordTypes(array $records, array $types)
{
foreach ($types as $type) {
$foundRecords = array_filter(
$records,
fn (Record $record): bool => is_a($record, $type)
);
$this->assertNotEmpty($foundRecords);
}
}
protected function assertDontSeeRecordTypes(Collection $records, array $types)
{
foreach ($types as $type) {
$foundRecords = array_filter(
$records->all(),
fn (Record $record): bool => is_a($record, $type)
);
$this->assertEmpty($foundRecords);
}
}
protected function assertOnlySeeRecordTypes(array $records, array $types)
{
$expectedCount = count($records);
$foundRecords = Collection::make($records)
->filter(fn (Record $record) => $this->recordIsOfType($record, $types));
$this->assertCount($expectedCount, $foundRecords);
}
protected function recordIsOfType(Record $record, array $types): bool
{
foreach ($types as $type) {
if (is_a($record, $type) && $record->type() === (new ReflectionClass($type))->getShortName()) {
return true;
}
}
return false;
}
}