Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion phpstan-baseline.neon

This file was deleted.

4 changes: 2 additions & 2 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down
20 changes: 15 additions & 5 deletions tests/FindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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'
);
});
14 changes: 12 additions & 2 deletions tests/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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'
);
});
5 changes: 5 additions & 0 deletions tests/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public function baseUrl(): string
{
return 'https://localhost';
}

public function headers(): array
{
return ['api-key' => 'test'];
}
}