From e7852ae1ba3fe497b421072565121f39c2ab464a Mon Sep 17 00:00:00 2001 From: Mohammad Alavi Date: Sat, 14 Dec 2024 19:13:54 +0330 Subject: [PATCH] test(hasid): test additional conditions --- tests/Unit/Traits/HashIdTraitTest.php | 150 ++++++++++++++++++-------- 1 file changed, 107 insertions(+), 43 deletions(-) diff --git a/tests/Unit/Traits/HashIdTraitTest.php b/tests/Unit/Traits/HashIdTraitTest.php index bfa318fc..59f14606 100644 --- a/tests/Unit/Traits/HashIdTraitTest.php +++ b/tests/Unit/Traits/HashIdTraitTest.php @@ -8,6 +8,7 @@ use Mockery\LegacyMockInterface; use Mockery\MockInterface; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; #[CoversClass(HashIdTrait::class)] class HashIdTraitTest extends UnitTestCase @@ -15,6 +16,86 @@ 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(); @@ -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 = []; @@ -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); } }