Skip to content

Commit

Permalink
Merge pull request #96 from lwohn/fix-ptr-record-type
Browse files Browse the repository at this point in the history
Fix PTR property naming.
  • Loading branch information
freekmurze authored Jul 29, 2022
2 parents 3857529 + 4e4a663 commit ef51d78
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
20 changes: 7 additions & 13 deletions src/Records/PTR.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Spatie\Dns\Records;

/**
* @method string reversDnsName()
* @method string target()
*/
class PTR extends Record
{
protected string $reversDnsName;
protected string $name;
protected string $target;

public static function parse(string $line): ?self
{
Expand All @@ -19,32 +18,27 @@ public static function parse(string $line): ?self
}

return static::make([
'reversDnsName' => $attributes[0],
'host' => $attributes[0],
'ttl' => $attributes[1],
'class' => $attributes[2],
'type' => $attributes[3],
'name' => $attributes[4],
'target' => $attributes[4],
]);
}

public function __toString(): string
{
return "{$this->reversDnsName}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t{$this->name}";
return "{$this->host}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t{$this->target}";
}

public function toArray()
{
return [
'reversDnsName' => $this->reversDnsName,
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'name' => $this->name,
'target' => $this->target,
];
}

protected function castReversDnsName(string $value): string
{
return $this->prepareDomain($value);
}
}
21 changes: 21 additions & 0 deletions tests/DnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Spatie\Dns\Records\A;
use Spatie\Dns\Records\MX;
use Spatie\Dns\Records\NS;
use Spatie\Dns\Records\PTR;
use Spatie\Dns\Records\Record;
use Spatie\Dns\Records\SOA;
use Spatie\Dns\Support\Collection;
Expand Down Expand Up @@ -117,6 +118,26 @@ public function it_fetches_records_for_given_type_and_ignores_record_chain()
$this->assertOnlySeeRecordTypes($records, [A::class]);
}

/** @test */
public function it_can_fetch_ptr_record()
{
$records = $this->dns->getRecords('1.73.1.5.in-addr.arpa', DNS_PTR);
$record = array_pop($records);

$ptrRecord = PTR::make([
'host' => '1.73.1.5.in-addr.arpa.',
'class' => 'IN',
'ttl' => 3600,
'type' => 'PTR',
'target' => 'ae0.452.fra.as205948.creoline.net.',
]);

$this->assertSame(
[$record->host(), $record->class(), $record->type(), $record->target()],
[$ptrRecord->host(), $ptrRecord->class(), $ptrRecord->type(), $ptrRecord->target()]
);
}

/** @test */
public function it_throws_an_exception_if_an_invalid_record_type_is_passed()
{
Expand Down
56 changes: 32 additions & 24 deletions tests/Records/PTRTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,75 @@

class PTRTest extends TestCase
{
/** @test */
public function it_can_parse_string()
public function rDnsProvider()
{
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.');
return [
['1.73.1.5.in-addr.arpa.', '1.73.1.5.in-addr.arpa'],
['1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.0.c.f.6.7.0.a.2.ip6.arpa.', '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.0.c.f.6.7.0.a.2.ip6.arpa'],
];
}

/** @test @dataProvider rDnsProvider */
public function it_can_parse_string($rDNS, $trimmedRDNS)
{
$record = PTR::parse($rDNS . ' 3600 IN PTR ae0.452.fra.as205948.creoline.net.');

$this->assertSame('1.73.1.5.in-addr.arpa', $record->reversDnsName());
$this->assertSame($trimmedRDNS, $record->host());
$this->assertSame(3600, $record->ttl());
$this->assertSame('IN', $record->class());
$this->assertSame('PTR', $record->type());
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->name());
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->target());
}

/** @test */
public function it_can_make_from_array()
/** @test @dataProvider rDnsProvider */
public function it_can_make_from_array($rDNS, $trimmedRDNS)
{
$record = PTR::make([
'reversDnsName' => '1.73.1.5.in-addr.arpa.',
'host' => $rDNS,
'class' => 'IN',
'ttl' => 3600,
'type' => 'PTR',
'name' => 'ae0.452.fra.as205948.creoline.net.',
'target' => 'ae0.452.fra.as205948.creoline.net.',
]);

$this->assertSame('1.73.1.5.in-addr.arpa', $record->reversDnsName());
$this->assertSame($trimmedRDNS, $record->host());
$this->assertSame(3600, $record->ttl());
$this->assertSame('IN', $record->class());
$this->assertSame('PTR', $record->type());
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->name());
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->target());
}

/** @test */
public function it_can_transform_to_string()
/** @test @dataProvider rDnsProvider */
public function it_can_transform_to_string($rDNS)
{
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.');
$record = PTR::parse($rDNS. ' 3600 IN PTR ae0.452.fra.as205948.creoline.net.');

$this->assertSame("1.73.1.5.in-addr.arpa.\t\t3600\tIN\tPTR\tae0.452.fra.as205948.creoline.net.", strval($record));
$this->assertSame($rDNS . "\t\t3600\tIN\tPTR\tae0.452.fra.as205948.creoline.net.", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
/** @test @dataProvider rDnsProvider */
public function it_can_be_converted_to_an_array($rDNS, $trimmedRDNS)
{
$record = PTR::make([
'reversDnsName' => '1.73.1.5.in-addr.arpa.',
'host' => $rDNS,
'class' => 'IN',
'ttl' => 3600,
'type' => 'PTR',
'name' => 'ae0.452.fra.as205948.creoline.net.',
'target' => 'ae0.452.fra.as205948.creoline.net.',
]);

$data = $record->toArray();
$this->assertSame('1.73.1.5.in-addr.arpa', $data['reversDnsName']);
$this->assertSame($trimmedRDNS, $data['host']);
$this->assertSame(3600, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('PTR', $data['type']);
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $data['name']);
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $data['target']);
}

/** @test */
public function it_return_null_for_to_few_attributes()
/** @test @dataProvider rDnsProvider */
public function it_return_null_for_to_few_attributes($rDNS)
{
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR');
$record = PTR::parse($rDNS . ' 3600 IN PTR');

$this->assertNull($record);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Support/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function dnsRecords(): array
[MX::class, 'spatie.be. 1665 IN MX 10 ASPMX.L.GOOGLE.COM.'],
[NS::class, 'spatie.be. 82516 IN NS ns1.openprovider.nl.'],
[PTR::class, '1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.'],
[PTR::class, '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.0.c.f.6.7.0.a.2.ip6.arpa 3600 IN PTR ae0.452.fra.as205948.creoline.net.'],
[SOA::class, 'spatie.be. 82393 IN SOA ns1.openprovider.nl. dns.openprovider.eu. 2020100801 10800 3600 604800 3600'],
[SRV::class, '_http._tcp.mxtoolbox.com. 3600 IN SRV 10 100 80 mxtoolbox.com.'],
[TXT::class, 'spatie.be. 594 IN TXT "v=spf1 include:eu.mailgun.org include:spf.factuursturen.be include:sendgrid.net a mx ~all"'],
Expand Down

0 comments on commit ef51d78

Please sign in to comment.