10
10
use Kodeine \Metable \Tests \Models \UserTest ;
11
11
use Illuminate \Database \Capsule \Manager as Capsule ;
12
12
use Illuminate \Database \Eloquent \ModelNotFoundException ;
13
+ use Kodeine \Metable \Tests \Casts \UserState \DefaultState ;
13
14
14
15
class MetableTest extends TestCase
15
16
{
@@ -31,6 +32,8 @@ public static function setUpBeforeClass(): void {
31
32
$ table ->string ( 'name ' )->default ( 'john ' );
32
33
$ table->
string (
'email ' )->
default (
'[email protected] ' );
33
34
$ table ->string ( 'password ' )->nullable ();
35
+ $ table ->string ( 'state ' )->nullable ();
36
+ $ table ->string ( 'null_value ' )->nullable ();
34
37
$ table ->integer ( 'user_test_id ' )->unsigned ()->nullable ();
35
38
$ table ->foreign ( 'user_test_id ' )->references ( 'id ' )->on ( 'user_tests ' );
36
39
$ table ->timestamps ();
@@ -47,6 +50,16 @@ public static function setUpBeforeClass(): void {
47
50
} );
48
51
}
49
52
53
+ public function testCast () {
54
+ $ user = new UserTest ;
55
+
56
+ $ this ->assertNull ( $ user ->state , 'Casted object should be null by default ' );
57
+
58
+ $ user ->state = DefaultState::class;
59
+
60
+ $ this ->assertInstanceOf ( DefaultState::class, $ user ->state , 'Casted object should be instanceof DefaultState ' );
61
+ }
62
+
50
63
public function testFluentMeta () {
51
64
$ user = new UserTest ;
52
65
@@ -112,6 +125,21 @@ public function testFluentMeta() {
112
125
$ this ->assertTrue ( $ user ->isMetaDirty ( 'foo ' , 'bar ' ), 'isMetaDirty should return true even if one of metas has changed ' );
113
126
$ this ->assertTrue ( $ user ->isMetaDirty ( 'foo,bar ' ), 'isMetaDirty should return true even if one of metas has changed ' );
114
127
128
+ //re retrieve user from database
129
+ /** @var UserTest $user */
130
+ $ user = UserTest::find ( $ user ->id );
131
+
132
+ $ this ->assertNull ( $ user ->null_value , 'null_value property should be null ' );
133
+ $ this ->assertNull ( $ user ->null_cast , 'null_cast property should be null ' );
134
+
135
+ $ user ->setMeta ( 'null_value ' , true );
136
+ $ user ->setMeta ( 'null_cast ' , true );
137
+
138
+ $ this ->assertTrue ( $ user ->getMeta ( 'null_value ' ), 'Meta should be set ' );
139
+ $ this ->assertTrue ( $ user ->getMeta ( 'null_cast ' ), 'Meta should be set ' );
140
+ $ this ->assertNull ( $ user ->null_value , 'null_value property should be null ' );
141
+ $ this ->assertNull ( $ user ->null_cast , 'null_cast property should be null ' );
142
+
115
143
$ user ->delete ();
116
144
117
145
$ this ->assertEquals ( 0 , $ metaData ->count (), 'Meta should be deleted from database after deleting user. ' );
@@ -227,6 +255,10 @@ public function testMetaMethods() {
227
255
228
256
$ user ->save ();
229
257
258
+ $ meta = $ user ->getMeta ();
259
+ $ this ->assertInstanceOf ( 'Illuminate\Support\Collection ' , $ meta , 'Meta method getMeta is not typeof Collection ' );
260
+ $ this ->assertTrue ( $ meta ->isNotEmpty (), 'Meta method getMeta did return empty collection ' );
261
+
230
262
// re retrieve user to make sure meta is saved
231
263
$ user = UserTest::with ( ['metas ' ] )->find ( $ user ->getKey () );
232
264
0 commit comments