From acda41f6fda5e4a91c37d6cd7f4dfdcdfdf4b088 Mon Sep 17 00:00:00 2001 From: chthomas Date: Tue, 27 Oct 2020 11:09:07 -0400 Subject: [PATCH] Fix Dig response parsing when extra tabs are present --- src/Resolvers/Dig.php | 2 +- tests/Fixtures/DigResponses.php | 10 ++++++++++ tests/Unit/Resolvers/DigTest.php | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Resolvers/Dig.php b/src/Resolvers/Dig.php index 089a4ae..576dc04 100644 --- a/src/Resolvers/Dig.php +++ b/src/Resolvers/Dig.php @@ -84,7 +84,7 @@ private static function parseDigResponseToRows(string $digResponse): array private static function normalizeColumns(string $response): string { $keysRemoved = preg_replace('/;(.*)/m', ' ', trim($response)); - $tabsRemoved = preg_replace('/(\t)/m', ' ', (string) $keysRemoved); + $tabsRemoved = preg_replace('/(\t+)/m', ' ', (string) $keysRemoved); $breaksRemoved = preg_replace('/\s\s/m', '', (string) $tabsRemoved); return (string) preg_replace('/(\(\s|(\s\)))/m', '', (string) $breaksRemoved); } diff --git a/tests/Fixtures/DigResponses.php b/tests/Fixtures/DigResponses.php index 6c005f3..1f69de4 100644 --- a/tests/Fixtures/DigResponses.php +++ b/tests/Fixtures/DigResponses.php @@ -23,6 +23,16 @@ public static function ARecords(Hostname $hostname): string EOT; } + public static function ARecordsWithTabs(Hostname $hostname): string + { + return <<spatieDNS->method('getRecords') + ->with(Entities\DNSRecordType::TYPE_A) + ->willReturn(Tests\Fixtures\DigResponses::ARecordsWithTabs($this->hostname)); + + $records = $this->dig->getARecords((string) $this->hostname); + $this->assertCount(4, $records); + foreach ($records as $record) { + $this->assertTrue($record->getType()->equals(Entities\DNSRecordType::createA())); + } + } + public function testReturnsEmptyCollectionForUnsupportedQueryType(): void { $this->assertFalse(in_array(Entities\DNSRecordType::TYPE_PTR, Resolvers\Dig::SUPPORTED_QUERY_TYPES));