"test/git@forgeb1.univ-lehavre.fr:khraimes/cours.git" n'existait pas sur "531d1e8791ea6b19834426bca603a3784ba71708"
Newer
Older
<?php
namespace App\Tests\Api;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use Doctrine\DBAL\Connection;
class StatsEndpointsTest extends ApiTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::bootKernel();
/** @var Connection $connection */
$connection = static::getContainer()->get(Connection::class);
$connection->executeStatement('DELETE FROM tax_data');
// Jeu de donnees minimal pour eviter la dependance aux fixtures lourdes.
$rows = [
[2019, '11', 'ILE-DE-FRANCE', '75', '75056', 12.5, 20.0, 11.0, 3.2, 100000, 200000, 300000, 400000, 1000000],
[2020, '11', 'ILE-DE-FRANCE', '75', '75056', 12.7, 20.4, 11.1, 3.4, 110000, 210000, 310000, 410000, 1040000],
[2022, '32', 'LES-HAUTS-DE-FRANCE', '59', '59350', 22.1, 31.5, 17.3, 7.8, 120000, 220000, 320000, 420000, 1080000],
];
foreach ($rows as $row) {
$connection->executeStatement(
'INSERT INTO tax_data (year, region_code, region_name, department_code, commune_code, rate_tfpnb, rate_tfpb, rate_th, rate_cfe, volume_tfpnb, volume_tfpb, volume_th, volume_cfe, collected_volume)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
$row
);
}
}
public function testRegionEvolutionEndpointReturnsData(): void
{
$response = static::createClient()->request('GET', '/api/stats/region-evolution?tax=tfpb&startYear=2019&endYear=2022');
$this->assertResponseIsSuccessful();
$data = $response->toArray(false);
$members = $this->extractMembers($data);
$this->assertNotEmpty($members, 'Le endpoint region-evolution doit retourner des données.');
$this->assertArrayHasKey('year', $members[0]);
$this->assertArrayHasKey('regionCode', $members[0]);
$this->assertArrayHasKey('regionName', $members[0]);
$this->assertArrayHasKey('rate', $members[0]);
$this->assertSame('11', $members[0]['regionCode']);
$this->assertSame('ILE-DE-FRANCE', $members[0]['regionName']);
}
public function testDepartmentCorrelationEndpointReturnsData(): void
{
$response = static::createClient()->request('GET', '/api/stats/department-correlation?tax=tfpb&year=2022&department=59');
$this->assertResponseIsSuccessful();
$data = $response->toArray(false);
$members = $this->extractMembers($data);
$this->assertNotEmpty($members, 'Le endpoint department-correlation doit retourner des données.');
$this->assertArrayHasKey('communeCode', $members[0]);
$this->assertArrayHasKey('rate', $members[0]);
$this->assertArrayHasKey('collectedVolume', $members[0]);
$this->assertSame('59350', $members[0]['communeCode']);
$this->assertEquals(220000.0, (float) $members[0]['collectedVolume']);
}
public function testRegionalDistributionEndpointReturnsData(): void
{
$response = static::createClient()->request('GET', '/api/stats/regional-distribution?year=2022');
$this->assertResponseIsSuccessful();
$data = $response->toArray(false);
$members = $this->extractMembers($data);
$this->assertNotEmpty($members, 'Le endpoint regional-distribution doit retourner des données.');
$this->assertArrayHasKey('regionCode', $members[0]);
$this->assertArrayHasKey('regionName', $members[0]);
$this->assertArrayHasKey('collectedVolume', $members[0]);
$this->assertSame('32', $members[0]['regionCode']);
$this->assertEquals(220000.0, (float) $members[0]['collectedVolume']);
}
public function testDepartmentCorrelationReturnsEmptyForUnknownDepartment(): void
{
$response = static::createClient()->request('GET', '/api/stats/department-correlation?tax=tfpb&year=2022&department=00');
$this->assertResponseIsSuccessful();
$data = $response->toArray(false);
$members = $this->extractMembers($data);
$this->assertSame([], $members);
}
public function testRegionEvolutionFallsBackWhenTaxIsInvalid(): void
{
$response = static::createClient()->request('GET', '/api/stats/region-evolution?tax=invalid&startYear=2019&endYear=2022');
$this->assertResponseIsSuccessful();
$data = $response->toArray(false);
$members = $this->extractMembers($data);
$this->assertNotEmpty($members);
$this->assertSame('11', $members[0]['regionCode']);
}
/**
* API Platform peut renvoyer "member" ou "hydra:member" selon la config.
*
* @param array<string, mixed> $data
* @return array<int, array<string, mixed>>
*/
private function extractMembers(array $data): array
{
if (isset($data['member']) && is_array($data['member'])) {
return $data['member'];
}
if (isset($data['hydra:member']) && is_array($data['hydra:member'])) {
return $data['hydra:member'];
}
return [];
}
}