Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<?php

namespace App\Packages\Domains\Test\QueryService;

use App\Models\Image;
use App\Packages\Domains\Ports\Dto\HeritageSearchResult;
use App\Packages\Domains\Ports\WorldHeritageSearchPort;
use Tests\TestCase;
use App\Packages\Domains\WorldHeritageQueryService;
use App\Enums\StudyRegion;
use Illuminate\Support\Facades\DB;
use App\Models\WorldHeritage;
use App\Models\Country;

class WorldHeritageQueryService_countEachRegionTest extends TestCase
{
private $queryService;

protected function setUp(): void
{
parent::setUp();
$this->refresh();

$this->app->bind(WorldHeritageSearchPort::class, function () {
return new class implements WorldHeritageSearchPort {
public function search($query, int $currentPage, int $perPage): HeritageSearchResult {
return new HeritageSearchResult(ids: [], total: 0, currentPage: 1, perPage: $perPage, lastPage: 0);
}
};
});

$this->queryService = app(WorldHeritageQueryService::class);
}

protected function tearDown(): void
{
$this->refresh();
parent::tearDown();
}

private function refresh(): void
{
if (env('APP_ENV') === 'testing') {
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;');
WorldHeritage::truncate();
Country::truncate();
DB::table('site_state_parties')->truncate();
Image::truncate();
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;');
}
}

private function baseRecord(array $override = []): array
{
$now = now();
return array_merge([
'official_name' => 'Test Site',
'name' => 'Test Site',
'name_jp' => 'テスト',
'country' => 'Test Country',
'region' => 'Test Region',
'study_region' => StudyRegion::UNKNOWN->value,
'category' => 'Cultural',
'criteria' => json_encode(['i']),
'year_inscribed' => 2000,
'area_hectares' => null,
'buffer_zone_hectares' => null,
'is_endangered' => false,
'latitude' => null,
'longitude' => null,
'short_description' => '',
'image_url' => null,
'unesco_site_url' => null,
'created_at' => $now,
'updated_at' => $now,
], $override);
}

public function test_count_each_region(): void
{
DB::table('world_heritage_sites')->insert([
// Africa × 3
// id:9 Simien National Park (Ethiopia, AFR)
$this->baseRecord([
'id' => 9,
'official_name'=> 'Simien National Park',
'name' => 'Simien National Park',
'study_region' => StudyRegion::AFRICA->value,
'category' => 'Natural',
'latitude' => 13.1833333333,
'longitude' => 38.0666666667,
]),
// id:25 Djoudj National Bird Sanctuary (Senegal, AFR)
$this->baseRecord([
'id' => 25,
'official_name'=> 'Djoudj National Bird Sanctuary',
'name' => 'Djoudj National Bird Sanctuary',
'study_region' => StudyRegion::AFRICA->value,
'category' => 'Natural',
'latitude' => 16.414602,
'longitude' => -16.237906,
]),
// id:26 Island of Gorée (Senegal, AFR)
$this->baseRecord([
'id' => 26,
'official_name'=> 'Island of Gorée',
'name' => 'Island of Gorée',
'study_region' => StudyRegion::AFRICA->value,
'latitude' => 14.66722,
'longitude' => -17.40083,
]),

// Europe × 3
// id:3 Aachen Cathedral (Germany, EUR)
$this->baseRecord([
'id' => 3,
'official_name'=> 'Aachen Cathedral',
'name' => 'Aachen Cathedral',
'study_region' => StudyRegion::EUROPE->value,
'latitude' => 50.7747468537,
'longitude' => 6.083919968,
]),
// id:29 Historic Centre of Kraków (Poland, EUR)
$this->baseRecord([
'id' => 29,
'official_name'=> 'Historic Centre of Kraków',
'name' => 'Historic Centre of Kraków',
'study_region' => StudyRegion::EUROPE->value,
'latitude' => 50.0613888889,
'longitude' => 19.9372222222,
]),
// id:30 Historic Centre of Warsaw (Poland, EUR)
$this->baseRecord([
'id' => 30,
'official_name'=> 'Historic Centre of Warsaw',
'name' => 'Historic Centre of Warsaw',
'study_region' => StudyRegion::EUROPE->value,
'latitude' => 52.25,
'longitude' => 21.013,
]),

// North America × 2
// id:4 L'Anse aux Meadows (Canada, EUR/North America)
$this->baseRecord([
'id' => 4,
'official_name'=> "L'Anse aux Meadows National Historic Site",
'name' => "L'Anse aux Meadows",
'study_region' => StudyRegion::NORTH_AMERICA->value,
'latitude' => 51.5847222222,
'longitude' => -55.55,
]),
// id:27 Mesa Verde National Park (USA, EUR/North America)
$this->baseRecord([
'id' => 27,
'official_name'=> 'Mesa Verde National Park',
'name' => 'Mesa Verde National Park',
'study_region' => StudyRegion::NORTH_AMERICA->value,
'latitude' => 37.26166667,
'longitude' => -108.4855556,
]),

// South America × 2
// id:1 Galápagos Islands (Ecuador, LAC)
$this->baseRecord([
'id' => 1,
'official_name'=> 'Galápagos Islands',
'name' => 'Galápagos Islands',
'study_region' => StudyRegion::SOUTH_AMERICA->value,
'category' => 'Natural',
'latitude' => -0.68986,
'longitude' => -90.501319,
]),
// id:2 City of Quito (Ecuador, LAC)
$this->baseRecord([
'id' => 2,
'official_name'=> 'City of Quito',
'name' => 'City of Quito',
'study_region' => StudyRegion::SOUTH_AMERICA->value,
'latitude' => -0.22,
'longitude' => -78.5120833333,
]),

// Oceania × 1 (JSONにないため仮データ)
$this->baseRecord([
'id' => 9999,
'official_name'=> 'Test Oceania Site',
'name' => 'Test Oceania Site',
'study_region' => StudyRegion::OCEANIA->value,
'latitude' => -25.0,
'longitude' => 130.0,
]),

// Asia × 1 (JSONにないため仮データ)
$this->baseRecord([
'id' => 9998,
'official_name'=> 'Test Asia Site',
'name' => 'Test Asia Site',
'study_region' => StudyRegion::ASIA->value,
'latitude' => 35.0,
'longitude' => 135.0,
]),

// Unknown × 2 → カウントに含まれない
// id:20 Ancient City of Damascus (Syria, ARB)
$this->baseRecord([
'id' => 20,
'official_name'=> 'Ancient City of Damascus',
'name' => 'Ancient City of Damascus',
'study_region' => StudyRegion::UNKNOWN->value,
'latitude' => 33.5108333333,
'longitude' => 36.3097222222,
]),
// id:8 Ichkeul National Park (Tunisia, ARB)
$this->baseRecord([
'id' => 8,
'official_name'=> 'Ichkeul National Park',
'name' => 'Ichkeul National Park',
'study_region' => StudyRegion::UNKNOWN->value,
'category' => 'Natural',
'latitude' => 37.16361,
'longitude' => 9.67472,
]),
]);

$result = $this->queryService->getEachRegionsHeritagesCount();

$this->assertSame(3, $result[StudyRegion::AFRICA->value]);
$this->assertSame(2, $result[StudyRegion::ASIA->value]);
$this->assertSame(3, $result[StudyRegion::EUROPE->value]);
Comment on lines +225 to +229
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test expects Asia to be 2, but the fixture data only inserts one Asia record. The second count is coming from the production-specific +1 adjustment in the query service rather than from test data, making the test brittle/incorrect in isolation. Either insert the Jerusalem record (id 148 with study_region=UNKNOWN) into the test data, or change the production logic to only add 1 when that record is actually present.

Copilot uses AI. Check for mistakes.
$this->assertSame(2, $result[StudyRegion::NORTH_AMERICA->value]);
$this->assertSame(2, $result[StudyRegion::SOUTH_AMERICA->value]);
$this->assertSame(1, $result[StudyRegion::OCEANIA->value]);
$this->assertArrayNotHasKey(StudyRegion::UNKNOWN->value, $result);
}
}
23 changes: 23 additions & 0 deletions src/app/Packages/Domains/WorldHeritageQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,29 @@ public function searchHeritages(AlgoliaSearchListQuery $query): PaginationDto
]
);
}

