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\Parser; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Classes; |
17
|
|
|
use Badcow\DNS\Parser\ParseException; |
18
|
|
|
use Badcow\DNS\Parser\Parser; |
19
|
|
|
use Badcow\DNS\Rdata\TXT; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
|
22
|
|
|
class CustomHandlerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function spfHandler(\ArrayIterator $iterator): TXT |
25
|
|
|
{ |
26
|
|
|
$string = ''; |
27
|
|
|
while ($iterator->valid()) { |
28
|
|
|
$string .= $iterator->current().' '; |
29
|
|
|
$iterator->next(); |
30
|
|
|
} |
31
|
|
|
$string = trim($string, ' "'); //Remove whitespace and quotes |
32
|
|
|
|
33
|
|
|
$spf = new TXT(); |
34
|
|
|
$spf->setText($string); |
35
|
|
|
|
36
|
|
|
return $spf; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws ParseException |
41
|
|
|
*/ |
42
|
|
|
public function testCustomHandler(): void |
43
|
|
|
{ |
44
|
|
|
$customHandlers = ['SPF' => [$this, 'spfHandler']]; |
45
|
|
|
|
46
|
|
|
$record = 'example.com. 7200 IN SPF "v=spf1 a mx ip4:69.64.153.131 include:_spf.google.com ~all"'; |
47
|
|
|
$parser = new Parser($customHandlers); |
48
|
|
|
$zone = $parser->makeZone('example.com.', $record); |
49
|
|
|
$rr = $zone->getResourceRecords()[0]; |
50
|
|
|
|
51
|
|
|
$this->assertEquals('TXT', $rr->getType()); |
52
|
|
|
$this->assertEquals('example.com.', $rr->getName()); |
53
|
|
|
$this->assertEquals(7200, $rr->getTtl()); |
54
|
|
|
$this->assertEquals(Classes::INTERNET, $rr->getClass()); |
55
|
|
|
$this->assertNotNull($rr->getRdata()); |
56
|
|
|
$this->assertEquals('v=spf1 a mx ip4:69.64.153.131 include:_spf.google.com ~all', $rr->getRdata()->getText()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: