From bdcf335802a59f9ee47625c7d114e9c54cefd0cf Mon Sep 17 00:00:00 2001 From: dj222837 Date: Thu, 12 Feb 2026 14:50:19 +0100 Subject: [PATCH] =?UTF-8?q?[TEST]=20-=20r=C3=A9alisation=20des=20tests=20u?= =?UTF-8?q?nitaires?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/Models/Taxe.php | 4 +- api/config/api-platform.php | 1 + api/database/factories/DepartmentFactory.php | 20 +++ api/database/factories/TaxeFactory.php | 30 ++++ api/tests/Feature/Api/EndpointsTest.php | 157 ++++++++++++++++++ api/tests/Feature/Api/StatisticsApiTest.php | 32 ++++ api/tests/Feature/Api/TaxeApiTest.php | 32 ++++ api/tests/Feature/ExampleTest.php | 19 --- api/tests/Unit/ExampleTest.php | 16 -- .../Unit/Services/TaxeStatServiceTest.php | 49 ++++++ 10 files changed, 323 insertions(+), 37 deletions(-) create mode 100644 api/database/factories/DepartmentFactory.php create mode 100644 api/database/factories/TaxeFactory.php create mode 100644 api/tests/Feature/Api/EndpointsTest.php create mode 100644 api/tests/Feature/Api/StatisticsApiTest.php create mode 100644 api/tests/Feature/Api/TaxeApiTest.php delete mode 100644 api/tests/Feature/ExampleTest.php delete mode 100644 api/tests/Unit/ExampleTest.php create mode 100644 api/tests/Unit/Services/TaxeStatServiceTest.php diff --git a/api/app/Models/Taxe.php b/api/app/Models/Taxe.php index 61177d1..2b9953f 100644 --- a/api/app/Models/Taxe.php +++ b/api/app/Models/Taxe.php @@ -39,8 +39,8 @@ use ApiPlatform\Laravel\Eloquent\Filter\EqualsFilter; ), ] )] -#[QueryParameter(key: 'commune_code', description: 'Filter by commune INSEE code (e.g. 76540)', filter: EqualsFilter::class)] -#[QueryParameter(key: 'commune_name', description: 'Filter by commune name', filter: EqualsFilter::class)] +#[QueryParameter(key: 'commune_code', description: 'Filter by commune INSEE code (e.g. 76540)', filter: EqualsFilter::class, property: 'commune_code')] +#[QueryParameter(key: 'commune_name', description: 'Filter by commune name', filter: EqualsFilter::class, property: 'commune_name')] #[QueryParameter(key: 'department_id', description: 'Filter by department ID (integer)', filter: EqualsFilter::class, property: 'department_id')] #[QueryParameter(key: 'year', description: 'Filter by year (2019-2022)', filter: EqualsFilter::class)] #[QueryParameter(key: 'sort[:property]', description: 'Sort by property (e.g. sort[year]=asc)', filter: EqualsFilter::class)] diff --git a/api/config/api-platform.php b/api/config/api-platform.php index cfee337..0ff3043 100644 --- a/api/config/api-platform.php +++ b/api/config/api-platform.php @@ -35,6 +35,7 @@ return [ 'formats' => [ 'jsonld' => ['application/ld+json'], + 'json' => ['application/json'], // 'jsonapi' => ['application/vnd.api+json'], // 'csv' => ['text/csv'], ], diff --git a/api/database/factories/DepartmentFactory.php b/api/database/factories/DepartmentFactory.php new file mode 100644 index 0000000..26dea83 --- /dev/null +++ b/api/database/factories/DepartmentFactory.php @@ -0,0 +1,20 @@ + $this->faker->unique()->numerify('##'), + 'department_name' => $this->faker->state(), + 'region_name' => $this->faker->state(), + ]; + } +} \ No newline at end of file diff --git a/api/database/factories/TaxeFactory.php b/api/database/factories/TaxeFactory.php new file mode 100644 index 0000000..647ffb6 --- /dev/null +++ b/api/database/factories/TaxeFactory.php @@ -0,0 +1,30 @@ + $this->faker->postcode(), + 'commune_name' => $this->faker->city(), + 'department_id' => Department::factory(), + 'tfpnb_amount' => $this->faker->randomFloat(2, 100, 5000), + 'tfpnb_percentage' => $this->faker->randomFloat(2, 1, 5), + 'tfpb_amount' => $this->faker->randomFloat(2, 100, 5000), + 'tfpb_percentage' => $this->faker->randomFloat(2, 1, 5), + 'th_amount' => $this->faker->randomFloat(2, 100, 5000), + 'th_percentage' => $this->faker->randomFloat(2, 1, 5), + 'cfe_amount' => $this->faker->randomFloat(2, 100, 5000), + 'cfe_percentage' => $this->faker->randomFloat(2, 1, 5), + 'year' => $this->faker->numberBetween(2019, 2022), + ]; + } +} \ No newline at end of file diff --git a/api/tests/Feature/Api/EndpointsTest.php b/api/tests/Feature/Api/EndpointsTest.php new file mode 100644 index 0000000..ec33d2c --- /dev/null +++ b/api/tests/Feature/Api/EndpointsTest.php @@ -0,0 +1,157 @@ +create([ + 'department_code' => '76', + 'department_name' => 'Seine-Maritime', + 'region_name' => 'Normandie' + ]); + + Taxe::factory()->create([ + 'department_id' => $dept->id, + 'commune_code' => '76001', + 'commune_name' => 'Le Havre', + 'year' => 2022, + 'tfpnb_amount' => 1000, + 'tfpb_amount' => 2000, + 'th_amount' => 3000, + 'cfe_amount' => 4000, + 'tfpnb_percentage' => 10.5, + 'tfpb_percentage' => 10.5, + 'th_percentage' => 10.5, + 'cfe_percentage' => 10.5, + ]); + } + + private function apiHeaders(): array + { + return ['Accept' => 'application/ld+json']; + } + + public function test_api_taxes_collection(): void + { + $response = $this->get('/api/taxes', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'member', + 'totalItems', + ]); + } + + public function test_api_departments_collection(): void + { + $response = $this->get('/api/departments', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'member', + 'totalItems' + ]); + } + + public function test_api_regions_collection(): void + { + $response = $this->get('/api/regions', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'member' + ]); + } + + public function test_stats_global_endpoint(): void + { + $response = $this->get('/api/stats/global?group_by=department&year=2022', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'groupBy', + 'year', + 'data' + ]); + } + + public function test_regions_distribution_endpoint(): void + { + $response = $this->get('/api/regions/distribution?tax_type=tfpnb&year=2022', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'taxType', + 'year', + 'data' => [ + '*' => ['region', 'total_amount'] + ] + ]); + } + + public function test_tax_timeseries_endpoint(): void + { + $response = $this->get('/api/taxes/timeseries?tax_type=tfpb', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'taxType', + 'regions', + 'data' => [ + 'Normandie' => [ + '*' => ['year', 'avg_rate'] + ] + ] + ]); + } + + public function test_commune_correlation_endpoint(): void + { + $response = $this->get('/api/communes/correlation?department_code=76&tax_type=th&year=2022', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'departmentCode', + 'taxType', + 'year', + 'data' => [ + '*' => ['commune_name', 'rate', 'amount'] + ] + ]); + } + + public function test_tax_field_stat_sum_endpoint(): void + { + $response = $this->get('/api/somme/tfpnb?department_code=76', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'field', + 'sum', + 'filters' + ]); + } + + public function test_tax_field_stat_average_endpoint(): void + { + $response = $this->get('/api/average/cfe?year=2022', $this->apiHeaders()); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'field', + 'average', + 'filters' + ]); + } +} \ No newline at end of file diff --git a/api/tests/Feature/Api/StatisticsApiTest.php b/api/tests/Feature/Api/StatisticsApiTest.php new file mode 100644 index 0000000..d62fcbb --- /dev/null +++ b/api/tests/Feature/Api/StatisticsApiTest.php @@ -0,0 +1,32 @@ + '27', 'department_name' => 'Eure', 'region_name' => 'Normandie']); + + Taxe::factory()->create([ + 'department_id' => $dept->id, + 'tfpb_amount' => 500.50, + 'year' => 2020 + ]); + + $response = $this->get('/api/stats/global?group_by=department&year=2020', ['Accept' => 'application/ld+json']); + + $response->assertStatus(200) + ->assertJsonFragment([ + 'groupBy' => 'department', + 'year' => 2020 + ]); + } +} \ No newline at end of file diff --git a/api/tests/Feature/Api/TaxeApiTest.php b/api/tests/Feature/Api/TaxeApiTest.php new file mode 100644 index 0000000..1168abd --- /dev/null +++ b/api/tests/Feature/Api/TaxeApiTest.php @@ -0,0 +1,32 @@ +create(['department_code' => '75', 'department_name' => 'Paris', 'region_name' => 'Île-de-France']); + + Taxe::factory()->count(5)->create([ + 'department_id' => $dept->id, + 'year' => 2021 + ]); + + $response = $this->get('/api/taxes.jsonld?year=2021'); + + $response->assertStatus(200) + ->assertJsonStructure([ + 'member' => [ + '*' => ['communeCode', 'communeName', 'tfpnbAmount', 'year'] + ] + ]); + } +} \ No newline at end of file diff --git a/api/tests/Feature/ExampleTest.php b/api/tests/Feature/ExampleTest.php deleted file mode 100644 index 8364a84..0000000 --- a/api/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,19 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/api/tests/Unit/ExampleTest.php b/api/tests/Unit/ExampleTest.php deleted file mode 100644 index 5773b0c..0000000 --- a/api/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,16 +0,0 @@ -assertTrue(true); - } -} diff --git a/api/tests/Unit/Services/TaxeStatServiceTest.php b/api/tests/Unit/Services/TaxeStatServiceTest.php new file mode 100644 index 0000000..52ff9bf --- /dev/null +++ b/api/tests/Unit/Services/TaxeStatServiceTest.php @@ -0,0 +1,49 @@ +service = new TaxeStatService(); + } + + public function test_calculate_sum_returns_correct_value(): void + { + $dept = Department::create(['department_code' => '76', 'department_name' => 'Seine-Maritime', 'region_name' => 'Normandie']); + + Taxe::factory()->create([ + 'department_id' => $dept->id, + 'tfpnb_amount' => 1000, + 'year' => 2022 + ]); + + Taxe::factory()->create([ + 'department_id' => $dept->id, + 'tfpnb_amount' => 2000, + 'year' => 2022 + ]); + + $sum = $this->service->calculateSum(Taxe::FIELD_TFPNB, null, 2022); + + $this->assertEquals(3000, $sum); + } + + public function test_validate_field_throws_exception_for_invalid_field(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->service->calculateSum('invalid_field'); + } +} \ No newline at end of file -- GitLab