From 308040d10f6f766e3fa2cf7ad02adc440f13949d Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:33:34 +0700 Subject: [PATCH] add api test --- composer.json | 2 + .../config/services.php | 19 ++-- .../IntegrationTests/ApiPlatform/ApiTest.php | 100 ++++++++++++++++++ 3 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 tests/src/IntegrationTests/ApiPlatform/ApiTest.php diff --git a/composer.json b/composer.json index 17cd7fd..9e68eda 100644 --- a/composer.json +++ b/composer.json @@ -65,11 +65,13 @@ "rekalogika/doctrine-collections-decorator": "^2.3", "symfony/asset": "^6.4 || ^7.0", "symfony/asset-mapper": "^6.4 || ^7.0", + "symfony/browser-kit": "^6.4 || ^7.0", "symfony/debug-bundle": "^6.4 || ^7.0", "symfony/doctrine-bridge": "^6.4 || ^7.0", "symfony/dotenv": "^6.4 || ^7.0", "symfony/form": "^6.4 || ^7.0", "symfony/framework-bundle": "^6.4 || ^7.0", + "symfony/http-client": "^6.4 || ^7.0", "symfony/maker-bundle": "^1.55", "symfony/monolog-bundle": "^3.0", "symfony/runtime": "^6.4 || ^7.0", diff --git a/packages/rekapager-api-platform/config/services.php b/packages/rekapager-api-platform/config/services.php index 4e95e36..eab7684 100644 --- a/packages/rekapager-api-platform/config/services.php +++ b/packages/rekapager-api-platform/config/services.php @@ -34,6 +34,7 @@ ->args([ '$resourceMetadataFactory' => service('api_platform.metadata.resource.metadata_collection_factory'), '$pageIdentifierEncoderLocator' => service(PageIdentifierEncoderLocatorInterface::class), + '$pagination' => service('api_platform.pagination'), '$pageParameterName' => '%api_platform.collection.pagination.page_parameter_name%', '$urlGenerationStrategy' => '%api_platform.url_generation_strategy%' ]); @@ -45,13 +46,13 @@ '$collectionNormalizer' => service('.inner'), ]); - $services->set('rekalogika.rekapager.api_platform.orm.extension') - ->class(RekapagerExtension::class) - ->args([ - '$pagerFactory' => service(PagerFactory::class), - '$pagination' => service('api_platform.pagination'), - ]) - ->tag('api_platform.doctrine.orm.query_extension.collection', [ - 'priority' => -48, - ]); + // $services->set('rekalogika.rekapager.api_platform.orm.extension') + // ->class(RekapagerExtension::class) + // ->args([ + // '$pagerFactory' => service(PagerFactory::class), + // '$pagination' => service('api_platform.pagination'), + // ]) + // ->tag('api_platform.doctrine.orm.query_extension.collection', [ + // 'priority' => -48, + // ]); }; diff --git a/tests/src/IntegrationTests/ApiPlatform/ApiTest.php b/tests/src/IntegrationTests/ApiPlatform/ApiTest.php new file mode 100644 index 0000000..1549534 --- /dev/null +++ b/tests/src/IntegrationTests/ApiPlatform/ApiTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Rekapager\Tests\IntegrationTests\ApiPlatform; + +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; + +class ApiTest extends ApiTestCase +{ + public function testApiWithCustomProvider(): void + { + $client = static::createClient(); + + $response = $client->request('GET', '/api/custom/posts'); + $this->assertResponseIsSuccessful(); + $this->assertJsonContains([ + '@context' => '/api/contexts/Post', + '@id' => '/api/custom/posts', + '@type' => 'hydra:Collection', + 'hydra:view' => [ + '@id' => '/api/custom/posts', + '@type' => 'hydra:PartialCollectionView', + ], + ]); + + $nextPage = $response->toArray()['hydra:view']['hydra:next'] ?? null; + self::assertNotNull($nextPage); + + $previousPage = $response->toArray()['hydra:view']['hydra:previous'] ?? null; + self::assertNull($previousPage); + + $firstPage = $response->toArray()['hydra:view']['hydra:first'] ?? null; + self::assertNull($firstPage); + + $lastPage = $response->toArray()['hydra:view']['hydra:last'] ?? null; + self::assertNotNull($lastPage); + + // test next page + + $response = $client->request('GET', $nextPage); + $this->assertResponseIsSuccessful(); + $this->assertJsonContains([ + '@context' => '/api/contexts/Post', + '@id' => '/api/custom/posts', + '@type' => 'hydra:Collection', + 'hydra:view' => [ + '@id' => $nextPage, + '@type' => 'hydra:PartialCollectionView', + ], + ]); + + $nextPage = $response->toArray()['hydra:view']['hydra:next'] ?? null; + self::assertNotNull($nextPage); + + $previousPage = $response->toArray()['hydra:view']['hydra:previous'] ?? null; + self::assertNotNull($previousPage); + + $firstPage = $response->toArray()['hydra:view']['hydra:first'] ?? null; + self::assertNotNull($firstPage); + + $lastPage = $response->toArray()['hydra:view']['hydra:last'] ?? null; + self::assertNotNull($lastPage); + + // test last page + + $response = $client->request('GET', $lastPage); + $this->assertResponseIsSuccessful(); + $this->assertJsonContains([ + '@context' => '/api/contexts/Post', + '@id' => '/api/custom/posts', + '@type' => 'hydra:Collection', + 'hydra:view' => [ + '@id' => $lastPage, + '@type' => 'hydra:PartialCollectionView', + ], + ]); + + $nextPage = $response->toArray()['hydra:view']['hydra:next'] ?? null; + self::assertNull($nextPage); + + $previousPage = $response->toArray()['hydra:view']['hydra:previous'] ?? null; + self::assertNotNull($previousPage); + + $firstPage = $response->toArray()['hydra:view']['hydra:first'] ?? null; + self::assertNotNull($firstPage); + + $lastPage = $response->toArray()['hydra:view']['hydra:last'] ?? null; + self::assertNull($lastPage); + } +}