Fix memory bug in get all on page test

This commit is contained in:
cmorenov1402
2025-03-31 08:55:32 +02:00
parent 670077f8fc
commit ed3c5ff0ea
2 changed files with 38 additions and 24 deletions
+13 -9
View File
@@ -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:
+25 -15
View File
@@ -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();