From ed3c5ff0ea2efaacc677878cc77c4797a7cc1771 Mon Sep 17 00:00:00 2001 From: cmorenov1402 Date: Mon, 31 Mar 2025 08:55:32 +0200 Subject: [PATCH] Fix memory bug in get all on page test --- config/packages/api_platform.yaml | 22 ++++++++------ tests/Entity/ProjectApi/GetAllTest.php | 40 ++++++++++++++++---------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/config/packages/api_platform.yaml b/config/packages/api_platform.yaml index 1a4e0f3..93a3cf4 100644 --- a/config/packages/api_platform.yaml +++ b/config/packages/api_platform.yaml @@ -3,7 +3,7 @@ api_platform: version: 4.0.0 show_webby: false collection: - order: 'DESC' + order: "DESC" openapi: contact: name: Fundación Platoniq @@ -16,20 +16,24 @@ api_platform: swagger_ui_extra_configuration: filter: true formats: - json: ['application/json'] - jsonld: ['application/ld+json'] - csv: ['text/csv'] + json: ["application/json"] + jsonld: ["application/ld+json"] + csv: ["text/csv"] docs_formats: - html: ['text/html'] - json: ['application/json'] - jsonld: ['application/ld+json'] - jsonopenapi: ['application/vnd.openapi+json'] + html: ["text/html"] + json: ["application/json"] + jsonld: ["application/ld+json"] + jsonopenapi: ["application/vnd.openapi+json"] defaults: stateless: true cache_headers: - vary: ['Content-Type', 'Authorization', 'Origin'] + vary: ["Content-Type", "Authorization", "Origin"] extra_properties: standard_put: true + pagination_enabled: true + pagination_items_per_page: 30 + pagination_client_enabled: true + pagination_client_items_per_page: true use_symfony_listeners: true swagger: api_keys: diff --git a/tests/Entity/ProjectApi/GetAllTest.php b/tests/Entity/ProjectApi/GetAllTest.php index 6283cf0..d4a593c 100644 --- a/tests/Entity/ProjectApi/GetAllTest.php +++ b/tests/Entity/ProjectApi/GetAllTest.php @@ -5,6 +5,7 @@ namespace App\Tests\Entity\ProjectApi; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Symfony\Bundle\Test\Client; use App\Entity\Project\ProjectTerritory; +use App\Entity\User\User; use App\Factory\Project\ProjectFactory; use App\Factory\User\UserFactory; use Doctrine\ORM\EntityManagerInterface; @@ -34,6 +35,16 @@ class GetAllTest extends ApiTestCase // Auxiliary functions + private function createTestUser() + { + return UserFactory::createOne([ + 'email' => self::USER_EMAIL, + 'password' => self::USER_PASSWORD, + ]); + } + + private function createTestProject(int $count, User $owner) {} + private function getHeaders(Client $client) { // Responsability 1: Get Token @@ -58,10 +69,7 @@ class GetAllTest extends ApiTestCase public function testGetAllSuccessful(): void { - $owner = UserFactory::createOne([ - 'email' => self::USER_EMAIL, - 'password' => self::USER_PASSWORD, - ]); + $owner = $this->createTestUser(); $numberOfProjects = 2; ProjectFactory::createMany($numberOfProjects, ['owner' => $owner]); @@ -74,37 +82,39 @@ class GetAllTest extends ApiTestCase $this->assertCount($numberOfProjects, $responseData['member']); } - private function getMinNumInPage($page = 1) + private function getMinNumInPage($page = 1, $pageSize = self::PAGE_SIZE) { - return self::PAGE_SIZE * ($page - 1); + return $pageSize * ($page - 1); } public function testGetAllOnPage(): void { - $owner = UserFactory::createOne([ - 'email' => self::USER_EMAIL, - 'password' => self::USER_PASSWORD, - ]); - + $owner = $this->createTestUser(); $territory = new ProjectTerritory('ES'); $page = 2; $numberOfProjectsInPage = 1; - $numberOfProjectsTotal = $this->getMinNumInPage($page) + $numberOfProjectsInPage; + $itemsPerPage = 5; + $minCountInPage = $this->getMinNumInPage($page, $itemsPerPage); + $numberOfProjectsTotal = $minCountInPage + $numberOfProjectsInPage; - // It passes an owner and a territory already created - // because memory management is critical here + // It passes an attributes already created because memory management is critical here ProjectFactory::createMany($numberOfProjectsTotal, [ 'owner' => $owner, 'territory' => $territory, ]); $client = static::createClient(); - $client->request(self::METHOD, self::BASE_URI."?page=$page", $this->getHeaders($client)); + $client->request( + self::METHOD, + self::BASE_URI."?page=$page&itemsPerPage=$itemsPerPage", + $this->getHeaders($client) + ); $this->assertResponseIsSuccessful(); $responseData = json_decode($client->getResponse()->getContent(), true); + $this->assertSame($numberOfProjectsTotal, $responseData['totalItems']); $this->assertCount(max($numberOfProjectsInPage, 0), $responseData['member']); $this->entityManager->clear();