. */ declare(strict_types=1); namespace App\Tests\API\Endpoints; use App\Tests\API\AuthenticatedApiTestCase; use Symfony\Contracts\HttpClient\ResponseInterface; abstract class CrudEndpointTestCase extends AuthenticatedApiTestCase { /** * Returns the base path of the endpoint. * @return string */ abstract protected function getBasePath(): string; protected function getItemPath(int $id): string { $basePath = $this->getBasePath(); if (!str_ends_with($basePath, '/')) { $basePath .= '/'; } return $basePath . $id; } protected function _testGetCollection(): ResponseInterface { $response = self::createAuthenticatedClient()->request('GET', $this->getBasePath()); self::assertResponseIsSuccessful(); self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); return $response; } protected function _testGetChildrenCollection(int $id): ResponseInterface { $response = self::createAuthenticatedClient()->request('GET', $this->getItemPath($id) . '/children'); self::assertResponseIsSuccessful(); self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); return $response; } protected function _testGetItem(int $id): ResponseInterface { $response = self::createAuthenticatedClient()->request('GET', $this->getItemPath($id)); self::assertResponseIsSuccessful(); self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); return $response; } protected function _testPostItem(array $data): ResponseInterface { $response = self::createAuthenticatedClient()->request('POST', $this->getBasePath(), ['json' => $data]); self::assertResponseIsSuccessful(); self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); return $response; } protected function _testPatchItem(int $id, array $data): ResponseInterface { $response = self::createAuthenticatedClient()->request('PATCH', $this->getItemPath($id), [ 'json' => $data, 'headers' => ['Content-Type' => 'application/merge-patch+json'] ]); self::assertResponseIsSuccessful(); self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); return $response; } protected function _testDeleteItem(int $id): ResponseInterface { $response = self::createAuthenticatedClient()->request('DELETE', $this->getItemPath($id)); self::assertResponseIsSuccessful(); return $response; } }