diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon deleted file mode 100644 index 8b13789..0000000 --- a/phpstan-baseline.neon +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 81b881a..9c33100 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -78,7 +78,7 @@ public function whereIn(string $property, array $values): static public function find(mixed $id): Model|null { - $response = Http::get($this->model->baseUrl().$this->toQuery($id))->throwUnlessStatus(404); + $response = Http::withHeaders($this->model->headers())->get($this->model->baseUrl().$this->toQuery($id))->throwUnlessStatus(404); if ($response->failed()) { return null; @@ -183,7 +183,7 @@ public function toQuery(string|int|null $id = null): string public function performCollectionQuery(): Response { - return Http::get($this->model->baseUrl().$this->toQuery()); + return Http::withHeaders($this->model->headers())->get($this->model->baseUrl().$this->toQuery()); } /** diff --git a/tests/FindTest.php b/tests/FindTest.php index 38e84f3..125e112 100644 --- a/tests/FindTest.php +++ b/tests/FindTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Illuminate\Http\Client\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Intermax\EloquentJsonApiClient\Tests\Team; @@ -11,8 +12,7 @@ uses(TestCase::class); it('fetches and hydrates a team', function () { - Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindResponse.json')) - ); + Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindResponse.json'))); $team = Team::find(1); @@ -24,17 +24,27 @@ }); it('fetches and hydrates a team with its relations', function () { - Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindRelationResponse.json')) - ); + Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindRelationResponse.json'))); $team = Team::query()->with(['members'])->find(1); assert($team instanceof Team); - Http::assertSent(fn (Request $request) => str_contains($request->url(), 'teams/1') && str_contains($request->url(), 'include=members') + Http::assertSent( + fn (Request $request) => str_contains($request->url(), 'teams/1') && str_contains($request->url(), 'include=members') ); expect($team)->toBeInstanceOf(Team::class) ->and($team->members)->toBeInstanceOf(Collection::class) ->and($team->members->first()->id)->toBe('5'); }); + +it('sends headers with the request', function () { + Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamFindResponse.json'))); + + Team::find(1); + + Http::assertSent( + fn (Request $request) => Arr::first($request->header('api-key')) === 'test' + ); +}); diff --git a/tests/GetTest.php b/tests/GetTest.php index ae8ace3..0c7ee42 100644 --- a/tests/GetTest.php +++ b/tests/GetTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Illuminate\Http\Client\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Intermax\EloquentJsonApiClient\Tests\Team; @@ -11,8 +12,7 @@ uses(TestCase::class); it('fetches teams', function () { - Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamGetResponse.json')) - ); + Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamGetResponse.json'))); $teams = Team::query()->get(); @@ -22,3 +22,13 @@ ->and($teams->count())->toBe(4) ->and($teams->first())->toBeInstanceOf(Team::class); }); + +it('sends headers with the request', function () { + Http::fake(fn () => Http::response(file_get_contents(__DIR__.'/Utilities/TeamGetResponse.json'))); + + Team::query()->get(); + + Http::assertSent( + fn (Request $request) => Arr::first($request->header('api-key')) === 'test' + ); +}); diff --git a/tests/Team.php b/tests/Team.php index 5473c61..9d6356a 100644 --- a/tests/Team.php +++ b/tests/Team.php @@ -38,4 +38,9 @@ public function baseUrl(): string { return 'https://localhost'; } + + public function headers(): array + { + return ['api-key' => 'test']; + } }