diff --git a/src/Traits/Kabsa.php b/src/Traits/Kabsa.php index 44e2fe5..a413506 100755 --- a/src/Traits/Kabsa.php +++ b/src/Traits/Kabsa.php @@ -7,7 +7,7 @@ trait Kabsa { - static $kabsaCollection; + protected static $kabsaCollection; public function getRows() { diff --git a/tests/KabsaTest.php b/tests/KabsaTest.php index 1762535..79e958c 100755 --- a/tests/KabsaTest.php +++ b/tests/KabsaTest.php @@ -46,21 +46,27 @@ public function first_test() /** @test * */ public function static_var() { + $collection = $this->getOrSetProperty(Role::class, 'kabsaCollection'); + + $this->assertEmpty($collection); + $this->assertEquals([ ['label' => 'admin'], ['label' => 'manager'], ['label' => 'user'] ], Role::all()->toArray()); + $collection = $this->getOrSetProperty(Role::class, 'kabsaCollection'); $this->assertEquals([ ['label' => 'admin'], ['label' => 'manager'], ['label' => 'user'] - ], Role::$kabsaCollection->toArray()); + ], $collection->toArray()); - Role::$kabsaCollection = collect([['static']]); + //set something else + $this->getOrSetProperty(Role::class, 'kabsaCollection', collect([['static']])); //one $this->assertEquals([ @@ -72,7 +78,7 @@ public function static_var() ['static'] ], Role::all()->toArray()); - Role::$kabsaCollection = []; + $this->getOrSetProperty(Role::class, 'kabsaCollection', null); $this->assertEquals([ ['label' => 'admin'], @@ -80,11 +86,31 @@ public function static_var() ['label' => 'user'] ], Role::all()->toArray()); + $collection = $this->getOrSetProperty(Role::class, 'kabsaCollection'); + + $this->assertEquals([ ['label' => 'admin'], ['label' => 'manager'], ['label' => 'user'] - ], Role::$kabsaCollection->toArray()); + ], $collection->toArray()); + } + + + public function getOrSetProperty($object, $name, $value = false) + { + $reflected = new \ReflectionClass($object); + + $property = $reflected->getProperty($name); + + $property->setAccessible(true); + + if($value !== false) { + $property->setValue($object, $value); + return true; + } + + return $property->getValue($object); } /**