diff --git a/tests/Feature/FeatureTest.php b/tests/Feature/FeatureTest.php index fc937cf..572edb1 100644 --- a/tests/Feature/FeatureTest.php +++ b/tests/Feature/FeatureTest.php @@ -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([ diff --git a/tests/Feature/ResourceIdentificationTest.php b/tests/Feature/ResourceIdentificationTest.php index 6ddf7e6..3369ef6 100644 --- a/tests/Feature/ResourceIdentificationTest.php +++ b/tests/Feature/ResourceIdentificationTest.php @@ -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 @@ -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 @@ -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 diff --git a/tests/Performance/FieldsTest.php b/tests/Performance/FieldsTest.php deleted file mode 100644 index 3ebcd7e..0000000 --- a/tests/Performance/FieldsTest.php +++ /dev/null @@ -1,43 +0,0 @@ -map(function ($type) { - return "fields[{$type}]=a,b,c,d,e,f"; -})->implode('&'); - -$request = Request::create("https://example.com/users?{$query}", 'GET'); - -$resources = []; -for ($i = 0; $i < $numberOfResourcesReturned; $i++) { - $resources[] = $types[$i % count($types)]; -} - -$start = microtime(true); -foreach ($resources as $resource) { - Fields::getInstance()->parse($request, $resource, true); -} -$end = microtime(true); - -echo 'Duration (milliseconds):'.PHP_EOL; -echo($end - $start) * 1000; diff --git a/tests/Performance/IncludesTest.php b/tests/Performance/IncludesTest.php deleted file mode 100644 index 8a81238..0000000 --- a/tests/Performance/IncludesTest.php +++ /dev/null @@ -1,45 +0,0 @@ -parse($request, $prefix); -} -$end = microtime(true); - -echo 'Duration (milliseconds):'.PHP_EOL; -echo($end - $start) * 1000; diff --git a/tests/Performance/RequestRelationshipsTest.php b/tests/Performance/RequestRelationshipsTest.php deleted file mode 100644 index 2b5cc49..0000000 --- a/tests/Performance/RequestRelationshipsTest.php +++ /dev/null @@ -1,65 +0,0 @@ - new BasicModel(['id' => Str::random()]); -Container::getInstance()->bind(ResponseFactory::class, fn () => new class () { - public function json(array $data): object - { - $data = json_encode($data); - - return new class ($data) { - public string|bool $data; - - /** - * @param string|bool $data - */ - public function __construct($data) - { - $this->data = $data; - } - - public function header(): void - { - // - } - }; - } -}); - -$users = []; -for ($i = 0; $i < $numberOfResourcesReturned; $i++) { - $users[] = $modelFactory() - ->setRelation('posts', [ - $modelFactory()->setRelation('author', $modelFactory())->setRelation('avatar', $modelFactory()), - $modelFactory()->setRelation('author', $modelFactory())->setRelation('avatar', $modelFactory()), - $modelFactory()->setRelation('author', $modelFactory())->setRelation('avatar', $modelFactory()), - ]) - ->setRelation('comments', [ - $modelFactory()->setRelation('author', $modelFactory())->setRelation('avatar', $modelFactory()), - $modelFactory()->setRelation('author', $modelFactory())->setRelation('avatar', $modelFactory()), - $modelFactory()->setRelation('author', $modelFactory())->setRelation('avatar', $modelFactory()), - ]); -} -$request = Request::create('https://example.com/users?include=posts.author.avatar,comments.author.avatar', 'GET'); -Container::getInstance()->bind('request', fn () => $request); -$resource = UserResource::collection($users); - -$start = microtime(true); -$resource->toResponse($request); -$end = microtime(true); - -echo 'Duration (milliseconds):'.PHP_EOL; -echo($end - $start) * 1000; diff --git a/tests/TestCase.php b/tests/TestCase.php index ace7e43..e86cc11 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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) { @@ -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()); @@ -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'; diff --git a/tests/Unit/ResourceIdentificationExceptionTest.php b/tests/Unit/Exceptions/ResourceIdentificationExceptionTest.php similarity index 90% rename from tests/Unit/ResourceIdentificationExceptionTest.php rename to tests/Unit/Exceptions/ResourceIdentificationExceptionTest.php index 79d07ec..a3992e0 100644 --- a/tests/Unit/ResourceIdentificationExceptionTest.php +++ b/tests/Unit/Exceptions/ResourceIdentificationExceptionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit; +namespace Tests\Unit\Exceptions; use PHPUnit\Framework\TestCase; use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException; @@ -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()); } @@ -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()); } diff --git a/tests/Unit/UnknownRelationshipExceptionTest.php b/tests/Unit/Exceptions/UnknownRelationshipExceptionTest.php similarity index 97% rename from tests/Unit/UnknownRelationshipExceptionTest.php rename to tests/Unit/Exceptions/UnknownRelationshipExceptionTest.php index b1d3cad..82d62ab 100644 --- a/tests/Unit/UnknownRelationshipExceptionTest.php +++ b/tests/Unit/Exceptions/UnknownRelationshipExceptionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit; +namespace Tests\Unit\Exceptions; use PHPUnit\Framework\TestCase; use stdClass; diff --git a/tests/Unit/FieldsTest.php b/tests/Unit/Support/FieldsTest.php similarity index 99% rename from tests/Unit/FieldsTest.php rename to tests/Unit/Support/FieldsTest.php index 4f13dcf..2de997b 100644 --- a/tests/Unit/FieldsTest.php +++ b/tests/Unit/Support/FieldsTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit; +namespace Tests\Unit\Support; use Illuminate\Foundation\Application; use Illuminate\Http\Request; diff --git a/tests/Unit/IncludesTest.php b/tests/Unit/Support/IncludesTest.php similarity index 98% rename from tests/Unit/IncludesTest.php rename to tests/Unit/Support/IncludesTest.php index b740ebf..3a98eee 100644 --- a/tests/Unit/IncludesTest.php +++ b/tests/Unit/Support/IncludesTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit; +namespace Tests\Unit\Support; use Illuminate\Foundation\Application; use Illuminate\Http\Request;