From b8381a17432bcf97b3df03038c70280dcdb3de40 Mon Sep 17 00:00:00 2001 From: Rafael Carrasco Date: Wed, 25 Sep 2024 15:58:05 -0300 Subject: [PATCH 1/2] fix: nullabe arrays of decoded ids were retrieved as [null] --- src/Traits/HashIdTrait.php | 5 ++++ tests/Unit/Traits/HashIdTraitTest.php | 34 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/Unit/Traits/HashIdTraitTest.php diff --git a/src/Traits/HashIdTrait.php b/src/Traits/HashIdTrait.php index a2a720e6..fa87bb35 100644 --- a/src/Traits/HashIdTrait.php +++ b/src/Traits/HashIdTrait.php @@ -143,6 +143,11 @@ private function locateAndDecodeIds($requestData, $key): mixed */ private function processField($data, $keysTodo, $currentFieldName): mixed { + // is the current field a null?! we can give it back and chill out + if(is_null($data)) { + return $data; + } + // check if there are no more fields to be processed if (empty($keysTodo)) { // there are no more keys left - so basically we need to decode this entry diff --git a/tests/Unit/Traits/HashIdTraitTest.php b/tests/Unit/Traits/HashIdTraitTest.php new file mode 100644 index 00000000..ec88060f --- /dev/null +++ b/tests/Unit/Traits/HashIdTraitTest.php @@ -0,0 +1,34 @@ +trait = new class { + use HashIdTrait; + }; + } + + public function testProcessFieldShouldNotWrapANullInAnArray(): void + { + $data = null; + $keysTodo = ['*']; + $currentFieldName = null; + $reflection = new ReflectionClass($this->trait); + $method = $reflection->getMethod('processField'); + + $result = $method->invoke($this->trait, $data, $keysTodo, $currentFieldName); + + $this->assertNull($result); + } +} From 3b7cce6b82684db0feb8ae074643780b1b4458db Mon Sep 17 00:00:00 2001 From: Mohammad Alavi Date: Sun, 29 Sep 2024 15:22:54 +0330 Subject: [PATCH 2/2] test: add covers attribute --- tests/Unit/Traits/HashIdTraitTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Unit/Traits/HashIdTraitTest.php b/tests/Unit/Traits/HashIdTraitTest.php index ec88060f..0f2192d4 100644 --- a/tests/Unit/Traits/HashIdTraitTest.php +++ b/tests/Unit/Traits/HashIdTraitTest.php @@ -4,8 +4,10 @@ use Apiato\Core\Tests\Unit\UnitTestCase; use Apiato\Core\Traits\HashIdTrait; +use PHPUnit\Framework\Attributes\CoversClass; use ReflectionClass; +#[CoversClass(HashIdTrait::class)] class HashIdTraitTest extends UnitTestCase { private $trait;