Skip to content

Commit 418a0aa

Browse files
committed
protected prop
1 parent 1295255 commit 418a0aa

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Traits/Kabsa.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
trait Kabsa
99
{
10-
static $kabsaCollection;
10+
protected static $kabsaCollection;
1111

1212
public function getRows()
1313
{

tests/KabsaTest.php

+30-4
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,27 @@ public function first_test()
4646
/** @test * */
4747
public function static_var()
4848
{
49+
$collection = $this->getOrSetProperty(Role::class, 'kabsaCollection');
50+
51+
$this->assertEmpty($collection);
52+
4953
$this->assertEquals([
5054
['label' => 'admin'],
5155
['label' => 'manager'],
5256
['label' => 'user']
5357
], Role::all()->toArray());
5458

59+
$collection = $this->getOrSetProperty(Role::class, 'kabsaCollection');
5560

5661
$this->assertEquals([
5762
['label' => 'admin'],
5863
['label' => 'manager'],
5964
['label' => 'user']
60-
], Role::$kabsaCollection->toArray());
65+
], $collection->toArray());
6166

6267

63-
Role::$kabsaCollection = collect([['static']]);
68+
//set something else
69+
$this->getOrSetProperty(Role::class, 'kabsaCollection', collect([['static']]));
6470

6571
//one
6672
$this->assertEquals([
@@ -72,19 +78,39 @@ public function static_var()
7278
['static']
7379
], Role::all()->toArray());
7480

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

7783
$this->assertEquals([
7884
['label' => 'admin'],
7985
['label' => 'manager'],
8086
['label' => 'user']
8187
], Role::all()->toArray());
8288

89+
$collection = $this->getOrSetProperty(Role::class, 'kabsaCollection');
90+
91+
8392
$this->assertEquals([
8493
['label' => 'admin'],
8594
['label' => 'manager'],
8695
['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);
88114
}
89115

90116
/**

0 commit comments

Comments
 (0)