Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 12, 2023
1 parent 4591dfe commit 7fb050c
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 180 deletions.
2 changes: 1 addition & 1 deletion tests/Feature/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testItCanPaginate(): void
}
Route::get('test-route', fn () => UserResource::collection(BasicModel::paginate(2)));

$response = $this->withoutExceptionHandling()->getJson('test-route');
$response = $this->getJson('test-route');

$response->assertOk();
$response->assertExactJson([
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/ResourceIdentificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testItThrowsWhenUnableToAutomaticallyResolveTheIdOfANonObject():
$this->expectException(ResourceIdentificationException::class);
$this->expectExceptionMessage('Unable to resolve resource object id for [array].');

$this->withoutExceptionHandling()->getJson('test-route');
$this->getJson('test-route');
}

public function testItThrowsWhenUnableToAutomaticallyResolveTheIdOfAnObject(): void
Expand All @@ -128,7 +128,7 @@ public function testItThrowsWhenUnableToAutomaticallyResolveTheIdOfAnObject(): v
$this->expectException(ResourceIdentificationException::class);
$this->expectExceptionMessage('Unable to resolve resource object id for [stdClass].');

$this->withoutExceptionHandling()->getJson('test-route');
$this->getJson('test-route');
}

public function testItThrowsWhenUnableToAutomaticallyResolveTheTypeOfANonObject(): void
Expand All @@ -144,7 +144,7 @@ public function toId($request): string
$this->expectException(ResourceIdentificationException::class);
$this->expectExceptionMessage('Unable to resolve resource object type for [array].');

$this->withoutExceptionHandling()->getJson('test-route');
$this->getJson('test-route');
}

public function testItThrowsWhenUnableToAutomaticallyResolveTypeOfAnObject(): void
Expand Down
43 changes: 0 additions & 43 deletions tests/Performance/FieldsTest.php

This file was deleted.

45 changes: 0 additions & 45 deletions tests/Performance/IncludesTest.php

This file was deleted.

65 changes: 0 additions & 65 deletions tests/Performance/RequestRelationshipsTest.php

This file was deleted.

30 changes: 13 additions & 17 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@

class TestCase extends BaseTestCase
{
public static $latestResponse;

public const JSON_API_SCHEMA_URL = 'http://jsonapi.org/schema';

public function setUp(): void
{
parent::setUp();

JsonApiResource::resolveServerImplementationNormally();

$this->withoutExceptionHandling();
}

protected function tearDown(): void
{
parent::tearDown();

JsonApiResource::resolveServerImplementationNormally();
}

protected function assertValidJsonApi(TestResponse|string|array $data): void
{
if ($data instanceof TestResponse) {
Expand All @@ -40,7 +43,12 @@ protected function assertValidJsonApi(TestResponse|string|array $data): void

$data = json_decode(json_encode($data));

$result = $this->jsonApiValidator()->validate($data, self::JSON_API_SCHEMA_URL);
$result = tap(new Validator, function ($validator) {
$validator->resolver()->registerFile(
self::JSON_API_SCHEMA_URL,
$this->localSchemaPath(self::JSON_API_SCHEMA_URL)
);
})->validate($data, self::JSON_API_SCHEMA_URL);

if ($result->isValid()) {
$this->assertTrue($result->isValid());
Expand All @@ -54,18 +62,6 @@ protected function assertValidJsonApi(TestResponse|string|array $data): void
);
}

private function jsonApiValidator(): Validator
{
$validator = new Validator();

$validator->resolver()->registerFile(
self::JSON_API_SCHEMA_URL,
$this->localSchemaPath(self::JSON_API_SCHEMA_URL)
);

return $validator;
}

private function localSchemaPath(string $url): string
{
$schema = __DIR__.'/../schema.json';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Unit;
namespace Tests\Unit\Exceptions;

use PHPUnit\Framework\TestCase;
use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException;
Expand All @@ -20,7 +20,7 @@ public function testItHandlesScalarsForId(): void
public function testItHandlesObjectsForId(): void
{
$this->expectException(ResourceIdentificationException::class);
$this->expectExceptionMessage('Unable to resolve resource object id for [Tests\Unit\MyTestClass].');
$this->expectExceptionMessage('Unable to resolve resource object id for [Tests\Unit\Exceptions\MyTestClass].');

throw ResourceIdentificationException::attemptingToDetermineIdFor(new MyTestClass());
}
Expand All @@ -36,7 +36,7 @@ public function testItHandlesScalarsForType(): void
public function testItHandlesObjectsForType(): void
{
$this->expectException(ResourceIdentificationException::class);
$this->expectExceptionMessage('Unable to resolve resource object type for [Tests\Unit\MyTestClass].');
$this->expectExceptionMessage('Unable to resolve resource object type for [Tests\Unit\Exceptions\MyTestClass].');

throw ResourceIdentificationException::attemptingToDetermineTypeFor(new MyTestClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Unit;
namespace Tests\Unit\Exceptions;

use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Unit;
namespace Tests\Unit\Support;

use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Unit;
namespace Tests\Unit\Support;

use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
Expand Down

0 comments on commit 7fb050c

Please sign in to comment.