22
33namespace TarfinLabs \LaravelConfig \Tests ;
44
5+ use Carbon \Carbon ;
56use Illuminate \Support \Str ;
67use TarfinLabs \LaravelConfig \Config \Config ;
78use TarfinLabs \LaravelConfig \Config \ConfigFactory ;
9+ use TarfinLabs \LaravelConfig \Enums \ConfigDataType ;
810use TarfinLabs \LaravelConfig \LaravelConfig ;
911
1012class LaravelConfigTest extends TestCase
@@ -24,17 +26,17 @@ public function it_create_a_new_config_parameter(): void
2426 {
2527 $ factory = new ConfigFactory ();
2628 $ configItem = $ factory ->setName (Str::random (5 ))
27- ->setType (' boolean ' )
29+ ->setType (ConfigDataType:: BOOLEAN )
2830 ->setValue ('1 ' )
2931 ->setDescription (Str::random (50 ))
3032 ->get ();
3133
3234 $ this ->laravelConfig ->create ($ configItem );
3335
3436 $ this ->assertDatabaseHas (config ('laravel-config.table ' ), [
35- 'name ' => $ configItem ->name ,
36- 'val ' => $ configItem ->val ,
37- 'type ' => $ configItem ->type ,
37+ 'name ' => $ configItem ->name ,
38+ 'val ' => $ configItem ->val ,
39+ 'type ' => $ configItem ->type ,
3840 'description ' => $ configItem ->description ,
3941 ]);
4042 }
@@ -44,7 +46,7 @@ public function it_create_a_new_config_parameter_with_tag(): void
4446 {
4547 $ factory = new ConfigFactory ();
4648 $ configItem = $ factory ->setName (Str::random (5 ))
47- ->setType (' boolean ' )
49+ ->setType (ConfigDataType:: BOOLEAN )
4850 ->setValue ('1 ' )
4951 ->setTags (['system ' ])
5052 ->setDescription (Str::random (50 ))
@@ -53,9 +55,9 @@ public function it_create_a_new_config_parameter_with_tag(): void
5355 $ this ->laravelConfig ->create ($ configItem );
5456
5557 $ this ->assertDatabaseHas (config ('laravel-config.table ' ), [
56- 'name ' => $ configItem ->name ,
57- 'val ' => $ configItem ->val ,
58- 'type ' => $ configItem ->type ,
58+ 'name ' => $ configItem ->name ,
59+ 'val ' => $ configItem ->val ,
60+ 'type ' => $ configItem ->type ,
5961 'description ' => $ configItem ->description ,
6062 ]);
6163
@@ -70,7 +72,7 @@ public function it_does_not_create_a_config_parameter_with_the_same_name(): void
7072
7173 $ factory = new ConfigFactory ();
7274 $ configItem = $ factory ->setName ($ config ->name )
73- ->setType (' boolean ' )
75+ ->setType (ConfigDataType:: BOOLEAN )
7476 ->setValue ('1 ' )
7577 ->setDescription (Str::random (50 ))
7678 ->get ();
@@ -87,17 +89,17 @@ public function it_updates_existing_config_parameter(): void
8789 $ this ->assertDatabaseHas (config ('laravel-config.table ' ), ['name ' => $ config ->name , 'val ' => $ config ->val ]);
8890
8991 $ factory = new ConfigFactory ($ config );
90- $ configItem = $ factory ->setType (' boolean ' )
92+ $ configItem = $ factory ->setType (ConfigDataType:: BOOLEAN )
9193 ->setValue ('0 ' )
9294 ->setDescription ('updated-description ' )
9395 ->get ();
9496
9597 $ this ->laravelConfig ->update ($ config , $ configItem );
9698
9799 $ this ->assertDatabaseHas (config ('laravel-config.table ' ), [
98- 'name ' => $ config ->name ,
99- 'val ' => $ configItem ->val ,
100- 'type ' => $ configItem ->type ,
100+ 'name ' => $ config ->name ,
101+ 'val ' => $ configItem ->val ,
102+ 'type ' => $ configItem ->type ,
101103 'description ' => $ configItem ->description ,
102104 ]);
103105 }
@@ -191,13 +193,13 @@ public function it_returns_all_config_parameters(): void
191193 public function it_returns_nested_config_parameters (): void
192194 {
193195 factory (Config::class)->create ([
194- 'name ' => 'foo.bar ' ,
195- 'val ' => true ,
196+ 'name ' => 'foo.bar ' ,
197+ 'val ' => true ,
196198 ]);
197199
198200 factory (Config::class)->create ([
199- 'name ' => 'foo.baz ' ,
200- 'val ' => false ,
201+ 'name ' => 'foo.baz ' ,
202+ 'val ' => false ,
201203 ]);
202204
203205 $ response = $ this ->laravelConfig ->getNested ('foo ' );
@@ -206,4 +208,78 @@ public function it_returns_nested_config_parameters(): void
206208 $ this ->assertEquals ('bar ' , $ response ->first ()->name );
207209 $ this ->assertEquals ('baz ' , $ response ->last ()->name );
208210 }
211+
212+ /** @test */
213+ public function it_returns_boolean_value_for_boolean_type_config_parameter_if_exists (): void
214+ {
215+ $ config = factory (Config::class)->create ([
216+ 'name ' => 'yunus.was.here ' ,
217+ 'val ' => '1 ' ,
218+ 'type ' => ConfigDataType::BOOLEAN ,
219+ ]);
220+
221+ $ response = $ this ->laravelConfig ->get ($ config ->name );
222+
223+ $ this ->assertTrue ($ response );
224+ }
225+
226+ /** @test */
227+ public function it_returns_integer_value_for_integer_type_config_parameter_if_exists (): void
228+ {
229+ $ config = factory (Config::class)->create ([
230+ 'name ' => 'yunus.was.here ' ,
231+ 'val ' => '123456 ' ,
232+ 'type ' => ConfigDataType::INTEGER ,
233+ ]);
234+
235+ $ response = $ this ->laravelConfig ->get ($ config ->name );
236+
237+ $ this ->assertIsInt ($ response );
238+ }
239+
240+ /** @test */
241+ public function it_returns_datetime_value_for_datetime_type_config_parameter_if_exists (): void
242+ {
243+ $ config = factory (Config::class)->create ([
244+ 'name ' => 'yunus.was.here ' ,
245+ 'val ' => '2024-02-29 12:00 ' ,
246+ 'type ' => ConfigDataType::DATE_TIME ,
247+ ]);
248+
249+ $ response = $ this ->laravelConfig ->get ($ config ->name );
250+
251+ $ this ->assertInstanceOf (Carbon::class, $ response );
252+ }
253+
254+ /** @test */
255+ public function it_returns_date_value_for_date_type_config_parameter_if_exists (): void
256+ {
257+ $ config = factory (Config::class)->create ([
258+ 'name ' => 'yunus.was.here ' ,
259+ 'val ' => '2024-02-29 ' ,
260+ 'type ' => ConfigDataType::DATE ,
261+ ]);
262+
263+ $ response = $ this ->laravelConfig ->get ($ config ->name );
264+
265+ $ this ->assertInstanceOf (Carbon::class, $ response );
266+ }
267+
268+ /** @test */
269+ public function it_returns_json_value_for_json_type_config_parameter_if_exists (): void
270+ {
271+ $ config = factory (Config::class)->create ([
272+ 'name ' => 'yunus.was.here ' ,
273+ 'val ' => '{"9":[7,8,9],"2":[7,8,9],"31":[10,11,12]} ' ,
274+ 'type ' => ConfigDataType::JSON ,
275+ ]);
276+
277+ $ response = $ this ->laravelConfig ->get ($ config ->name );
278+
279+ $ this ->assertIsArray ($ response );
280+
281+ $ this ->assertArrayHasKey (9 , $ response );
282+ $ this ->assertArrayHasKey (2 , $ response );
283+ $ this ->assertArrayHasKey (31 , $ response );
284+ }
209285}
0 commit comments