Commit 4d0d5ce2 authored by Matt's avatar Matt

Separate Dig command from resolver class, add tests for resolver

parent f5acb246
Pipeline #49699779 failed with stages
in 3 minutes and 46 seconds
<?php namespace App\DNS;
class Dig
{
public function host(string $hostname, array $types): array
{
//$records = dns_get_record('serv.' . $hostname, DNS_TXT);
// dns_get_record appears to be broken (PHP native bug?!) for __long txt records__, use dig directly instead
$output = [];
$hostname = escapeshellarg($hostname);
$types = escapeshellarg(implode(' ', $types));
exec("dig $types +short $hostname", $output);
$results = array_map(function ($value) {
return trim($value, '"');
}, $output);
return $results;
}
}
<?php namespace App\DNS\Record;
use App\DNS;
class Resolver
{
const PREFIXES = ['ga', 'favicon', 'title', 'keywords', 'description', 'theme'];
const EXTERNAL_LINK_PREFIXES = ['css', 'script'];
public function getServRecords(string $hostname): Container
public function __construct(DNS\Dig $dig)
{
$this->dig = $dig;
}
public function getRecords(string $hostname): Container
{
$records = $this->getRecords('serv.' . $hostname, ['TXT']);
$records = $this->dig->host($hostname, ['TXT']);
foreach ($records as $key => $record) {
foreach (self::EXTERNAL_LINK_PREFIXES as $link_prefix) {
......@@ -57,26 +64,7 @@ class Resolver
'title' => $title ?? null,
'css' => $css ?? null,
'scripts' => $scripts ?? null,
'scripts' => $script ?? null,
]);
}
private function getRecords(string $hostname, array $types): array
{
//$records = dns_get_record('serv.' . $hostname, DNS_TXT);
// dns_get_record appears to be broken (PHP native bug?!) for __long txt records__, use dig directly instead
$output = [];
$hostname = escapeshellarg($hostname);
$types = escapeshellarg(implode(' ', $types));
exec("dig $types +short $hostname", $output);
$results = array_map(function ($value) {
return trim($value, '"');
}, $output);
return $results;
}
}
......@@ -20,7 +20,7 @@ class Service
public function process(string $host)
{
$records = $this->resolver->getServRecords($host);
$records = $this->resolver->getRecords('serv.' . $host);
if (! $abbr = $records->getEmmetRecord()) {
$this->log->notice('Emmet record was not found;', ['host' => $host, 'records' => $records->toArray()]);
......@@ -40,7 +40,7 @@ class Service
}
}
if ($host !== 'serv.from.zone') {
if ($host !== 'from.zone') {
$this->log->info('Successfully rendered;', ['host' => $host, 'records' => $records->toArray()]);
}
......
<?php namespace App\Tests\Unit\DNS\Record;
use App\Service;
use App\DNS;
use App\Emmet;
use Prophecy\Argument;
use Psr\Log\LoggerInterface as Log;
class ResolverTest extends \PHPUnit\Framework\TestCase
{
private $dig;
private $resolver;
public function setUp()
{
$this->dig = $this->prophesize(DNS\Dig::class);
$this->resolver = new DNS\Record\Resolver($this->dig->reveal());
}
/**
* @test
* @dataProvider txtRecordsProvider
*/
public function returned_container_must_map_txt_records_to_record_container(array $txt_records)
{
$this->dig->host($host = 'serv.example.com', ['TXT'])->willReturn($txt_records);
$container = $this->resolver->getRecords($host);
$this->assertEquals([
'emmet' => '<h1>testing</h1>',
'theme' => '000000',
'description' => null,
'keywords' => null,
'ga' => 'UA-abc123',
'favicon' => 'https://example.com/favicon.ico',
'title' => 'test title',
'css' => [
'https://example.com/css/styles.min.css',
],
'scripts' => [
'https://example.com/script.min.js',
],
], $container->toArray());
}
public function txtRecordsProvider(): array
{
return [
[
[
'title=test title',
'theme=000000',
'<h1>testing</h1>',
'favicon=https://example.com/favicon.ico',
'script=https://example.com/script.min.js',
'css=https://example.com/css/styles.min.css',
'ga=UA-abc123',
],
],
];
}
}
......@@ -37,7 +37,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
*/
public function missing_dns_records_should_provide_error_html(DNS\Record\Container $records)
{
$this->resolver->getServRecords($host = 'example.com')
$this->resolver->getRecords('serv.' . ($host = 'example.com'))
->willReturn($records);
$this->expander->expand(sprintf(Service::ERROR_NOT_FOUND, $host))
......@@ -54,7 +54,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
*/
public function a_missing_emmet_record_be_logged(DNS\Record\Container $records)
{
$this->resolver->getServRecords($host = 'example.com')
$this->resolver->getRecords('serv.' . ($host = 'example.com'))
->willReturn($records);
$this->expander->expand(sprintf(Service::ERROR_NOT_FOUND, $host))
......@@ -79,7 +79,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
*/
public function records_and_expanded_payload_will_be_returned(DNS\Record\Container $records)
{
$this->resolver->getServRecords($host = 'example.com')->willReturn($records);
$this->resolver->getRecords('serv.' . ($host = 'example.com'))->willReturn($records);
$this->expander->expand(self::SAMPLE_ABBR)->willReturn(self::SAMPLE_EXPAND);
......@@ -95,7 +95,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
*/
public function successful_foreign_hosts_should_be_logged(DNS\Record\Container $records)
{
$this->resolver->getServRecords($host = 'example.com')->willReturn($records);
$this->resolver->getRecords('serv.' . ($host = 'example.com'))->willReturn($records);
$this->expander->expand(self::SAMPLE_ABBR)->willReturn(self::SAMPLE_EXPAND);
......@@ -111,7 +111,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
*/
public function main_host_should_be_not_be_logged(DNS\Record\Container $records)
{
$this->resolver->getServRecords($host = 'serv.from.zone')->willReturn($records);
$this->resolver->getRecords('serv.' . ($host = 'from.zone'))->willReturn($records);
$this->expander->expand(self::SAMPLE_ABBR)->willReturn(self::SAMPLE_EXPAND);
......@@ -129,7 +129,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
{
$e = new Emmet\Exception\FailedExpansion(self::SAMPLE_ABBR, 1);
$this->resolver->getServRecords($host = 'example.com')->willReturn($records);
$this->resolver->getRecords('serv.' . ($host = 'example.com'))->willReturn($records);
$this->expander->expand(Service::ERROR_BAD_STRING)->willReturn(self::SAMPLE_EXPANDED_ERROR);
......@@ -149,7 +149,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
{
$e = new Emmet\Exception\FailedExpansion(self::SAMPLE_ABBR, 1);
$this->resolver->getServRecords($host = 'example.com')->willReturn($records);
$this->resolver->getRecords('serv.' . ($host = 'example.com'))->willReturn($records);
$this->expander->expand(Service::ERROR_BAD_STRING)->willReturn(self::SAMPLE_EXPANDED_ERROR);
......@@ -169,7 +169,7 @@ class ServiceTest extends \PHPUnit\Framework\TestCase
{
$e = new Emmet\Exception\LengthExceeded(Emmet\Expander::ABBR_LENGTH_MAX + 1);
$this->resolver->getServRecords($host = 'example.com')->willReturn($records);
$this->resolver->getRecords('serv.' . ($host = 'example.com'))->willReturn($records);
$this->expander->expand(Service::ERROR_BAD_STRING)->willReturn(self::SAMPLE_EXPANDED_ERROR);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment