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 App \Packages \Domains \Ports \Dto \HeritageSearchResult ;
9+ use App \Packages \Domains \Ports \WorldHeritageSearchPort ;
10+ use Database \Seeders \DatabaseSeeder ;
11+ use Illuminate \Support \Facades \DB ;
12+ use Tests \TestCase ;
13+
14+ class GetCountEachRegionTest extends TestCase
15+ {
16+ protected function setUp (): void
17+ {
18+ parent ::setUp ();
19+ $ this ->refresh ();
20+
21+ $ this ->app ->bind (WorldHeritageSearchPort::class, function () {
22+ return new class implements WorldHeritageSearchPort {
23+ public function search ($ query , int $ currentPage , int $ perPage ): HeritageSearchResult {
24+ return new HeritageSearchResult (ids: [], total: 0 , currentPage: 1 , perPage: $ perPage , lastPage: 0 );
25+ }
26+ };
27+ });
28+
29+ $ seeder = new DatabaseSeeder ();
30+ $ seeder ->run ();
31+ }
32+
33+ protected function tearDown (): void
34+ {
35+ $ this ->refresh ();
36+ parent ::tearDown ();
37+ }
38+
39+ private function refresh (): void
40+ {
41+ if (env ('APP_ENV ' ) === 'testing ' ) {
42+ DB ::connection ('mysql ' )->statement ('SET FOREIGN_KEY_CHECKS=0; ' );
43+ WorldHeritage::truncate ();
44+ Country::truncate ();
45+ DB ::table ('site_state_parties ' )->truncate ();
46+ Image::truncate ();
47+ DB ::connection ('mysql ' )->statement ('SET FOREIGN_KEY_CHECKS=1; ' );
48+ }
49+ }
50+
51+ public function test_check_count_each_region_value (): void
52+ {
53+ $ response = $ this ->getJson ('/api/v1/heritages/region-count ' );
54+
55+ $ response ->assertStatus (200 )
56+ ->assertJsonStructure ([
57+ 'data ' => [
58+ '* ' => [
59+ 'region ' ,
60+ 'count ' ,
61+ ],
62+ ],
63+ ]);
64+ }
65+
66+ public function test_returns_all_six_regions (): void
67+ {
68+ $ response = $ this ->getJson ('/api/v1/heritages/region-count ' );
69+
70+ $ data = $ response ->json ('data ' );
71+ $ regions = array_column ($ data , 'region ' );
72+
73+
74+ $ this ->assertCount (6 , $ data );
75+ $ this ->assertContains ('Africa ' , $ regions );
76+ $ this ->assertContains ('Asia ' , $ regions );
77+ $ this ->assertContains ('Europe ' , $ regions );
78+ $ this ->assertContains ('North America ' , $ regions );
79+ $ this ->assertContains ('South America ' , $ regions );
80+ $ this ->assertContains ('Oceania ' , $ regions );
81+ }
82+
83+ public function test_count_is_positive_integer (): void
84+ {
85+ $ response = $ this ->getJson ('/api/v1/heritages/region-count ' );
86+
87+ foreach ($ response ->json ('data ' ) as $ item ) {
88+ $ this ->assertIsInt ($ item ['count ' ]);
89+ $ this ->assertGreaterThanOrEqual (0 , $ item ['count ' ]);
90+ }
91+ }
92+
93+ public function test_unknown_region_is_not_included (): void
94+ {
95+ $ response = $ this ->getJson ('/api/v1/heritages/region-count ' );
96+
97+ $ regions = array_column ($ response ->json ('data ' ), 'region ' );
98+ $ this ->assertNotContains ('Unknown ' , $ regions );
99+ }
100+ }
0 commit comments