Skip to content

Commit

Permalink
finish data type implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
chthomas committed Dec 5, 2018
1 parent 22ba452 commit 5d94e22
Show file tree
Hide file tree
Showing 20 changed files with 540 additions and 37 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ $cachedResolver = new Resolvers\Cached($cache, $resolverOfChoice, $TTL);

Take a look in the `src/Entities` to see what's available for you to query by and receive.

For records with extra type data, like SOA, TXT, MX, and NS there is a data attribute on `Entities\DNSRecord` that will be set with the proper type

**Reverse Lookup**

This is offered via a separate `ReverseDNSQuery` interface as it is not common or available for every type of DNS Resolver.
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/DataAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function createFromTypeAndString(DNSRecordType $recordType, string
if ($recordType->isA(DNSRecordType::TYPE_MX)) {
$exploded = explode(' ', $data);

return new MXData($exploded[1], (int)$exploded[0]);
return new MXData(new Hostname($exploded[1]), (int)$exploded[0]);
}

if ($recordType->isA(DNSRecordType::TYPE_SOA)) {
Expand All @@ -41,7 +41,7 @@ public static function createFromTypeAndString(DNSRecordType $recordType, string
}

if ($recordType->isA(DNSRecordType::TYPE_NS)) {
return new NSData($data);
return new NSData(new Hostname($data));
}

throw new InvalidArgumentException("{$data} could not be created with type {$recordType}");
Expand Down
13 changes: 7 additions & 6 deletions src/Entities/MXData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class MXData extends DataAbstract
{
/**
* @var string
* @var \RemotelyLiving\PHPDNS\Entities\Hostname
*/
private $target;

Expand All @@ -13,7 +13,7 @@ class MXData extends DataAbstract
*/
private $priority;

public function __construct(string $target, int $priority = 0)
public function __construct(Hostname $target, int $priority = 0)
{
$this->target = $target;
$this->priority = $priority;
Expand All @@ -24,7 +24,7 @@ public function __toString(): string
return "{$this->priority} {$this->target}";
}

public function getTarget(): string
public function getTarget(): Hostname
{
return $this->target;
}
Expand All @@ -37,7 +37,7 @@ public function getPriority(): int
public function toArray(): array
{
return [
'target' => $this->target,
'target' => (string)$this->target,
'priority' => $this->priority,
];
}
Expand All @@ -49,7 +49,8 @@ public function serialize(): string

public function unserialize($serialized): void
{
$this->target = $serialized['target'];
$this->priority = $serialized['priority'];
$unserialized = \unserialize($serialized);
$this->target = new Hostname($unserialized['target']);
$this->priority = $unserialized['priority'];
}
}
13 changes: 7 additions & 6 deletions src/Entities/NSData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
class NSData extends DataAbstract
{
/**
* @var string
* @var \RemotelyLiving\PHPDNS\Entities\Hostname
*/
private $target;

public function __construct(string $target)
public function __construct(Hostname $target)
{
$this->target = $target;
}

public function __toString(): string
{
return $this->target;
return (string)$this->target;
}

public function getTarget(): string
public function getTarget(): Hostname
{
return $this->target;
}

public function toArray(): array
{
return [
'target' => $this->target,
'target' => (string)$this->target,
];
}

Expand All @@ -37,6 +37,7 @@ public function serialize(): string

public function unserialize($serialized): void
{
$this->target = $serialized['target'];
$unserialized = \unserialize($serialized);
$this->target = new Hostname($unserialized['target']);
}
}
15 changes: 8 additions & 7 deletions src/Entities/SOAData.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ public function serialize(): string

public function unserialize($serialized): void
{
$this->mname = new Hostname($serialized['mname']);
$this->rname = new Hostname($serialized['rname']);
$this->serial = $serialized['serial'];
$this->refresh = $serialized['refresh'];
$this->retry = $serialized['retry'];
$this->expire = $serialized['expire'];
$this->minTTL = $serialized['minimumTTL'];
$unserialized = \unserialize($serialized);
$this->mname = new Hostname($unserialized['mname']);
$this->rname = new Hostname($unserialized['rname']);
$this->serial = $unserialized['serial'];
$this->refresh = $unserialized['refresh'];
$this->retry = $unserialized['retry'];
$this->expire = $unserialized['expire'];
$this->minTTL = $unserialized['minimumTTL'];
}
}
3 changes: 2 additions & 1 deletion src/Entities/TXTData.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function serialize(): string

public function unserialize($serialized): void
{
$this->value = $serialized['value'];
$unserialized = \unserialize($serialized);
$this->value = $unserialized['value'];
}
}
13 changes: 12 additions & 1 deletion tests/Unit/BaseTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
namespace RemotelyLiving\PHPDNS\Tests\Unit;

use PHPUnit\Framework\TestCase;
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;