public function getEachRegionsHeritagesCount(): array
{
$counts = $this->model
->whereNotNull('study_region')
->where('study_region', '!=', StudyRegion::UNKNOWN->value)
->groupBy('study_region')
->selectRaw('study_region, COUNT(*) as count')
->pluck('count', 'study_region')
->toArray();

Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grouped COUNT(*) values returned by pluck() are likely strings (PDO returns aggregate numeric columns as strings by default). This method can therefore return a mix of string and int values (especially after the += 1), which is error-prone for callers and can break strict assertions. Consider explicitly casting all counts to int before returning.

Suggested change
// Ensure all counts are integers (PDO returns COUNT(*) as strings by default)
$counts = array_map('intval', $counts);

Copilot uses AI. Check for mistakes.
foreach (StudyRegion::cases() as $region) {
if ($region === StudyRegion::UNKNOWN) continue;
$counts[$region->value] ??= 0;
}

// id: 148 (Old City of Jerusalem) is stored as Unknown
// but geographically belongs to Asia
$counts[StudyRegion::ASIA->value] += 1;
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getEachRegionsHeritagesCount() unconditionally increments the Asia count, which will over-count if the dataset doesn’t actually contain the Jerusalem record (or if that record is later fixed to have study_region=ASIA). Consider making this adjustment conditional (e.g., only if id=148 exists with study_region=UNKNOWN), or better, fix the underlying data via a migration/seed so the query can stay purely data-driven.

Suggested change
$counts[StudyRegion::ASIA->value] += 1;
if ($this->model
->where('id', 148)
->where('study_region', StudyRegion::UNKNOWN->value)
->exists()
) {
$counts[StudyRegion::ASIA->value] += 1;
}

Copilot uses AI. Check for mistakes.

return $counts;
}

private function buildWorldHeritagePayload($heritage): array
{
$countryRelations = $heritage->countries ?? collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public function getHeritagesByIds(
public function searchHeritages(
AlgoliaSearchListQuery $query
): PaginationDto;

public function getEachRegionsHeritagesCount(): array;
}
Loading