1+ <?php
2+
3+ namespace Matteoc99 \LaravelPreference \Tests \Casts ;
4+
5+ use Illuminate \Foundation \Testing \RefreshDatabase ;
6+ use Illuminate \Foundation \Testing \WithFaker ;
7+ use Matteoc99 \LaravelPreference \Casts \ValueCaster ;
8+ use Matteoc99 \LaravelPreference \Enums \Cast ;
9+
10+ class ValueCasterTest extends CasterTestCase
11+ {
12+ use WithFaker, RefreshDatabase;
13+
14+ /** @test */
15+ public function test_get ()
16+ {
17+ $ caster = new ValueCaster ();
18+
19+ // With Int Cast
20+ $ this ->dummyPref ->cast = Cast::INT ;
21+ $ result = $ caster ->get ($ this ->dummyPref , '' , '123 ' , []);
22+ $ this ->assertEquals (123 , $ result ); // Assert integer conversion
23+
24+ // With Float Cast
25+ $ this ->dummyPref ->cast = Cast::FLOAT ;
26+ $ result = $ caster ->get ($ this ->dummyPref , '' , '3.14 ' , []);
27+ $ this ->assertEquals (3.14 , $ result ); // Assert float conversion
28+
29+ // With String Cast (or unknown)
30+ $ this ->dummyPref ->cast = Cast::STRING ;
31+ $ result = $ caster ->get ($ this ->dummyPref , '' , 'hello ' , []);
32+ $ this ->assertEquals ('hello ' , $ result ); // Assert no change
33+
34+
35+ $ this ->dummyPref ->cast = Cast::ARRAY ;
36+ $ result = $ caster ->get ($ this ->dummyPref , '' , "[1, \"hello \"] " , []);
37+ $ this ->assertIsArray ($ result ); // Assert valid JSON representation
38+
39+ // With Date Cast
40+ $ this ->dummyPref ->cast = Cast::DATE ;
41+ $ result = $ caster ->get ($ this ->dummyPref , '' , '2023-12-25 ' , []);
42+ $ this ->assertInstanceOf (\Carbon \Carbon::class, $ result );
43+ $ this ->assertEquals ('2023-12-25 ' , $ result ->toDateString ());
44+
45+ // With Time Cast
46+ $ this ->dummyPref ->cast = Cast::TIME ;
47+ $ result = $ caster ->get ($ this ->dummyPref , '' , '15:30:00 ' , []);
48+ $ this ->assertInstanceOf (\Carbon \Carbon::class, $ result );
49+ $ this ->assertEquals ('15:30:00 ' , $ result ->toTimeString ());
50+
51+ // With DateTime Cast
52+ $ this ->dummyPref ->cast = Cast::DATETIME ;
53+ $ result = $ caster ->get ($ this ->dummyPref , '' , '2023-12-25 15:30:00 ' , []);
54+ $ this ->assertInstanceOf (\Carbon \Carbon::class, $ result );
55+ $ this ->assertEquals ('2023-12-25 15:30:00 ' , $ result ->toDateTimeString ());
56+
57+ // With Timestamp Cast
58+ $ this ->dummyPref ->cast = Cast::TIMESTAMP ;
59+ $ timestamp = 1679164665 ;
60+ $ result = $ caster ->get ($ this ->dummyPref , '' , (string )$ timestamp , []);
61+ $ this ->assertInstanceOf (\Carbon \Carbon::class, $ result );
62+ $ this ->assertEquals ($ timestamp , $ result ->getTimestamp ());
63+
64+ }
65+
66+ /** @test */
67+ public function test_set ()
68+ {
69+ $ caster = new ValueCaster ();
70+
71+ // With Bool Cast
72+ $ this ->dummyPref ->cast = Cast::BOOL ;
73+ $ result = $ caster ->set ($ this ->dummyPref , '' , true , []);
74+ $ this ->assertEquals (true , $ result );
75+
76+ // With Array cast
77+ $ this ->dummyPref ->cast = Cast::ARRAY ;
78+ $ result = $ caster ->set ($ this ->dummyPref , '' , [1 , "hello " ], []);
79+ $ this ->assertJson ($ result ); // Assert valid JSON representation
80+
81+ $ this ->dummyPref ->cast = Cast::DATE ;
82+ $ date = \Carbon \Carbon::now ();
83+ $ result = $ caster ->set ($ this ->dummyPref , '' , $ date , []);
84+ $ this ->assertEquals ($ date ->toDateString (), $ result );
85+
86+ // With Time Cast
87+ $ this ->dummyPref ->cast = Cast::TIME ;
88+ $ time = \Carbon \Carbon::parse ('10:30:00 ' );
89+ $ result = $ caster ->set ($ this ->dummyPref , '' , $ time , []);
90+ $ this ->assertEquals ($ time ->toTimeString (), $ result );
91+
92+ // With Datetime Cast
93+ $ this ->dummyPref ->cast = Cast::DATETIME ;
94+ $ datetime = \Carbon \Carbon::now ();
95+ $ result = $ caster ->set ($ this ->dummyPref , '' , $ datetime , []);
96+ $ this ->assertEquals ($ datetime ->toDateTimeString (), $ result );
97+
98+ // With Timestamp Cast
99+ $ this ->dummyPref ->cast = Cast::TIMESTAMP ;
100+ $ timestamp = time ();
101+ $ result = $ caster ->set ($ this ->dummyPref , '' , $ timestamp , []);
102+ $ this ->assertEquals ((string )$ timestamp , $ result );
103+
104+ }
105+
106+ }
0 commit comments