@@ -17,17 +17,28 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
17
17
Schema::create ('test_eloquent_model_with_custom_casts ' , function (Blueprint $ table ) {
18
18
$ table ->increments ('id ' );
19
19
$ table ->text ('array_object ' );
20
+ $ table ->json ('array_object_json ' );
20
21
$ table ->text ('collection ' );
21
22
$ table ->string ('stringable ' );
22
23
$ table ->timestamps ();
23
24
});
25
+
26
+ Schema::create ('test_eloquent_model_with_custom_casts_nullables ' , function (Blueprint $ table ) {
27
+ $ table ->increments ('id ' );
28
+ $ table ->text ('array_object ' )->nullable ();
29
+ $ table ->json ('array_object_json ' )->nullable ();
30
+ $ table ->text ('collection ' )->nullable ();
31
+ $ table ->string ('stringable ' )->nullable ();
32
+ $ table ->timestamps ();
33
+ });
24
34
}
25
35
26
36
public function test_custom_casting ()
27
37
{
28
38
$ model = new TestEloquentModelWithCustomCasts ;
29
39
30
40
$ model ->array_object = ['name ' => 'Taylor ' ];
41
+ $ model ->array_object_json = ['name ' => 'Taylor ' ];
31
42
$ model ->collection = collect (['name ' => 'Taylor ' ]);
32
43
$ model ->stringable = Str::of ('Taylor ' );
33
44
@@ -36,21 +47,84 @@ public function test_custom_casting()
36
47
$ model = $ model ->fresh ();
37
48
38
49
$ this ->assertEquals (['name ' => 'Taylor ' ], $ model ->array_object ->toArray ());
50
+ $ this ->assertEquals (['name ' => 'Taylor ' ], $ model ->array_object_json ->toArray ());
39
51
$ this ->assertEquals (['name ' => 'Taylor ' ], $ model ->collection ->toArray ());
40
52
$ this ->assertSame ('Taylor ' , (string ) $ model ->stringable );
41
53
42
54
$ model ->array_object ['age ' ] = 34 ;
43
55
$ model ->array_object ['meta ' ]['title ' ] = 'Developer ' ;
44
56
57
+ $ model ->array_object_json ['age ' ] = 34 ;
58
+ $ model ->array_object_json ['meta ' ]['title ' ] = 'Developer ' ;
59
+
60
+ $ model ->save ();
61
+
62
+ $ model = $ model ->fresh ();
63
+
64
+ $ this ->assertEquals (
65
+ [
66
+ 'name ' => 'Taylor ' ,
67
+ 'age ' => 34 ,
68
+ 'meta ' => ['title ' => 'Developer ' ],
69
+ ],
70
+ $ model ->array_object ->toArray ()
71
+ );
72
+
73
+ $ this ->assertEquals (
74
+ [
75
+ 'name ' => 'Taylor ' ,
76
+ 'age ' => 34 ,
77
+ 'meta ' => ['title ' => 'Developer ' ],
78
+ ],
79
+ $ model ->array_object_json ->toArray ()
80
+ );
81
+ }
82
+
83
+ public function test_custom_casting_nullable_values ()
84
+ {
85
+ $ model = new TestEloquentModelWithCustomCastsNullable ();
86
+
87
+ $ model ->array_object = null ;
88
+ $ model ->array_object_json = null ;
89
+ $ model ->collection = collect ();
90
+ $ model ->stringable = null ;
91
+
92
+ $ model ->save ();
93
+
94
+ $ model = $ model ->fresh ();
95
+
96
+ $ this ->assertEmpty ($ model ->array_object );
97
+ $ this ->assertEmpty ($ model ->array_object_json );
98
+ $ this ->assertEmpty ($ model ->collection );
99
+ $ this ->assertSame ('' , (string ) $ model ->stringable );
100
+
101
+ $ model ->array_object = ['name ' => 'John ' ];
102
+ $ model ->array_object ['name ' ] = 'Taylor ' ;
103
+ $ model ->array_object ['meta ' ]['title ' ] = 'Developer ' ;
104
+
105
+ $ model ->array_object_json = ['name ' => 'John ' ];
106
+ $ model ->array_object_json ['name ' ] = 'Taylor ' ;
107
+ $ model ->array_object_json ['meta ' ]['title ' ] = 'Developer ' ;
108
+
45
109
$ model ->save ();
46
110
47
111
$ model = $ model ->fresh ();
48
112
49
- $ this ->assertEquals ([
50
- 'name ' => 'Taylor ' ,
51
- 'age ' => 34 ,
52
- 'meta ' => ['title ' => 'Developer ' ],
53
- ], $ model ->array_object ->toArray ());
113
+ $ this ->assertEquals (
114
+ [
115
+ 'name ' => 'Taylor ' ,
116
+ 'meta ' => ['title ' => 'Developer ' ],
117
+ ],
118
+ $ model ->array_object ->toArray ()
119
+ );
120
+
121
+ $ this ->assertEquals (
122
+ [
123
+ 'name ' => 'Taylor ' ,
124
+ 'meta ' => ['title ' => 'Developer ' ],
125
+ ],
126
+ $ model ->array_object_json ->toArray ()
127
+ );
54
128
}
55
129
}
56
130
@@ -70,6 +144,29 @@ class TestEloquentModelWithCustomCasts extends Model
70
144
*/
71
145
protected $ casts = [
72
146
'array_object ' => AsArrayObject::class,
147
+ 'array_object_json ' => AsArrayObject::class,
148
+ 'collection ' => AsCollection::class,
149
+ 'stringable ' => AsStringable::class,
150
+ ];
151
+ }
152
+
153
+ class TestEloquentModelWithCustomCastsNullable extends Model
154
+ {
155
+ /**
156
+ * The attributes that aren't mass assignable.
157
+ *
158
+ * @var string[]
159
+ */
160
+ protected $ guarded = [];
161
+
162
+ /**
163
+ * The attributes that should be cast to native types.
164
+ *
165
+ * @var array
166
+ */
167
+ protected $ casts = [
168
+ 'array_object ' => AsArrayObject::class,
169
+ 'array_object_json ' => AsArrayObject::class,
73
170
'collection ' => AsCollection::class,
74
171
'stringable ' => AsStringable::class,
75
172
];
0 commit comments