Skip to content

Commit

Permalink
Merge pull request #130 from tripal/122_fix-reflectory
Browse files Browse the repository at this point in the history
Fix 122 and add tests
  • Loading branch information
bradfordcondon authored Dec 4, 2018
2 parents 04eb736 + 6465753 commit c918684
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
21 changes: 11 additions & 10 deletions src/Services/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class Reflector
*/
public function __construct($class)
{
$this->reflection = new \ReflectionClass($class);

$this->object = $class;

$this->reflection = new \ReflectionClass($this->object);
}

/**
* @param $name
* @param $arguments
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
public function __call($name, array $arguments)
{
$method = $this->reflection->getMethod($name);
$method->setAccessible(true);
Expand All @@ -41,7 +41,7 @@ public function __call($name, $arguments)
}

/**
* @param $name
* @param string $name
* @return mixed
*/
public function __get($name)
Expand All @@ -53,14 +53,15 @@ public function __get($name)
}

/**
* @param $name
* @param $value
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set($name, $value)
{
$property = $this->reflection->getProperty($name);
$property->setAccessible(true);

return $property->setValue($value);
$property->setValue($this->object, $value);
}
}
43 changes: 29 additions & 14 deletions tests/Feature/ReflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,70 @@ class ReflectorTest extends TestCase
*/
public function testMakingPrivateMethodsAccessible()
{
$privateClass = reflect(new PrivateClass());
$this->assertEquals('private', $privateClass->myPrivate());
$private = reflect(new PrivateClass());
$this->assertEquals('private', $private->myPrivate());
}

/**
* @throws \Exception
*/
public function testMakingProtectedMethodsAccessible()
{
$privateClass = reflect(new PrivateClass());
$this->assertEquals('protected', $privateClass->myProtected());
$private = reflect(new PrivateClass());
$this->assertEquals('protected', $private->myProtected());
}

/**
* @throws \Exception
*/
public function testMakingPublicMethodsAccessible()
{
$privateClass = reflect(new PrivateClass());
$this->assertEquals('public', $privateClass->myPublic());
$private = reflect(new PrivateClass());
$this->assertEquals('public', $private->myPublic());
}

/**
* @throws \Exception
*/
public function testMakingPrivateMethodsWithArgsAccessible()
{
$privateClass = reflect(new PrivateClass());
$this->assertEquals('arg1 arg2', $privateClass->privateWithArgs('arg1', 'arg2'));
$private = reflect(new PrivateClass());
$this->assertEquals('arg1 arg2', $private->privateWithArgs('arg1', 'arg2'));
}

/**
* @throws \Exception
*/
public function testAccessingPrivateProperties() {
$privateClass = reflect(new PrivateClass());
$this->assertEquals('private', $privateClass->private);
$private = reflect(new PrivateClass());
$this->assertEquals('private', $private->private);
}

/**
* @throws \Exception
*/
public function testAccessingProtectedProperties() {
$privateClass = reflect(new PrivateClass());
$this->assertEquals('protected', $privateClass->protected);
$private = reflect(new PrivateClass());
$this->assertEquals('protected', $private->protected);
}

/**
* @throws \Exception
*/
public function testAccessingPublicProperties() {
$privateClass = reflect(new PrivateClass());
$this->assertEquals('public', $privateClass->public);
$private = reflect(new PrivateClass());
$this->assertEquals('public', $private->public);
}

/**
* @throws \Exception
*/
public function testManipulatingPrivateProperties() {
$private = reflect(new PrivateClass());
$this->assertEquals('private', $private->getPrivate());
$private->private = 'not private';
$this->assertEquals('not private', $private->getPrivate());
$this->assertEquals('not private', $private->private);
}
}

Expand Down Expand Up @@ -101,4 +112,8 @@ private function privateWithArgs($one, $two)
{
return $one.' '.$two;
}

private function getPrivate() {
return $this->private;
}
}

0 comments on commit c918684

Please sign in to comment.