Skip to content

Commit

Permalink
test(hasid): test additional conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Dec 14, 2024
1 parent 9af8006 commit e7852ae
Showing 1 changed file with 107 additions and 43 deletions.
150 changes: 107 additions & 43 deletions tests/Unit/Traits/HashIdTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,94 @@
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

#[CoversClass(HashIdTrait::class)]
class HashIdTraitTest extends UnitTestCase
{
private $trait;
private LegacyMockInterface|MockInterface $mockTrait;

public static function hashedIdsProvider(): array
{
$firstId = 1;
$secondId = 2;
return [
'top level value' => [
['id' => $firstId],
['id'],
['id' => 1],
],
'top level empty string' => [
['id' => ''],
['id'],
['id' => ''],
],
'nested value' => [
['data' => ['id' => $firstId]],
['data.id'],
['data' => ['id' => 1]],
],
'array' => [
['ids' => [$firstId, $secondId]],
['ids.*'],
['ids' => [1, 2]],
],
'nested array' => [
['nested' => ['ids' => [$firstId, $secondId]]],
['nested.ids.*'],
['nested' => ['ids' => [1, 2]]],
],
'string non existent key - should return value as is' => [
['non_existent_key' => 'value'],
['id'],
['non_existent_key' => 'value'],
],
'null top level value' => [
['id' => null],
['id'],
['id' => null],
],
'null nested value' => [
['data' => ['id' => null]],
['data.id'],
['data' => ['id' => null]],
],
'null array' => [
['ids' => [null, null]],
['ids.*'],
['ids' => [null, null]],
],
'null nested array' => [
['nested' => ['ids' => [null, null]]],
['nested.ids.*'],
['nested' => ['ids' => [null, null]]],
],
];
}

public static function invalidHashedIdsProvider(): array
{
return [
'top level value' => [
['id' => 'invalid'],
['id'],
],
'nested value' => [
['data' => ['id' => 'invalid']],
['data.id'],
],
'array' => [
['ids' => ['invalid', 'invalid']],
['ids.*'],
],
'nested array' => [
['nested' => ['ids' => ['invalid', 'invalid']]],
['nested.ids.*'],
],
];
}

public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -124,17 +205,7 @@ public function testGetHashedKeyReturnsNullForNullValue(): void
$this->assertNull($result);
}

public function testDecodeHashedIdsBeforeValidationDecodesIds(): void
{
$requestData = ['id' => $this->trait->encode(123)];
$this->trait->decode = ['id'];

$result = $this->trait->publicDecodeHashedIdsBeforeValidation($requestData);

$this->assertEquals(['id' => 123], $result);
}

public function testDecodeHashedIdsBeforeValidationSkipsDecodingIfDisabled(): void
public function testSkipsDecodingIfDisabled(): void
{
$requestData = ['id' => $this->trait->encode(123)];
$this->trait->decode = [];
Expand All @@ -144,60 +215,53 @@ public function testDecodeHashedIdsBeforeValidationSkipsDecodingIfDisabled(): vo
$this->assertEquals($requestData, $result);
}

public function testDecodeHashedIdsBeforeValidationDecodesNestedIds(): void
{
$requestData = ['data' => ['id' => $this->trait->encode(123)]];
$this->trait->decode = ['data.id'];

$result = $this->trait->publicDecodeHashedIdsBeforeValidation($requestData);

$this->assertEquals(['data' => ['id' => 123]], $result);
}

public function testDecodeHashedIdsBeforeValidationDecodesArrayOfIds(): void
#[DataProvider('hashedIdsProvider')]
public function testCanDecodeHashedIds(array $requestData, array $decode, array $expected): void
{
$requestData = ['ids' => [$this->trait->encode(1), $this->trait->encode(2)]];
$this->trait->decode = ['ids.*'];
$requestData = $this->recursiveEncode($requestData);
$this->trait->decode = $decode;

$result = $this->trait->publicDecodeHashedIdsBeforeValidation($requestData);

$this->assertEquals(['ids' => [1, 2]], $result);
$this->assertEquals($expected, $result);
}

public function testDecodeHashedIdsBeforeValidationThrowsIncorrectIdException(): void
private function recursiveEncode(array $data): array
{
$this->expectException(IncorrectIdException::class);
$this->expectExceptionMessage('ID (id) is incorrect, consider using the hashed ID.');

$requestData = ['id' => 'invalid'];
$this->trait->decode = ['id'];

$this->trait->publicDecodeHashedIdsBeforeValidation($requestData);
return array_map(function ($value) {
if (is_array($value)) {
return $this->recursiveEncode($value);
}
if (is_int($value)) {
return $this->trait->encode($value);
}
return $value;
}, $data);
}

public function testDecodeHashedIdsBeforeValidationReturnsDataWhenSkipHashIdDecodeIsTrue(): void
public function testCanDecodeNestedAssocArray(): void
{
$requestData = ['id' => ''];
$this->trait->decode = ['id'];
$requestData = ['nested' => ['ids' => [['first' => 1, 'second' => $this->encode(2)]]]];
$this->trait->decode = ['nested.ids.*.second'];

$result = $this->trait->publicDecodeHashedIdsBeforeValidation($requestData);

$this->assertEquals($requestData, $result);
$this->assertEquals(['nested' => ['ids' => [['first' => 1, 'second' => 2]]]], $result);
}

public function testDecodeReturnsNullForNonHashString(): void
{
$result = $this->trait->decode('string_id');
$result = $this->trait->decode('non_hash_string');
$this->assertNull($result);
}

public function testDecodeHashedIdsBeforeValidationReturnsDataWhenKeyDoesNotExist(): void
#[DataProvider('invalidHashedIdsProvider')]
public function testThrowsIncorrectIdException(array $requestData, array $decode): void
{
$requestData = ['non_existent_key' => 'value'];
$this->trait->decode = ['id'];
$this->expectException(IncorrectIdException::class);

$result = $this->trait->publicDecodeHashedIdsBeforeValidation($requestData);
$this->trait->decode = $decode;

$this->assertEquals($requestData, $result);
$this->trait->publicDecodeHashedIdsBeforeValidation($requestData);
}
}

0 comments on commit e7852ae

Please sign in to comment.