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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$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]);
}
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]);
}
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]);
}
/**
* 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 [];
}
}