From 8c70c710da8e45173fc48670b3d84ca5aac88841 Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Tue, 17 Oct 2023 21:29:30 +0200 Subject: [PATCH 1/6] fix: add transformer for comments --- src/Transformers/CommentTransformer.php | 18 ++++++++++++++++++ src/Transformers/RRSetTransformer.php | 3 +++ 2 files changed, 21 insertions(+) create mode 100644 src/Transformers/CommentTransformer.php diff --git a/src/Transformers/CommentTransformer.php b/src/Transformers/CommentTransformer.php new file mode 100644 index 0000000..46c7119 --- /dev/null +++ b/src/Transformers/CommentTransformer.php @@ -0,0 +1,18 @@ + $this->data->getContent(), + 'account' => $this->data->getAccount(), + 'modified_at' => $this->data->getModifiedAt(), + ]; + } +} diff --git a/src/Transformers/RRSetTransformer.php b/src/Transformers/RRSetTransformer.php index d3abe92..dbf998e 100644 --- a/src/Transformers/RRSetTransformer.php +++ b/src/Transformers/RRSetTransformer.php @@ -41,6 +41,9 @@ private function transformResourceRecord(ResourceRecord $resourceRecord) 'ttl' => $resourceRecord->getTtl(), 'changetype' => $resourceRecord->getChangeType(), 'records' => $recordList, + 'comments' => array_map(function($comment) { + return (new CommentTransformer($comment))->transform(); + } ,$resourceRecord->getComments()) ]; } From dda46344a2559cc8eae27548b71854d713ae0a1d Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Tue, 17 Oct 2023 21:29:39 +0200 Subject: [PATCH 2/6] chore: add tests for comment-transformer --- tests/Resources/CommentTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Resources/CommentTest.php b/tests/Resources/CommentTest.php index d29eb31..08af106 100644 --- a/tests/Resources/CommentTest.php +++ b/tests/Resources/CommentTest.php @@ -3,6 +3,7 @@ namespace Exonet\Powerdns\tests\Resources; use Exonet\Powerdns\Resources\Comment; +use Exonet\Powerdns\Transformers\CommentTransformer; use PHPUnit\Framework\TestCase; /** @@ -23,4 +24,20 @@ public function testSettersAndGetters(): void $this->assertSame('test content', $comment->getContent()); $this->assertSame(1234, $comment->getModifiedAt()); } + + public function testTransformer(): void + { + $comment = (new Comment()) + ->setAccount('test account') + ->setContent('test content') + ->setModifiedAt(1234); + + $transformer = new CommentTransformer($comment); + + $this->assertEquals((object) [ + 'modified_at' => 1234, + 'account' => 'test account', + 'content' => 'test content' + ], $transformer->transform()); + } } From 1749c37a0fe7b397f3491bec67c1e108c132a919 Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Tue, 17 Oct 2023 22:33:11 +0200 Subject: [PATCH 3/6] fix: added comments to every other place on zone creation --- src/Helper.php | 21 ++++++++++++++++--- src/Zone.php | 30 +++++++++++++++------------- tests/HelperTest.php | 16 ++++++++++++++- tests/functional/ZoneRecordsTest.php | 20 ++++++++++--------- 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/Helper.php b/src/Helper.php index 7b39c53..46c5c6f 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -2,6 +2,7 @@ namespace Exonet\Powerdns; +use Exonet\Powerdns\Resources\Comment; use Exonet\Powerdns\Resources\Record; use Exonet\Powerdns\Resources\ResourceRecord; @@ -15,6 +16,7 @@ class Helper * @param string $type The type of the resource record. * @param array|string $content The content of the resource record. * @param int $ttl The TTL. + * @param array $comments The Comment. * * @throws Exceptions\InvalidRecordType If the given type is invalid. * @@ -25,14 +27,15 @@ public static function createResourceRecord( $name, string $type = '', $content = '', - int $ttl = 3600 + int $ttl = 3600, + array $comments = [] ): ResourceRecord { if (is_array($name)) { if (isset($name['records'])) { return (new ResourceRecord())->setApiResponse($name); } - ['name' => $name, 'type' => $type, 'ttl' => $ttl, 'content' => $content] = $name; + ['name' => $name, 'type' => $type, 'ttl' => $ttl, 'content' => $content, 'comments' => $comments] = $name; } $name = str_replace('@', $zoneName, $name); @@ -48,11 +51,23 @@ public static function createResourceRecord( ->setName($name) ->setType($type) ->setTtl($ttl); - + if (is_string($content)) { $content = [$content]; } + if (is_array($comments)) { + $commentList = []; + foreach ($comments as $comment) { + // we can use an empty fallback for account and nothing the current time for modified_at + $commentList[] = (new Comment()) + ->setContent($comment['content']) + ->setAccount($comment['account'] ?? '') + ->setModifiedAt($comment['modified_at'] ?? time()); + } + $resourceRecord->setComments($commentList); + } + $recordList = []; foreach ($content as $record) { $recordItem = new Record(); diff --git a/src/Zone.php b/src/Zone.php index 2485d1f..2aa59d6 100644 --- a/src/Zone.php +++ b/src/Zone.php @@ -21,25 +21,26 @@ class Zone extends AbstractZone * resource records will be created in a single call to the PowerDNS server. If $name is a string, a single resource * record is created. * - * @param mixed[]|string $name The resource record name. - * @param string $type The type of the resource record. - * @param mixed[]|string $content The content of the resource record. When passing a multidimensional array, - * multiple records are created for this resource record. - * @param int $ttl The TTL. + * @param mixed[]|string $name The resource record name. + * @param string $type The type of the resource record. + * @param mixed[]|string $content The content of the resource record. When passing a multidimensional array, + * multiple records are created for this resource record. + * @param mixed[]|array $comments The comment to assign to the record. + * @param int $ttl The TTL. * * @throws Exceptions\InvalidRecordType If the given type is invalid. * * @return bool True when created. */ - public function create($name, string $type = '', $content = '', int $ttl = 3600): bool + public function create($name, string $type = '', $content = '', int $ttl = 3600, array $comments = []): bool { if (is_array($name)) { $resourceRecords = []; foreach ($name as $item) { - $resourceRecords[] = $this->make($item['name'], $item['type'], $item['content'], $item['ttl'] ?? $ttl); + $resourceRecords[] = $this->make($item['name'], $item['type'], $item['content'], $item['ttl'] ?? $ttl, $item['comments'] ?? []); } } else { - $resourceRecords = [$this->make($name, $type, $content, $ttl)]; + $resourceRecords = [$this->make($name, $type, $content, $ttl, $comments)]; } return $this->patch($resourceRecords); @@ -138,18 +139,19 @@ public function find(string $resourceRecordName, ?string $recordType = null): Re /** * Make (but not insert/POST) a new resource record. * - * @param string $name The resource record name. - * @param string $type The type of the resource record. - * @param string $content The content of the resource record. - * @param int $ttl The TTL. + * @param string $name The resource record name. + * @param string $type The type of the resource record. + * @param string $content The content of the resource record. + * @param int $ttl The TTL. + * @param array $comments The Comments. * * @throws Exceptions\InvalidRecordType If the given type is invalid. * * @return ResourceRecord The constructed ResourceRecord. */ - public function make(string $name, string $type, $content, int $ttl): ResourceRecord + public function make(string $name, string $type, $content, int $ttl, array $comments): ResourceRecord { - return Helper::createResourceRecord($this->zone, compact('name', 'type', 'content', 'ttl')); + return Helper::createResourceRecord($this->zone, compact('name', 'type', 'content', 'ttl', 'comments')); } /** diff --git a/tests/HelperTest.php b/tests/HelperTest.php index d24d388..72ca362 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -13,13 +13,14 @@ class HelperTest extends TestCase { public function testWithArguments(): void { - $result = Helper::createResourceRecord('unit.test.', 'www', RecordType::A, '127.0.0.1', 1337); + $result = Helper::createResourceRecord('unit.test.', 'www', RecordType::A, '127.0.0.1', 1337, [['content' => 'Hello World', 'account' => 'Tester']]); self::assertSame('www.unit.test.', $result->getName()); self::assertSame('A', $result->getType()); self::assertSame(1337, $result->getTtl()); self::assertCount(1, $result->getRecords()); self::assertSame('127.0.0.1', $result->getRecords()[0]->getContent()); + self::assertSame('Hello World', $result->getComments()[0]->getContent()); } public function testWithArray(): void @@ -31,6 +32,18 @@ public function testWithArray(): void 'type' => RecordType::A, 'content' => ['127.0.0.1', '127.0.0.2'], 'ttl' => 1337, + 'comments' => [ + [ + 'content' => "Hello", + 'account' => 'rooti', + 'modified_at' => 999 + ], + [ + 'content' => "World", + 'account' => 'rooti', + 'modified_at' => 111 + ] + ] ] ); @@ -40,6 +53,7 @@ public function testWithArray(): void self::assertCount(2, $result->getRecords()); self::assertSame('127.0.0.1', $result->getRecords()[0]->getContent()); self::assertSame('127.0.0.2', $result->getRecords()[1]->getContent()); + self::assertSame(111, $result->getComments()[1]->getModifiedAt()); } public function testWithApiResponse(): void diff --git a/tests/functional/ZoneRecordsTest.php b/tests/functional/ZoneRecordsTest.php index 1ca0f8a..6e461a7 100644 --- a/tests/functional/ZoneRecordsTest.php +++ b/tests/functional/ZoneRecordsTest.php @@ -18,20 +18,21 @@ class ZoneRecordsTest extends FunctionalTestCase private $canonicalName; private $dnsRecords = [ - ['name' => 'www', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60], - ['name' => 'www', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60], - ['name' => 'bla', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60], - ['name' => '@', 'type' => RecordType::unknownTypePrefix. 65534, 'content' => '\# 4 aabbccdd', 'ttl' => 60], - ['name' => '@', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60], + ['name' => 'www', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60, 'comments' => []], + ['name' => 'www', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60, 'comments' => []], + ['name' => 'bla', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60, 'comments' => []], + ['name' => '@', 'type' => RecordType::unknownTypePrefix. 65534, 'content' => '\# 4 aabbccdd', 'ttl' => 60, 'comments' => []], + ['name' => '@', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60, 'comments' => []], [ 'name' => '@', 'type' => RecordType::SOA, 'content' => 'ns1.test. hostmaster.test. 0 10800 3605 604800 3600', - 'ttl' => 60, + 'ttl' => 60, + 'comments' => [], ], - ['name' => '@', 'type' => RecordType::NS, 'content' => 'ns1.powerdns-php.', 'ttl' => 60], - ['name' => '@', 'type' => RecordType::NS, 'content' => 'ns2.powerdns-php.', 'ttl' => 60], - ['name' => '@', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60], + ['name' => '@', 'type' => RecordType::NS, 'content' => 'ns1.powerdns-php.', 'ttl' => 60, 'comments' => []], + ['name' => '@', 'type' => RecordType::NS, 'content' => 'ns2.powerdns-php.', 'ttl' => 60, 'comments' => []], + ['name' => '@', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60, 'comments' => []], ]; protected function setUp(): void @@ -89,6 +90,7 @@ function (ResourceRecord $item) use (&$createdRecords) { 'type' => $item->getType(), 'content' => $content, 'ttl' => $item->getTtl(), + 'comments' => [] ]; } } From d26297d47b892e8f0e3543a75fecc0a3f1d08d8f Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Wed, 18 Oct 2023 17:45:57 +0200 Subject: [PATCH 4/6] chore: extended tests --- tests/HelperTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 72ca362..fdcbe63 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -21,6 +21,7 @@ public function testWithArguments(): void self::assertCount(1, $result->getRecords()); self::assertSame('127.0.0.1', $result->getRecords()[0]->getContent()); self::assertSame('Hello World', $result->getComments()[0]->getContent()); + self::assertSame('Tester', $result->getComments()[0]->getAccount()); } public function testWithArray(): void @@ -34,16 +35,16 @@ public function testWithArray(): void 'ttl' => 1337, 'comments' => [ [ - 'content' => "Hello", + 'content' => 'Hello', 'account' => 'rooti', - 'modified_at' => 999 + 'modified_at' => 999, ], [ - 'content' => "World", + 'content' => 'World', 'account' => 'rooti', - 'modified_at' => 111 - ] - ] + 'modified_at' => 111, + ], + ], ] ); @@ -54,6 +55,8 @@ public function testWithArray(): void self::assertSame('127.0.0.1', $result->getRecords()[0]->getContent()); self::assertSame('127.0.0.2', $result->getRecords()[1]->getContent()); self::assertSame(111, $result->getComments()[1]->getModifiedAt()); + self::assertSame('World', $result->getComments()[1]->getContent()); + self::assertSame('rooti', $result->getComments()[1]->getAccount()); } public function testWithApiResponse(): void From ffd431451b22c33279f69ea0fc2a090bf179f243 Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Wed, 18 Oct 2023 17:46:06 +0200 Subject: [PATCH 5/6] chore: formatting --- src/Helper.php | 4 ++-- src/Transformers/RRSetTransformer.php | 4 ++-- src/Zone.php | 2 +- tests/Resources/CommentTest.php | 2 +- tests/functional/ZoneRecordsTest.php | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Helper.php b/src/Helper.php index 46c5c6f..e1c8ecf 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -16,7 +16,7 @@ class Helper * @param string $type The type of the resource record. * @param array|string $content The content of the resource record. * @param int $ttl The TTL. - * @param array $comments The Comment. + * @param array $comments The Comment. * * @throws Exceptions\InvalidRecordType If the given type is invalid. * @@ -51,7 +51,7 @@ public static function createResourceRecord( ->setName($name) ->setType($type) ->setTtl($ttl); - + if (is_string($content)) { $content = [$content]; } diff --git a/src/Transformers/RRSetTransformer.php b/src/Transformers/RRSetTransformer.php index dbf998e..82ed7f1 100644 --- a/src/Transformers/RRSetTransformer.php +++ b/src/Transformers/RRSetTransformer.php @@ -41,9 +41,9 @@ private function transformResourceRecord(ResourceRecord $resourceRecord) 'ttl' => $resourceRecord->getTtl(), 'changetype' => $resourceRecord->getChangeType(), 'records' => $recordList, - 'comments' => array_map(function($comment) { + 'comments' => array_map(function ($comment) { return (new CommentTransformer($comment))->transform(); - } ,$resourceRecord->getComments()) + }, $resourceRecord->getComments()), ]; } diff --git a/src/Zone.php b/src/Zone.php index 2aa59d6..4a2a6b9 100644 --- a/src/Zone.php +++ b/src/Zone.php @@ -25,7 +25,7 @@ class Zone extends AbstractZone * @param string $type The type of the resource record. * @param mixed[]|string $content The content of the resource record. When passing a multidimensional array, * multiple records are created for this resource record. - * @param mixed[]|array $comments The comment to assign to the record. + * @param array|mixed[] $comments The comment to assign to the record. * @param int $ttl The TTL. * * @throws Exceptions\InvalidRecordType If the given type is invalid. diff --git a/tests/Resources/CommentTest.php b/tests/Resources/CommentTest.php index 08af106..4c817b9 100644 --- a/tests/Resources/CommentTest.php +++ b/tests/Resources/CommentTest.php @@ -37,7 +37,7 @@ public function testTransformer(): void $this->assertEquals((object) [ 'modified_at' => 1234, 'account' => 'test account', - 'content' => 'test content' + 'content' => 'test content', ], $transformer->transform()); } } diff --git a/tests/functional/ZoneRecordsTest.php b/tests/functional/ZoneRecordsTest.php index 6e461a7..8613a05 100644 --- a/tests/functional/ZoneRecordsTest.php +++ b/tests/functional/ZoneRecordsTest.php @@ -27,7 +27,7 @@ class ZoneRecordsTest extends FunctionalTestCase 'name' => '@', 'type' => RecordType::SOA, 'content' => 'ns1.test. hostmaster.test. 0 10800 3605 604800 3600', - 'ttl' => 60, + 'ttl' => 60, 'comments' => [], ], ['name' => '@', 'type' => RecordType::NS, 'content' => 'ns1.powerdns-php.', 'ttl' => 60, 'comments' => []], @@ -90,7 +90,7 @@ function (ResourceRecord $item) use (&$createdRecords) { 'type' => $item->getType(), 'content' => $content, 'ttl' => $item->getTtl(), - 'comments' => [] + 'comments' => [], ]; } } From 865754bf21dd04bde717688e37a53f413ffabb9a Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Mon, 23 Oct 2023 14:02:25 +0200 Subject: [PATCH 6/6] chore: order of function args in comment --- src/Zone.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Zone.php b/src/Zone.php index 4a2a6b9..cb2c4ac 100644 --- a/src/Zone.php +++ b/src/Zone.php @@ -25,8 +25,8 @@ class Zone extends AbstractZone * @param string $type The type of the resource record. * @param mixed[]|string $content The content of the resource record. When passing a multidimensional array, * multiple records are created for this resource record. - * @param array|mixed[] $comments The comment to assign to the record. * @param int $ttl The TTL. + * @param array|mixed[] $comments The comment to assign to the record. * * @throws Exceptions\InvalidRecordType If the given type is invalid. *