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