Skip to content

Commit

Permalink
add api test
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Apr 8, 2024
1 parent e28ffd7 commit 308040d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 10 additions & 9 deletions packages/rekapager-api-platform/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
]);
Expand All @@ -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,
// ]);
};
100 changes: 100 additions & 0 deletions tests/src/IntegrationTests/ApiPlatform/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/rekapager package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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);

Check failure on line 50 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedArgument

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:50:45: MixedArgument: Argument 2 of ApiPlatform\Symfony\Bundle\Test\Client::request cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 50 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedArgument

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:50:45: MixedArgument: Argument 2 of ApiPlatform\Symfony\Bundle\Test\Client::request cannot be mixed, expecting string (see https://psalm.dev/030)
$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;

Check failure on line 62 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:62:9: MixedAssignment: Unable to determine the type that $nextPage is being assigned to (see https://psalm.dev/032)

Check failure on line 62 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:62:9: MixedAssignment: Unable to determine the type that $nextPage is being assigned to (see https://psalm.dev/032)
self::assertNotNull($nextPage);

$previousPage = $response->toArray()['hydra:view']['hydra:previous'] ?? null;

Check failure on line 65 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:65:9: MixedAssignment: Unable to determine the type that $previousPage is being assigned to (see https://psalm.dev/032)

Check failure on line 65 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:65:9: MixedAssignment: Unable to determine the type that $previousPage is being assigned to (see https://psalm.dev/032)
self::assertNotNull($previousPage);

$firstPage = $response->toArray()['hydra:view']['hydra:first'] ?? null;

Check failure on line 68 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:68:9: MixedAssignment: Unable to determine the type that $firstPage is being assigned to (see https://psalm.dev/032)

Check failure on line 68 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:68:9: MixedAssignment: Unable to determine the type that $firstPage is being assigned to (see https://psalm.dev/032)
self::assertNotNull($firstPage);

$lastPage = $response->toArray()['hydra:view']['hydra:last'] ?? null;

Check failure on line 71 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:71:9: MixedAssignment: Unable to determine the type that $lastPage is being assigned to (see https://psalm.dev/032)

Check failure on line 71 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:71:9: MixedAssignment: Unable to determine the type that $lastPage is being assigned to (see https://psalm.dev/032)
self::assertNotNull($lastPage);

// test last page

$response = $client->request('GET', $lastPage);

Check failure on line 76 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedArgument

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:76:45: MixedArgument: Argument 2 of ApiPlatform\Symfony\Bundle\Test\Client::request cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 76 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedArgument

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:76:45: MixedArgument: Argument 2 of ApiPlatform\Symfony\Bundle\Test\Client::request cannot be mixed, expecting string (see https://psalm.dev/030)
$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;

Check failure on line 88 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:88:9: MixedAssignment: Unable to determine the type that $nextPage is being assigned to (see https://psalm.dev/032)

Check failure on line 88 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:88:9: MixedAssignment: Unable to determine the type that $nextPage is being assigned to (see https://psalm.dev/032)
self::assertNull($nextPage);

$previousPage = $response->toArray()['hydra:view']['hydra:previous'] ?? null;

Check failure on line 91 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:91:9: MixedAssignment: Unable to determine the type that $previousPage is being assigned to (see https://psalm.dev/032)

Check failure on line 91 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:91:9: MixedAssignment: Unable to determine the type that $previousPage is being assigned to (see https://psalm.dev/032)
self::assertNotNull($previousPage);

$firstPage = $response->toArray()['hydra:view']['hydra:first'] ?? null;

Check failure on line 94 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:94:9: MixedAssignment: Unable to determine the type that $firstPage is being assigned to (see https://psalm.dev/032)

Check failure on line 94 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:94:9: MixedAssignment: Unable to determine the type that $firstPage is being assigned to (see https://psalm.dev/032)
self::assertNotNull($firstPage);

$lastPage = $response->toArray()['hydra:view']['hydra:last'] ?? null;

Check failure on line 97 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.2, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:97:9: MixedAssignment: Unable to determine the type that $lastPage is being assigned to (see https://psalm.dev/032)

Check failure on line 97 in tests/src/IntegrationTests/ApiPlatform/ApiTest.php

View workflow job for this annotation

GitHub Actions / Symfony 7.*, highest deps, PHP 8.3, ubuntu-latest

MixedAssignment

tests/src/IntegrationTests/ApiPlatform/ApiTest.php:97:9: MixedAssignment: Unable to determine the type that $lastPage is being assigned to (see https://psalm.dev/032)
self::assertNull($lastPage);
}
}

0 comments on commit 308040d

Please sign in to comment.