Skip to content

Commit

Permalink
fix: nullabe arrays of decoded ids were retrieved as [null] (#211)
Browse files Browse the repository at this point in the history
* fix: nullabe arrays of decoded ids were retrieved as [null]

* test: add covers attribute

---------

Co-authored-by: Mohammad Alavi <[email protected]>
  • Loading branch information
rscarrasco and Mohammad-Alavi authored Sep 29, 2024
1 parent 63d5b5d commit d6658de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Traits/HashIdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Traits/HashIdTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Apiato\Core\Tests\Unit\Traits;

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;

public function setUp(): void
{
parent::setUp();

$this->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);
}
}

0 comments on commit d6658de

Please sign in to comment.