Skip to content

Commit

Permalink
Merge 4e17a6f into c5a9de5
Browse files Browse the repository at this point in the history
  • Loading branch information
codenamegary committed Nov 21, 2014
2 parents c5a9de5 + 4e17a6f commit c5dbcc9
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ composer.lock
.project
/vendor/
.README.md.html
/.idea
/tests/clover.xml
4 changes: 3 additions & 1 deletion src/JsonStorageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace yswery\DNS;

use \Exception;

class JsonStorageProvider extends AbstractStorageProvider {

private $dns_records;
Expand All @@ -10,7 +12,7 @@ class JsonStorageProvider extends AbstractStorageProvider {

public function __construct($record_file, $default_ttl = 300)
{
$handle = fopen($record_file, "r");
$handle = @fopen($record_file, "r");
if(!$handle) {
throw new Exception('Unable to open dns record file.');
}
Expand Down
57 changes: 56 additions & 1 deletion tests/JsonStorageProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,62 @@ public function testHostRecordResolves()
));
$answer = $this->storage->get_answer($question);
$this->assertTrue($answer === $expected);

}

public function testUnconfiguredRecordDoesNotResolve()
{
$question = array(array(
'qname' => 'testestestes.com',
'qtype' => \yswery\DNS\RecordTypeEnum::TYPE_A,
'qclass' => 1,
));
$answer = $this->storage->get_answer($question);
$this->assertTrue($answer === array());
}

public function testHostRecordReturnsArray()
{
$question = array(array(
'qname' => 'test2.com',
'qtype' => \yswery\DNS\RecordTypeEnum::TYPE_A,
'qclass' => 1,
));
$expected = array(
array(
'name' => 'test2.com',
'class' => 1,
'ttl' => 300,
'data' => array(
'type' => 1,
'value' => '111.111.111.111',
),
),
array(
'name' => 'test2.com',
'class' => 1,
'ttl' => 300,
'data' => array(
'type' => 1,
'value' => '112.112.112.112',
),
),
);
$answer = $this->storage->get_answer($question);
$this->assertTrue($answer === $expected);
}

public function testConstructorThrowsExceptions()
{
$this->setExpectedException('Exception', 'Unable to open dns record file.');
$jsonAdapter = new \yswery\DNS\JsonStorageProvider('blah.json');
$this->setExpectedException('Exception', 'Unable to parse dns record file.');
$jsonAdapter = new \yswery\DNS\JsonStorageProvider('invalid_dns_records.json');
}

public function testConstructorLoadsRecords()
{
$this->storage = new \yswery\DNS\JsonStorageProvider(__DIR__ . '/test_records.json');
$this->assertTrue($this->storage !== false);
}

}
57 changes: 57 additions & 0 deletions tests/RecordTypeEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

class RecordTypeEnumTest extends PHPUnit_Framework_TestCase {

/**
* @var yswery\DNS\RecordTypeEnum
*/
protected $recordTypes;

public function setUp()
{
$this->recordTypes = new \yswery\DNS\RecordTypeEnum;
}

public function testGetsHostRecordIndex()
{
$hostIndex = $this->recordTypes->get_type_index('A');
$this->assertTrue($hostIndex === \yswery\DNS\RecordTypeEnum::TYPE_A);
}

public function testDoesNotGetInvalidRecordTypeIndex()
{
$hostIndex = $this->recordTypes->get_type_index('BLAH');
$this->assertTrue($hostIndex === false);
}

public function testGetsNameFromType()
{
$typeName = $this->recordTypes->get_name(\yswery\DNS\RecordTypeEnum::TYPE_A);
$this->assertTrue('A' === $typeName);
}

public function testDoesNotGetInvalidNameFromType()
{
$typeName = $this->recordTypes->get_name(932);
$this->assertTrue(false === $typeName);
}

public function testGetTypes()
{
$expected = array(
'A' => 1,
'NS' => 2,
'CNAME' => 5,
'SOA' => 6,
'PTR' => 12,
'MX' => 15,
'TXT' => 16,
'AAAA' => 28,
'OPT' => 41,
'AXFR' => 252,
'ANY' => 255,
);
$this->assertTrue($this->recordTypes->get_types() === $expected);
}

}
52 changes: 0 additions & 52 deletions tests/clover.xml

This file was deleted.

1 change: 1 addition & 0 deletions tests/invalid_dns_records.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ alkj }

0 comments on commit c5dbcc9

Please sign in to comment.