Skip to content

Commit 50c698e

Browse files
committed
feat: add new function in Controller & make test
1 parent 1301b59 commit 50c698e

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

src/app/Packages/Features/Controller/WorldHeritageController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Packages\Features\Controller;
44

55
use App\Http\Controllers\Controller;
6+
use App\Packages\Features\QueryUseCases\UseCase\GetCountEachRegionUseCase;
67
use App\Packages\Features\QueryUseCases\UseCase\GetWorldHeritageByIdUseCase;
78
use App\Packages\Features\QueryUseCases\UseCase\SearchWorldHeritagesWithAlgoliaUseCase;
89
use App\Packages\Features\QueryUseCases\ViewModel\WorldHeritageViewModel;
@@ -79,4 +80,17 @@ public function searchWorldHeritages(
7980
'data' => $dto->toArray(),
8081
], 200);
8182
}
83+
84+
public function getWorldHeritagesCountByRegion(
85+
Request $request,
86+
GetCountEachRegionUseCase $useCase
87+
): JsonResponse
88+
{
89+
$dto = $useCase->handle();
90+
91+
return response()->json([
92+
'status' => 'success',
93+
'data' => array_map(fn ($item) => $item->toArray(), $dto),
94+
], 200);
95+
}
8296
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace App\Packages\Features\Tests;
4+
5+
use App\Models\Country;
6+
use App\Models\Image;
7+
use App\Models\WorldHeritage;
8+
use Database\Seeders\DatabaseSeeder;
9+
use Illuminate\Support\Facades\DB;
10+
use Tests\TestCase;
11+
12+
class GetCountEachRegionTest extends TestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
$this->refresh();
18+
19+
$seeder = new DatabaseSeeder();
20+
$seeder->run();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
$this->refresh();
26+
parent::tearDown();
27+
}
28+
29+
private function refresh(): void
30+
{
31+
if (env('APP_ENV') === 'testing') {
32+
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;');
33+
WorldHeritage::truncate();
34+
Country::truncate();
35+
DB::table('site_state_parties')->truncate();
36+
Image::truncate();
37+
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;');
38+
}
39+
}
40+
41+
public function test_check_count_each_region_value(): void
42+
{
43+
$response = $this->getJson('/api/v1/heritages/region-count');
44+
45+
$response->assertStatus(200)
46+
->assertJsonStructure([
47+
'data' => [
48+
'*' => [
49+
'region',
50+
'count',
51+
],
52+
],
53+
]);
54+
}
55+
56+
public function test_returns_all_six_regions(): void
57+
{
58+
$response = $this->getJson('/api/v1/heritages/region-count');
59+
60+
$data = $response->json('data');
61+
$regions = array_column($data, 'region');
62+
63+
64+
$this->assertCount(6, $data);
65+
$this->assertContains('Africa', $regions);
66+
$this->assertContains('Asia', $regions);
67+
$this->assertContains('Europe', $regions);
68+
$this->assertContains('North America', $regions);
69+
$this->assertContains('South America', $regions);
70+
$this->assertContains('Oceania', $regions);
71+
}
72+
73+
public function test_count_is_positive_integer(): void
74+
{
75+
$response = $this->getJson('/api/v1/heritages/region-count');
76+
77+
foreach ($response->json('data') as $item) {
78+
$this->assertIsInt($item['count']);
79+
$this->assertGreaterThanOrEqual(0, $item['count']);
80+
}
81+
}
82+
83+
public function test_unknown_region_is_not_included(): void
84+
{
85+
$response = $this->getJson('/api/v1/heritages/region-count');
86+
87+
$regions = array_column($response->json('data'), 'region');
88+
$this->assertNotContains('Unknown', $regions);
89+
}
90+
}

0 commit comments

Comments
 (0)