abstract class BaseTestAbstract extends TestCase
{
public function assertSerializable(Serializable $serializable)
protected function assertSerializable(Serializable $serializable)
{
$this->assertEquals($serializable, \unserialize(\serialize($serializable)));
}

protected function assertArrayableAndEquals(array $expected, Arrayable $arrayable)
{
$this->assertEquals($expected, $arrayable->toArray());
}

protected function assertStringableAndEquals(string $expected, string $stringable)
{
$this->assertSame($expected, $stringable);
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Entities/DNSRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ public function isArrayable()
{
$this->assertInstanceOf(Arrayable::class, $this->DNSARecord);
$this->assertEquals([
'hostname' => 'google.com',
'hostname' => 'google.com.',
'type' => 'A',
'TTL' => 123,
'class' => 'AS',
'IPAddress' => '127.0.0.1',
], $this->DNSARecord->toArray());

$this->assertEquals([
'hostname' => 'google.com',
'hostname' => 'google.com.',
'type' => 'TXT',
'TTL' => 123,
'class' => 'AS',
'txt' => 'txtval'
'data' => 'txtval'
], $this->DNSTXTRecord->toArray());
}

Expand Down
114 changes: 114 additions & 0 deletions tests/Unit/Entities/DataAbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
namespace RemotelyLiving\PHPDNS\Tests\Unit\Entities;

use RemotelyLiving\PHPDNS\Entities\DataAbstract;
use RemotelyLiving\PHPDNS\Entities\DNSRecordType;
use RemotelyLiving\PHPDNS\Tests\Unit\BaseTestAbstract;

class DataAbstractTest extends BaseTestAbstract
{
/**
* @var \RemotelyLiving\PHPDNS\Entities\DataAbstract
*/
private $dataAbstract1;

/**
* @var \RemotelyLiving\PHPDNS\Entities\DataAbstract
*/
private $dataAbstract2;

protected function setUp()
{
parent::setUp();

$this->dataAbstract1 = new class extends DataAbstract {
public function __toString()
{
return 'dataAbstract1';
}

public function toArray(): array
{
return [];
}

public function serialize(): string
{
return 'seralized';
}

public function unserialize($serialized): void
{
}
};

$this->dataAbstract2 = new class extends DataAbstract {
public function __toString()
{
return 'dataAbstract2';
}

public function toArray(): array
{
return [];
}

public function serialize(): string
{
return 'seralized';
}

public function unserialize($serialized): void
{
}
};
}

/**
* @test
*/
public function knowsIfEquals()
{
$this->assertTrue($this->dataAbstract1->equals($this->dataAbstract1));
$this->assertFalse($this->dataAbstract1->equals($this->dataAbstract2));
}

/**
* @test
*/
public function createsDataByType()
{
/** @var \RemotelyLiving\PHPDNS\Entities\TXTData $txtData */
$txtData = $this->dataAbstract1::createFromTypeAndString(DNSRecordType::createTXT(), 'value');
$this->assertSame('value', $txtData->getValue());

/** @var \RemotelyLiving\PHPDNS\Entities\MXData $mxData */
$mxData = $this->dataAbstract1::createFromTypeAndString(DNSRecordType::createMX(), '60 target.com');
$this->assertSame('target.com.', (string)$mxData->getTarget());
$this->assertSame(60, $mxData->getPriority());

/** @var \RemotelyLiving\PHPDNS\Entities\NSData $nsData */
$nsData = $this->dataAbstract1::createFromTypeAndString(DNSRecordType::createNS(), 'target.com');
$this->assertSame('target.com.', (string)$nsData->getTarget());

/** @var \RemotelyLiving\PHPDNS\Entities\SOAData $soaData */
$soaString = 'ns1.google.com. dns-admin.google.com. 224049761 900 800 1800 60';
$soaData = $this->dataAbstract1::createFromTypeAndString(DNSRecordType::createSOA(), $soaString);
$this->assertSame('ns1.google.com.', (string)$soaData->getMname());
$this->assertSame('dns-admin.google.com.', (string)$soaData->getRname());
$this->assertSame(224049761, $soaData->getSerial());
$this->assertSame(900, $soaData->getRefresh());
$this->assertSame(800, $soaData->getRetry());
$this->assertSame(1800, $soaData->getExpire());
$this->assertSame(60, $soaData->getMinTTL());
}

/**
* @test
* @expectedException \RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException
*/
public function createsDataByTypeOrThrows()
{
$this->dataAbstract1::createFromTypeAndString(DNSRecordType::createA(), '');
}
}
8 changes: 4 additions & 4 deletions tests/Unit/Entities/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ protected function setUp()
*/
public function hasBasicGettersAndIsStringy()
{
$this->assertSame('facebook.com', (string)$this->hostname);
$this->assertSame('facebook.com', $this->hostname->getHostName());
$this->assertSame('facebook.com.', (string)$this->hostname);
$this->assertSame('facebook.com.', $this->hostname->getHostName());
}

/**
Expand Down Expand Up @@ -54,10 +54,10 @@ public function doesNotAllowInvalidHostNames()
*/
public function handlesIDNOperations()
{
$utf8IDN = 'ańodelgatos.com';
$utf8IDN = 'ańodelgatos.com.';
$IDN = Hostname::createFromString($utf8IDN);

$expectedAscii = 'xn--aodelgatos-w0b.com';
$expectedAscii = 'xn--aodelgatos-w0b.com.';
$this->assertTrue($IDN->isPunycoded());
$this->assertSame($expectedAscii, $IDN->getHostName());
$this->assertSame($utf8IDN, $IDN->toUTF8());
Expand Down

0 comments on commit 5d94e22

Please sign in to comment.