Skip to content

Commit

Permalink
protected prop
Browse files Browse the repository at this point in the history
  • Loading branch information
BSN4 committed Mar 30, 2020
1 parent 1295255 commit 418a0aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Traits/Kabsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait Kabsa
{
static $kabsaCollection;
protected static $kabsaCollection;

public function getRows()
{
Expand Down
34 changes: 30 additions & 4 deletions tests/KabsaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -72,19 +78,39 @@ public function static_var()
['static']
], Role::all()->toArray());

Role::$kabsaCollection = [];
$this->getOrSetProperty(Role::class, 'kabsaCollection', null);

$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());
}


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);
}

/**
Expand Down

0 comments on commit 418a0aa

Please sign in to comment.