Skip to content

Commit 3d074fd

Browse files
committed
Add resolver test
1 parent 096ade4 commit 3d074fd

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed

tests/ResolverTest.php

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tests\ArrayAccess\DnsRecord;
5+
6+
use ArrayAccess\DnsRecord\Cache\Adapter\ArrayCacheAdapter;
7+
use ArrayAccess\DnsRecord\Cache\CacheStorage;
8+
use ArrayAccess\DnsRecord\DnsServerStorage;
9+
use ArrayAccess\DnsRecord\Exceptions\Exception;
10+
use ArrayAccess\DnsRecord\Exceptions\RequestException;
11+
use ArrayAccess\DnsRecord\Interfaces\Cache\CacheStorageInterface;
12+
use ArrayAccess\DnsRecord\Interfaces\Packet\PacketRequestDataInterface;
13+
use ArrayAccess\DnsRecord\Interfaces\Packet\PacketResponseInterface;
14+
use ArrayAccess\DnsRecord\Packet\Answers;
15+
use ArrayAccess\DnsRecord\Resolver;
16+
use ArrayAccess\DnsRecord\ResourceRecord\Definitions\Opcode\IQuery;
17+
use ArrayAccess\DnsRecord\ResourceRecord\Definitions\Opcode\Query;
18+
use ArrayAccess\DnsRecord\ResourceRecord\Definitions\QClass\IN;
19+
use ArrayAccess\DnsRecord\ResourceRecord\RRTypes\A;
20+
use PHPUnit\Framework\TestCase;
21+
use Throwable;
22+
use function sprintf;
23+
24+
/**
25+
* Many classes already handled by this method call
26+
* @see CacheStorageTest fot cache test
27+
*/
28+
class ResolverTest extends TestCase
29+
{
30+
public function testSetDnsServerStorage()
31+
{
32+
$resolver = new Resolver();
33+
$dnsServerStorage = $resolver->getDnsServerStorage();
34+
$resolver->setDnsServerStorage(DnsServerStorage::createDefault());
35+
$this->assertNotSame(
36+
$dnsServerStorage,
37+
$resolver->getDnsServerStorage(),
38+
sprintf(
39+
'%1$s->getDnsServerStorage() should not identical with default dns storage',
40+
$resolver::class
41+
)
42+
);
43+
}
44+
45+
public function testGetDnsServerStorage()
46+
{
47+
$resolver = new Resolver();
48+
$dnsServerStorage = $resolver->getDnsServerStorage();
49+
$this->assertInstanceOf(
50+
DnsServerStorage::class,
51+
$dnsServerStorage,
52+
sprintf(
53+
'%1$s->getDnsServerStorage() should instance of %2$s',
54+
$resolver::class,
55+
DnsServerStorage::class
56+
)
57+
);
58+
$resolver->setDnsServerStorage(DnsServerStorage::createDefault());
59+
$this->assertNotSame(
60+
$dnsServerStorage,
61+
$resolver->getDnsServerStorage(),
62+
sprintf(
63+
'%1$s->getDnsServerStorage() should not identical with default dns storage',
64+
$resolver::class
65+
)
66+
);
67+
}
68+
69+
public function testSetCache()
70+
{
71+
$resolver = new Resolver();
72+
$cache = $resolver->getCache();
73+
$resolver->setCache(new CacheStorage());
74+
$this->assertNotSame(
75+
$cache,
76+
$resolver->getCache(),
77+
sprintf(
78+
'%1$s->getCache() should not identical with default cache',
79+
$resolver::class
80+
)
81+
);
82+
}
83+
84+
public function testGetCache()
85+
{
86+
$resolver = new Resolver();
87+
$cache = $resolver->getCache();
88+
$this->assertInstanceOf(
89+
CacheStorageInterface::class,
90+
$cache,
91+
sprintf(
92+
'%1$s->getCache() should instance of %2$s',
93+
$resolver::class,
94+
CacheStorageInterface::class
95+
)
96+
);
97+
$this->assertNull(
98+
$cache->getAdapter(),
99+
sprintf(
100+
'%1$s->getCache()->getAdapter() should null if adapter not set',
101+
$resolver::class
102+
)
103+
);
104+
$arrayAdapter = new ArrayCacheAdapter();
105+
$newCache = new CacheStorage($arrayAdapter);
106+
$resolver->setCache($newCache);
107+
$this->assertNotSame(
108+
$cache,
109+
$resolver->getCache(),
110+
sprintf(
111+
'%1$s->getCache() should not identical with default cache',
112+
$resolver::class
113+
)
114+
);
115+
$this->assertSame(
116+
$arrayAdapter,
117+
$resolver->getCache()->getAdapter(),
118+
sprintf(
119+
'%1$s->getCache()->getAdapter() should identical with set adapter',
120+
$resolver::class
121+
)
122+
);
123+
}
124+
public function testQuery()
125+
{
126+
$resolver = new Resolver();
127+
$query = $resolver->query('example.com');
128+
$this->assertInstanceOf(
129+
PacketRequestDataInterface::class,
130+
$query,
131+
sprintf(
132+
'%1$s->query() should instance of %2$s',
133+
$resolver::class,
134+
PacketRequestDataInterface::class
135+
)
136+
);
137+
$this->assertInstanceOf(
138+
Query::class,
139+
$query->getHeader()->getOpCode(),
140+
sprintf(
141+
'%1$s->query()->getHeader()->getOpCode() should instance of %2$s',
142+
$resolver::class,
143+
Query::class
144+
)
145+
);
146+
}
147+
148+
public function testIQuery()
149+
{
150+
$resolver = new Resolver();
151+
$query = $resolver->iQuery('example.com');
152+
$this->assertInstanceOf(
153+
PacketRequestDataInterface::class,
154+
$query,
155+
sprintf(
156+
'%1$s->iQuery() should instance of %2$s',
157+
$resolver::class,
158+
PacketRequestDataInterface::class
159+
)
160+
);
161+
$this->assertInstanceOf(
162+
IQuery::class,
163+
$query->getHeader()->getOpCode(),
164+
sprintf(
165+
'%1$s->query()->getHeader()->getOpCode() should instance of %2$s',
166+
$resolver::class,
167+
IQuery::class
168+
)
169+
);
170+
}
171+
172+
public function testLookup()
173+
{
174+
$resolver = new Resolver(
175+
DnsServerStorage::createDefault(),
176+
new CacheStorage(new ArrayCacheAdapter())
177+
);
178+
try {
179+
$lookup = $resolver->lookup(
180+
'example.com',
181+
timeout: 1
182+
);
183+
} catch (Throwable $e) {
184+
$this->assertInstanceOf(
185+
RequestException::class,
186+
$e
187+
);
188+
return;
189+
}
190+
191+
$this->assertInstanceOf(
192+
PacketResponseInterface::class,
193+
$lookup,
194+
sprintf(
195+
'%1$s->lookup() should instanceof %2$s',
196+
$resolver::class,
197+
PacketResponseInterface::class
198+
)
199+
);
200+
// cause adapter is array, it should be identical
201+
$this->assertSame(
202+
$lookup,
203+
$resolver->lookup('example.com'),
204+
sprintf(
205+
'%1$s->lookup() should identical with current one',
206+
$resolver::class
207+
)
208+
);
209+
}
210+
211+
public function testLookups()
212+
{
213+
$resolver = new Resolver();
214+
$lookups = $resolver->lookups(
215+
'example.com',
216+
IN::NAME,
217+
...['A' => 'A', 'B' => 'NS']
218+
);
219+
$this->assertArrayHasKey(
220+
'A',
221+
$lookups,
222+
sprintf(
223+
'%1$s->lookups() result should contain given array key of "A"',
224+
$resolver::class
225+
)
226+
);
227+
$this->assertArrayHasKey(
228+
'B',
229+
$lookups,
230+
sprintf(
231+
'%1$s->lookups() result should contain given array key of "B"',
232+
$resolver::class
233+
)
234+
);
235+
if (!$lookups['A'] instanceof Throwable) {
236+
$this->assertInstanceOf(
237+
PacketResponseInterface::class,
238+
$lookups['A'],
239+
sprintf(
240+
'%1$s->lookups()[\'A\'] result should instanceof %2$s',
241+
$resolver::class,
242+
PacketResponseInterface::class
243+
)
244+
);
245+
$this->assertInstanceOf(
246+
Answers::class,
247+
$lookups['A']->getAnswers(),
248+
sprintf(
249+
'%1$s->lookups()[\'A\']->getAnswers() result should instanceof %2$s',
250+
$resolver::class,
251+
Answers::class
252+
)
253+
);
254+
$this->assertSame(
255+
'example.com',
256+
$lookups['A']->getAnswers()->getQuestion()->getName(),
257+
'Question name lookup should example.com'
258+
);
259+
$this->assertSame(
260+
IN::NAME,
261+
$lookups['A']->getAnswers()->getQuestion()->getClass()->getName(),
262+
sprintf(
263+
'Question class name lookup should %s',
264+
IN::NAME
265+
)
266+
);
267+
$records = $lookups['A']
268+
->getAnswers()
269+
->getRecords();
270+
// not exists
271+
$this->assertNull(
272+
$records->getFilteredType('AN'),
273+
'Checking record that not exists should null'
274+
);
275+
// no message
276+
$this->assertInstanceOf(
277+
A::class,
278+
$records->getFilteredType('A', true),
279+
'Checking record that exists should instance A'
280+
);
281+
} else {
282+
$this->assertInstanceOf(
283+
Exception::class,
284+
$lookups['A'],
285+
sprintf(
286+
'%1$s->lookups()[\'A\'] exception result should instanceof %2$s',
287+
$resolver::class,
288+
Exception::class
289+
)
290+
);
291+
}
292+
293+
if (!$lookups['B'] instanceof Throwable) {
294+
$this->assertInstanceOf(
295+
PacketResponseInterface::class,
296+
$lookups['B'],
297+
sprintf(
298+
'%1$s->lookups()[\'B\'] result should instanceof %2$s',
299+
$resolver::class,
300+
PacketResponseInterface::class
301+
)
302+
);
303+
$this->assertInstanceOf(
304+
Answers::class,
305+
$lookups['B']->getAnswers(),
306+
sprintf(
307+
'%1$s->lookups()[\'B\']->getAnswers() result should instanceof %2$s',
308+
$resolver::class,
309+
Answers::class
310+
)
311+
);
312+
$this->assertSame(
313+
'example.com',
314+
$lookups['B']->getAnswers()->getQuestion()->getName(),
315+
'Question name lookup should example.com'
316+
);
317+
$this->assertSame(
318+
IN::NAME,
319+
$lookups['B']->getAnswers()->getQuestion()->getClass()->getName(),
320+
sprintf(
321+
'Question class name lookup should %s',
322+
IN::NAME
323+
)
324+
);
325+
} else {
326+
$this->assertInstanceOf(
327+
Exception::class,
328+
$lookups['B'],
329+
sprintf(
330+
'%1$s->lookups()[\'B\'] exception result should instanceof %2$s',
331+
$resolver::class,
332+
Exception::class
333+
)
334+
);
335+
}
336+
}
337+
}

0 commit comments

Comments
 (0)