22
33namespace App \Packages \Domains \Test \QueryService ;
44
5+ use App \Common \Pagination \PaginationDto ;
6+ use App \Models \WorldHeritage ;
7+ use Database \Seeders \JapaneseWorldHeritageSeeder ;
8+ use Illuminate \Support \Facades \DB ;
59use Tests \TestCase ;
610use App \Packages \Domains \WorldHeritageQueryService ;
7- use App \Models \WorldHeritage ;
811
912class WorldHeritageQueryService_getByIdsTest extends TestCase
1013{
11-
1214 private $ queryService ;
13- private int $ currentPage ;
1415 private int $ perPage ;
16+ private int $ currentPage ;
1517
1618 protected function setUp (): void
1719 {
1820 parent ::setUp ();
21+ $ this ->refresh ();
22+ $ this ->perPage = 15 ;
23+ $ this ->currentPage = 1 ;
1924 $ this ->queryService = app (WorldHeritageQueryService::class);
25+ $ seeder = new JapaneseWorldHeritageSeeder ();
26+ $ seeder ->run ();
2027 }
2128
2229 protected function tearDown (): void
2330 {
31+ $ this ->refresh ();
2432 parent ::tearDown ();
2533 }
2634
35+ private function refresh (): void
36+ {
37+ if (env ('APP_ENV ' ) === 'testing ' ) {
38+ DB ::connection ('mysql ' )->statement ('SET FOREIGN_KEY_CHECKS=0; ' );
39+ WorldHeritage::truncate ();
40+ DB ::connection ('mysql ' )->statement ('SET FOREIGN_KEY_CHECKS=1; ' );
41+ }
42+ }
43+
2744 private static function arrayData (): array
2845 {
2946 return [
@@ -47,6 +64,7 @@ private static function arrayData(): array
4764 'short_description ' => "Early Buddhist wooden structures including the world's oldest wooden building. " ,
4865 'image_url ' => '' ,
4966 'unesco_site_url ' => 'https://whc.unesco.org/en/list/660/ ' ,
67+ 'created_at ' => now (), 'updated_at ' => now (),
5068 ],
5169 [
5270 'id ' => 2 ,
@@ -68,6 +86,7 @@ private static function arrayData(): array
6886 'short_description ' => "A masterpiece of Japanese castle architecture in original form. " ,
6987 'image_url ' => '' ,
7088 'unesco_site_url ' => 'https://whc.unesco.org/en/list/661/ ' ,
89+ 'created_at ' => now (), 'updated_at ' => now (),
7190 ],
7291 [
7392 'id ' => 3 ,
@@ -89,6 +108,7 @@ private static function arrayData(): array
89108 'short_description ' => "A subtropical island with ancient cedar forests and diverse ecosystems. " ,
90109 'image_url ' => '' ,
91110 'unesco_site_url ' => 'https://whc.unesco.org/en/list/662/ ' ,
111+ 'created_at ' => now (), 'updated_at ' => now (),
92112 ],
93113 [
94114 'id ' => 4 ,
@@ -110,64 +130,53 @@ private static function arrayData(): array
110130 'short_description ' => "Pristine beech forest with minimal human impact. " ,
111131 'image_url ' => '' ,
112132 'unesco_site_url ' => 'https://whc.unesco.org/en/list/663/ ' ,
113- ]
133+ 'created_at ' => now (), 'updated_at ' => now (),
134+ ],
114135 ];
115136 }
116137
117- public function test_getByIds_count_objects (): void
138+ public function test_fetch_data_check_type (): void
118139 {
119- $ ids = [1 , 2 ];
120- $ result = $ this ->queryService ->getHeritagesByIds ($ ids , 1 , 10 );
121-
122- $ this ->assertCount (2 , $ result ->getCollection ());
140+ $ ids = array_column (self ::arrayData (), 'id ' );
141+ $ result = $ this ->queryService ->getHeritagesByIds (
142+ $ ids ,
143+ $ this ->perPage ,
144+ $ this ->currentPage
145+ );
146+
147+ $ this ->assertInstanceOf (PaginationDto::class, $ result );
123148 }
124149
125- public function test_getByIds_check_each_value (): void
150+ public function test_fetch_data_check_value (): void
126151 {
127- $ ids = [1 , 2 ];
128- $ result = $ this ->queryService ->getHeritagesByIds ($ ids , 1 , 10 );
129-
130- $ expectedMap = collect (self ::arrayData ())
131- ->filter (fn ($ row ) => in_array ($ row ['id ' ], $ ids ))
132- ->keyBy ('id ' );
133-
134- foreach ($ result ->getCollection () as $ entity ) {
135- $ id = $ entity ['id ' ];
136- $ expected = $ expectedMap [$ id ] ?? null ;
137- $ this ->assertSame ($ expected ['id ' ], $ entity ['id ' ]);
138- $ this ->assertEquals ($ expected ['unesco_id ' ], $ entity ['unesco_id ' ]);
139- $ this ->assertSame ($ expected ['official_name ' ], $ entity ['official_name ' ]);
140- $ this ->assertSame ($ expected ['name ' ], $ entity ['name ' ]);
141- $ this ->assertSame ($ expected ['name_jp ' ], $ entity ['name_jp ' ]);
142- $ this ->assertSame ($ expected ['country ' ], $ entity ['country ' ]);
143- $ this ->assertSame ($ expected ['region ' ], $ entity ['region ' ]);
144- $ this ->assertSame ($ expected ['state_party ' ], $ entity ['state_party ' ]);
145- $ this ->assertSame ($ expected ['category ' ], $ entity ['category ' ]);
146- $ this ->assertSame ($ expected ['criteria ' ], $ entity ['criteria ' ]);
147- $ this ->assertEquals ($ expected ['year_inscribed ' ], $ entity ['year_inscribed ' ]);
148- $ this ->assertSame ($ expected ['area_hectares ' ], $ entity ['area_hectares ' ]);
149- $ this ->assertSame ($ expected ['buffer_zone_hectares ' ], $ entity ['buffer_zone_hectares ' ]);
150- $ this ->assertIsBool ($ expected ['is_endangered ' ], $ entity ['is_endangered ' ]);
151- $ this ->assertIsFloat ($ expected ['latitude ' ], $ entity ['latitude ' ]);
152- $ this ->assertIsFloat ($ expected ['longitude ' ], $ entity ['longitude ' ]);
153- $ this ->assertSame ($ expected ['short_description ' ], $ entity ['short_description ' ]);
154- $ this ->assertSame ($ expected ['image_url ' ], $ entity ['image_url ' ]);
155- $ this ->assertSame ($ expected ['unesco_site_url ' ], $ entity ['unesco_site_url ' ]);
152+ $ ids = array_column (self ::arrayData (), 'id ' );
153+ $ result = $ this ->queryService ->getHeritagesByIds (
154+ $ ids ,
155+ $ this ->currentPage ,
156+ $ this ->perPage
157+ );
158+
159+ $ this ->assertSame (array_column (self ::arrayData (), 'id ' ), array_column ($ result ->toArray ()['data ' ], 'id ' ));
160+ foreach ($ result ->toArray ()['data ' ] as $ key => $ value ) {
161+ $ this ->assertSame (self ::arrayData ()[$ key ]['id ' ], $ value ['id ' ]);
162+ $ this ->assertSame (self ::arrayData ()[$ key ]['unesco_id ' ], $ value ['unescoId ' ]);
163+ $ this ->assertSame (self ::arrayData ()[$ key ]['official_name ' ], $ value ['officialName ' ]);
164+ $ this ->assertSame (self ::arrayData ()[$ key ]['name ' ], $ value ['name ' ]);
165+ $ this ->assertSame (self ::arrayData ()[$ key ]['name_jp ' ], $ value ['nameJp ' ]);
166+ $ this ->assertSame (self ::arrayData ()[$ key ]['country ' ], $ value ['country ' ]);
167+ $ this ->assertSame (self ::arrayData ()[$ key ]['region ' ], $ value ['region ' ]);
168+ $ this ->assertSame (self ::arrayData ()[$ key ]['state_party ' ], $ value ['stateParty ' ]);
169+ $ this ->assertSame (self ::arrayData ()[$ key ]['category ' ], $ value ['category ' ]);
170+ $ this ->assertSame (self ::arrayData ()[$ key ]['criteria ' ], $ value ['criteria ' ]);
171+ $ this ->assertSame (self ::arrayData ()[$ key ]['year_inscribed ' ], $ value ['yearInscribed ' ]);
172+ $ this ->assertSame (self ::arrayData ()[$ key ]['area_hectares ' ], $ value ['areaHectares ' ]);
173+ $ this ->assertSame (self ::arrayData ()[$ key ]['buffer_zone_hectares ' ], $ value ['bufferZoneHectares ' ]);
174+ $ this ->assertSame (self ::arrayData ()[$ key ]['is_endangered ' ], $ value ['isEndangered ' ]);
175+ $ this ->assertSame (self ::arrayData ()[$ key ]['latitude ' ], $ value ['latitude ' ]);
176+ $ this ->assertSame (self ::arrayData ()[$ key ]['longitude ' ], $ value ['longitude ' ]);
177+ $ this ->assertSame (self ::arrayData ()[$ key ]['short_description ' ], $ value ['shortDescription ' ]);
178+ $ this ->assertSame (self ::arrayData ()[$ key ]['image_url ' ], $ value ['imageUrl ' ]);
179+ $ this ->assertSame (self ::arrayData ()[$ key ]['unesco_site_url ' ], $ value ['unescoSiteUrl ' ]);
156180 }
157181 }
158-
159- public function test_getByIds_pagination_meta (): void
160- {
161- $ ids = [1 , 2 ];
162- $ currentPage = 1 ;
163- $ perPage = 1 ;
164-
165- $ result = $ this ->queryService ->getHeritagesByIds ($ ids , $ currentPage , $ perPage );
166-
167- $ this ->assertSame ($ currentPage , $ result ->getCurrentPage ());
168- $ this ->assertSame ($ perPage , $ result ->getPerPage ());
169-
170- $ this ->assertSame (2 , $ result ->getTotal ());
171- $ this ->assertSame (2 , $ result ->getLastPage ());
172- }
173182}
0 commit comments