Skip to content

Commit

Permalink
Update RequestTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Feb 5, 2025
1 parent 1b458f0 commit 0e37beb
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions tests/Unit/Abstract/Requests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
function getSut(): Request
{
return new class extends Request {
public function publicDecodeHashedIds(array $requestData): array
{
return $this->decodeHashedIds($requestData);
}

public function setDecodeArray(array $decode): void
{
$this->decode = $decode;
Expand All @@ -39,18 +34,20 @@ public function setDecodeArray(array $decode): void
it('skips decoding if disabled', function (): void {
config(['apiato.hash-id' => false]);
$data = ['id' => hashids()->tryEncode(123)];
$sut = getSut()->merge($data);
$sut->setDecodeArray(['id']);

$result = getSut()->publicDecodeHashedIds($data);
$result = $sut->all();

expect($result)->toBe($data);
});

it('can decode hash ids', function (array $data, array $decode, array $expected): void {
$data = recursiveEncode($data);
$sut = getSut();
$sut = getSut()->merge($data);
$sut->setDecodeArray($decode);

$result = $sut->publicDecodeHashedIds($data);
$result = $sut->all();

expect($result)->toBe($expected);
})->with([
Expand Down Expand Up @@ -122,19 +119,19 @@ function recursiveEncode(array $data): array

it('can decode nested associative arrays', function (): void {
$data = ['nested' => ['ids' => [['first' => 1, 'second' => hashids()->tryEncode(2)]]]];
$sut = getSut();
$sut = getSut()->merge($data);
$sut->setDecodeArray(['nested.ids.*.second']);

$result = $sut->publicDecodeHashedIds($data);
$result = $sut->all();

expect($result)->toBe(['nested' => ['ids' => [['first' => 1, 'second' => 2]]]]);
});

it('throws in case of invalid hash id', function (array $data, array $decode): void {
$sut = getSut();
$sut = getSut()->merge($data);
$sut->setDecodeArray($decode);

expect(fn () => $sut->publicDecodeHashedIds($data))
expect(fn () => $sut->all())
->toThrow(\RuntimeException::class);
})->with([
'top level value' => [
Expand Down Expand Up @@ -167,4 +164,4 @@ function recursiveEncode(array $data): array

expect($result)->toBe(['age' => 100]);
});
});
})->only();

0 comments on commit 0e37beb

Please sign in to comment.