@@ -46,21 +46,27 @@ public function first_test()
46
46
/** @test * */
47
47
public function static_var ()
48
48
{
49
+ $ collection = $ this ->getOrSetProperty (Role::class, 'kabsaCollection ' );
50
+
51
+ $ this ->assertEmpty ($ collection );
52
+
49
53
$ this ->assertEquals ([
50
54
['label ' => 'admin ' ],
51
55
['label ' => 'manager ' ],
52
56
['label ' => 'user ' ]
53
57
], Role::all ()->toArray ());
54
58
59
+ $ collection = $ this ->getOrSetProperty (Role::class, 'kabsaCollection ' );
55
60
56
61
$ this ->assertEquals ([
57
62
['label ' => 'admin ' ],
58
63
['label ' => 'manager ' ],
59
64
['label ' => 'user ' ]
60
- ], Role:: $ kabsaCollection ->toArray ());
65
+ ], $ collection ->toArray ());
61
66
62
67
63
- Role::$ kabsaCollection = collect ([['static ' ]]);
68
+ //set something else
69
+ $ this ->getOrSetProperty (Role::class, 'kabsaCollection ' , collect ([['static ' ]]));
64
70
65
71
//one
66
72
$ this ->assertEquals ([
@@ -72,19 +78,39 @@ public function static_var()
72
78
['static ' ]
73
79
], Role::all ()->toArray ());
74
80
75
- Role::$ kabsaCollection = [] ;
81
+ $ this -> getOrSetProperty ( Role::class, ' kabsaCollection ' , null ) ;
76
82
77
83
$ this ->assertEquals ([
78
84
['label ' => 'admin ' ],
79
85
['label ' => 'manager ' ],
80
86
['label ' => 'user ' ]
81
87
], Role::all ()->toArray ());
82
88
89
+ $ collection = $ this ->getOrSetProperty (Role::class, 'kabsaCollection ' );
90
+
91
+
83
92
$ this ->assertEquals ([
84
93
['label ' => 'admin ' ],
85
94
['label ' => 'manager ' ],
86
95
['label ' => 'user ' ]
87
- ], Role::$ kabsaCollection ->toArray ());
96
+ ], $ collection ->toArray ());
97
+ }
98
+
99
+
100
+ public function getOrSetProperty ($ object , $ name , $ value = false )
101
+ {
102
+ $ reflected = new \ReflectionClass ($ object );
103
+
104
+ $ property = $ reflected ->getProperty ($ name );
105
+
106
+ $ property ->setAccessible (true );
107
+
108
+ if ($ value !== false ) {
109
+ $ property ->setValue ($ object , $ value );
110
+ return true ;
111
+ }
112
+
113
+ return $ property ->getValue ($ object );
88
114
}
89
115
90
116
/**
0 commit comments