Skip to content

Commit 035b3b3

Browse files
committed
fix: get by id returns null on 404
1 parent f397394 commit 035b3b3

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/RegistroSportESalute.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
* @phpstan-type OrganizationDetailData array{
2424
* stato: int,
2525
* errori: list<mixed>,
26-
* payload: array{
27-
* testata: array<string, string>,
28-
* immagine: array<string, string>,
29-
* corpo: array{
26+
* payload?: array{
27+
* testata?: array<string, string>,
28+
* immagine?: array<string, string>,
29+
* corpo?: array{
3030
* dati: list<array{ label: string, tipo: 'input'|'textarea'|'number', value: string|int }>
3131
* }
3232
* },
@@ -110,11 +110,11 @@ public function get(): Collection
110110
}
111111

112112
/**
113-
* @return array<string, int|string|null>
113+
* @return array<string, int|string|null>|null
114114
*
115115
* @throws RequestException
116116
*/
117-
public function getById(int $id): array
117+
public function getById(int $id): ?array
118118
{
119119
$key = "registro-sport-e-salute.detail.$id";
120120

@@ -128,6 +128,10 @@ public function getById(int $id): array
128128
->throwUnlessStatus(200)
129129
->json();
130130

131+
if (! isset($res['payload']) || empty($res['payload'])) {
132+
return null;
133+
}
134+
131135
return Arr::mapWithKeys($res['payload']['corpo']['dati'], fn (array $dato) => [$dato['label'] => $dato['value']]);
132136
});
133137
}

src/Testing/Fakes/RegistroSportESaluteFake.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CarloEusebi\RegistroSportESalute\Factories\OrganizationFactory;
66
use CarloEusebi\RegistroSportESalute\RegistroSportESalute;
77
use Faker\Factory;
8+
use Illuminate\Http\Client\RequestException;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Facades\Http;
1011
use Illuminate\Support\Testing\Fakes\Fake;
@@ -34,7 +35,12 @@ public function get(): Collection
3435
return collect(array_fill(0, $this->count, OrganizationFactory::create()));
3536
}
3637

37-
public function getById(int $id): array
38+
/**
39+
* @return array<string, int|string|null>|null
40+
*
41+
* @throws RequestException
42+
*/
43+
public function getById(int $id): ?array
3844
{
3945
if ($this->shouldThrowHttpException) {
4046
Http::fake([

tests/Feature/RegistroSportESaluteTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@
7272
expect(RegistroSportESalute::getCount())->toBe(5);
7373
});
7474

75+
it('handles 404 on get by id', function (): void {
76+
Http::fake([
77+
'https://registro.sportesalute.eu/api/istruttoria/*/sidebar*' => Http::response([
78+
'stato' => 0,
79+
'payload' => [],
80+
'errori' => [
81+
0 => [
82+
'codice' => '404',
83+
'campo' => '',
84+
'messaggio' => 'Non trovato',
85+
],
86+
],
87+
]),
88+
]);
89+
90+
expect(RegistroSportESalute::getById(1))->toBeNull();
91+
});
92+
7593
describe('exceptions', function (): void {
7694
beforeEach(function (): void {
7795
Http::fake([

0 commit comments

Comments
 (0)