1+ <?php
2+
3+ namespace App \Packages \Domains \Test \Repository ;
4+
5+ use Tests \TestCase ;
6+ use App \Packages \Domains \WorldHeritageEntity ;
7+ use App \Packages \Domains \WorldHeritageEntityCollection ;
8+ use Illuminate \Support \Facades \DB ;
9+ use App \Models \WorldHeritage ;
10+ use App \Packages \Domains \WorldHeritageRepository ;
11+
12+ class WorldHeritageRepository_insertManyTest extends TestCase
13+ {
14+
15+ private $ repository ;
16+ protected function setUp (): void
17+ {
18+ parent ::setUp ();
19+ $ this ->refresh ();
20+ $ this ->repository = app (WorldHeritageRepository::class);
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+ DB ::connection ('mysql ' )->statement ('SET FOREIGN_KEY_CHECKS=1; ' );
35+ }
36+ }
37+
38+ private function arrayData (): array
39+ {
40+ return [
41+ [
42+ 'unesco_id ' => '660 ' ,
43+ 'official_name ' => 'Buddhist Monuments in the Horyu-ji Area ' ,
44+ 'name ' => 'Buddhist Monuments in the Horyu-ji Area ' ,
45+ 'name_jp ' => '法隆寺地域の仏教建造物 ' ,
46+ 'country ' => 'Japan ' ,
47+ 'region ' => 'Asia ' ,
48+ 'state_party ' => 'JP ' ,
49+ 'category ' => 'cultural ' ,
50+ 'criteria ' => ['ii ' , 'iii ' , 'v ' ],
51+ 'year_inscribed ' => 1993 ,
52+ 'area_hectares ' => 442.0 ,
53+ 'buffer_zone_hectares ' => 320.0 ,
54+ 'is_endangered ' => false ,
55+ 'latitude ' => 34.6147 ,
56+ 'longitude ' => 135.7355 ,
57+ 'short_description ' => "Early Buddhist wooden structures including the world's oldest wooden building. " ,
58+ 'image_url ' => '' ,
59+ 'unesco_site_url ' => 'https://whc.unesco.org/en/list/660/ ' ,
60+ 'created_at ' => now (), 'updated_at ' => now (),
61+ ],
62+ [
63+ 'unesco_id ' => '661 ' ,
64+ 'official_name ' => 'Himeji-jo ' ,
65+ 'name ' => 'Himeji-jo ' ,
66+ 'name_jp ' => '姫路城 ' ,
67+ 'country ' => 'Japan ' ,
68+ 'region ' => 'Asia ' ,
69+ 'state_party ' => 'JP ' ,
70+ 'category ' => 'cultural ' ,
71+ 'criteria ' => ['ii ' , 'iii ' , 'v ' ],
72+ 'year_inscribed ' => 1993 ,
73+ 'area_hectares ' => 442.0 ,
74+ 'buffer_zone_hectares ' => 320.0 ,
75+ 'is_endangered ' => false ,
76+ 'latitude ' => 34.8394 ,
77+ 'longitude ' => 134.6939 ,
78+ 'short_description ' => "A masterpiece of Japanese castle architecture in original form. " ,
79+ 'image_url ' => '' ,
80+ 'unesco_site_url ' => 'https://whc.unesco.org/en/list/661/ ' ,
81+ 'created_at ' => now (), 'updated_at ' => now (),
82+ ],
83+ [
84+ 'unesco_id ' => '662 ' ,
85+ 'official_name ' => 'Yakushima ' ,
86+ 'name ' => 'Yakushima ' ,
87+ 'name_jp ' => '屋久島 ' ,
88+ 'country ' => 'Japan ' ,
89+ 'region ' => 'Asia ' ,
90+ 'state_party ' => 'JP ' ,
91+ 'category ' => 'natural ' ,
92+ 'criteria ' => ['ii ' , 'iii ' , 'v ' ],
93+ 'year_inscribed ' => 1993 ,
94+ 'area_hectares ' => 442.0 ,
95+ 'buffer_zone_hectares ' => 320.0 ,
96+ 'is_endangered ' => false ,
97+ 'latitude ' => 30.3581 ,
98+ 'longitude ' => 130.546 ,
99+ 'short_description ' => "A subtropical island with ancient cedar forests and diverse ecosystems. " ,
100+ 'image_url ' => '' ,
101+ 'unesco_site_url ' => 'https://whc.unesco.org/en/list/662/ ' ,
102+ 'created_at ' => now (), 'updated_at ' => now (),
103+ ],
104+ [
105+ 'unesco_id ' => '663 ' ,
106+ 'official_name ' => 'Shirakami-Sanchi ' ,
107+ 'name ' => 'Shirakami-Sanchi ' ,
108+ 'name_jp ' => '白神山地 ' ,
109+ 'country ' => 'Japan ' ,
110+ 'region ' => 'Asia ' ,
111+ 'state_party ' => 'JP ' ,
112+ 'category ' => 'natural ' ,
113+ 'criteria ' => ['ii ' , 'iii ' , 'v ' ],
114+ 'year_inscribed ' => 1993 ,
115+ 'area_hectares ' => 442.0 ,
116+ 'buffer_zone_hectares ' => 320.0 ,
117+ 'is_endangered ' => false ,
118+ 'latitude ' => 40.5167 ,
119+ 'longitude ' => 140.05 ,
120+ 'short_description ' => "Pristine beech forest with minimal human impact. " ,
121+ 'image_url ' => '' ,
122+ 'unesco_site_url ' => 'https://whc.unesco.org/en/list/663/ ' ,
123+ 'created_at ' => now (), 'updated_at ' => now (),
124+ ],
125+ ];
126+ }
127+
128+ public function test_check_return_type (): void
129+ {
130+ $ collection = new WorldHeritageEntityCollection (
131+ array_map (function ($ d ) {
132+ return new WorldHeritageEntity (
133+ null ,
134+ $ d ['unesco_id ' ],
135+ $ d ['official_name ' ],
136+ $ d ['name ' ],
137+ $ d ['country ' ],
138+ $ d ['region ' ],
139+ $ d ['category ' ],
140+ (int ) $ d ['year_inscribed ' ],
141+ isset ($ d ['latitude ' ]) ? (float ) $ d ['latitude ' ] : null ,
142+ isset ($ d ['longitude ' ]) ? (float ) $ d ['longitude ' ] : null ,
143+ (bool ) ($ d ['is_endangered ' ] ?? false ),
144+ $ d ['name_jp ' ] ?? null ,
145+ $ d ['state_party ' ] ?? null ,
146+ is_string ($ d ['criteria ' ] ?? null )
147+ ? json_decode ($ d ['criteria ' ], true , 512 , JSON_THROW_ON_ERROR )
148+ : ($ d ['criteria ' ] ?? []),
149+ isset ($ d ['area_hectares ' ]) ? (float ) $ d ['area_hectares ' ] : null ,
150+ isset ($ d ['buffer_zone_hectares ' ]) ? (float ) $ d ['buffer_zone_hectares ' ] : null ,
151+ $ d ['short_description ' ] ?? null ,
152+ $ d ['image_url ' ] ?? null ,
153+ $ d ['unesco_site_url ' ] ?? null
154+ );
155+ }, self ::arrayData ())
156+ );
157+
158+ $ result = $ this ->repository ->insertHeritages ($ collection );
159+ $ this ->assertInstanceOf (WorldHeritageEntityCollection::class, $ result );
160+ }
161+
162+ public function test_check_return_value (): void
163+ {
164+ $ collection = new WorldHeritageEntityCollection (
165+ array_map (function ($ data ) {
166+ return new WorldHeritageEntity (
167+ null ,
168+ $ data ['unesco_id ' ],
169+ $ data ['official_name ' ],
170+ $ data ['name ' ],
171+ $ data ['country ' ],
172+ $ data ['region ' ],
173+ $ data ['category ' ],
174+ $ data ['year_inscribed ' ],
175+ $ data ['latitude ' ],
176+ $ data ['longitude ' ],
177+ $ data ['is_endangered ' ],
178+ $ data ['name_jp ' ],
179+ $ data ['state_party ' ],
180+ $ data ['criteria ' ],
181+ $ data ['area_hectares ' ],
182+ $ data ['buffer_zone_hectares ' ],
183+ $ data ['short_description ' ],
184+ $ data ['image_url ' ],
185+ $ data ['unesco_site_url ' ]
186+ );
187+ }, self ::arrayData ())
188+ );
189+
190+ $ result = $ this ->repository ->insertHeritages ($ collection );
191+
192+ foreach ($ result ->getAllHeritages () as $ entity ) {
193+ foreach (self ::arrayData () as $ value ) {
194+ if ((string )$ entity ->getUnescoId () !== (string )$ value ['unesco_id ' ]) {
195+ continue ;
196+ }
197+ $ this ->assertEquals ($ value ['unesco_id ' ], $ entity ->getUnescoId ());
198+ $ this ->assertEquals ($ value ['official_name ' ], $ entity ->getOfficialName ());
199+ $ this ->assertEquals ($ value ['name ' ], $ entity ->getName ());
200+ $ this ->assertEquals ($ value ['country ' ], $ entity ->getCountry ());
201+ $ this ->assertEquals ($ value ['region ' ], $ entity ->getRegion ());
202+ $ this ->assertEquals ($ value ['category ' ], $ entity ->getCategory ());
203+ $ this ->assertEquals ($ value ['year_inscribed ' ], $ entity ->getYearInscribed ());
204+ $ this ->assertEquals ($ value ['latitude ' ], $ entity ->getLatitude ());
205+ $ this ->assertEquals ($ value ['longitude ' ], $ entity ->getLongitude ());
206+ $ this ->assertEquals ($ value ['is_endangered ' ], $ entity ->isEndangered ());
207+ $ this ->assertEquals ($ value ['name_jp ' ], $ entity ->getNameJp ());
208+ $ this ->assertEquals ($ value ['state_party ' ], $ entity ->getStateParty ());
209+ $ this ->assertEquals ($ value ['criteria ' ], $ entity ->getCriteria ());
210+ $ this ->assertEquals ($ value ['area_hectares ' ], $ entity ->getAreaHectares ());
211+ $ this ->assertEquals ($ value ['buffer_zone_hectares ' ], $ entity ->getBufferZoneHectares ());
212+ $ this ->assertEquals ($ value ['short_description ' ], $ entity ->getShortDescription ());
213+ $ this ->assertEquals ($ value ['image_url ' ], $ entity ->getImageUrl ());
214+ $ this ->assertEquals ($ value ['unesco_site_url ' ], $ entity ->getUnescoSiteUrl ());
215+
216+ break ;
217+ }
218+ }
219+ }
220+ }
0 commit comments