Created
April 22, 2015 01:10
-
-
Save ishtaka/9e0ef5df9a31683b31d6 to your computer and use it in GitHub Desktop.
[PHP]private, protectedなメソッド、プロパティのテスト用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait TestTrait | |
{ | |
/** | |
* private, protectedなmethodのテスト用 | |
* | |
* $method = $this->getAccessibleMethod($obj, 'method_name'); | |
* $method->invokeArgs($obj, array()); | |
* | |
* @param Object $obj :テストターゲット | |
* @param string $name:呼出したいメソッド | |
* | |
*/ | |
public function getAccessibleMethod($obj, $name) { | |
$obj_name = get_class($obj); | |
if (false === $obj_name) return false; | |
$reflection = new ReflectionClass($obj_name); | |
$method = $reflection->getMethod($name); | |
$method->setAccessible(true); | |
return $method; | |
} | |
public function getAccessibleProperty($obj, $name) { | |
$obj_name = get_class($obj); | |
if (false === $obj_name) return false; | |
$reflection = new ReflectionClass($obj_name); | |
$property = $reflection->getProperty($name); | |
$property->setAccessible(true); | |
return $property->getValue($obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment