From 9351e899d7f4276f4cae9c67423ea0d4a5f72475 Mon Sep 17 00:00:00 2001 From: Tristan Date: Tue, 24 Oct 2023 09:41:48 +0200 Subject: [PATCH] Prevent notice if no comments are passed --- src/Helper.php | 3 ++- tests/HelperTest.php | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Helper.php b/src/Helper.php index e1c8ecf..d07740c 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -35,7 +35,8 @@ public static function createResourceRecord( return (new ResourceRecord())->setApiResponse($name); } - ['name' => $name, 'type' => $type, 'ttl' => $ttl, 'content' => $content, 'comments' => $comments] = $name; + $comments = $name['comments'] ?? []; + ['name' => $name, 'type' => $type, 'ttl' => $ttl, 'content' => $content] = $name; } $name = str_replace('@', $zoneName, $name); diff --git a/tests/HelperTest.php b/tests/HelperTest.php index fdcbe63..b31ca12 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -59,6 +59,27 @@ public function testWithArray(): void self::assertSame('rooti', $result->getComments()[1]->getAccount()); } + public function testWithArrayWithoutOptionalFields(): void + { + $result = Helper::createResourceRecord( + 'unit.test.', + [ + 'name' => '@', + 'type' => RecordType::A, + 'content' => ['127.0.0.1', '127.0.0.2'], + 'ttl' => 1337, + ] + ); + + self::assertSame('unit.test.', $result->getName()); + self::assertSame('A', $result->getType()); + self::assertSame(1337, $result->getTtl()); + 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::assertEmpty($result->getComments()); + } + public function testWithApiResponse(): void { foreach (ZoneTest::API_RESPONSE['rrsets'] as $rrset) {