Skip to content

Instantly share code, notes, and snippets.

@ishtaka
Created April 22, 2015 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishtaka/9e0ef5df9a31683b31d6 to your computer and use it in GitHub Desktop.
Save ishtaka/9e0ef5df9a31683b31d6 to your computer and use it in GitHub Desktop.
[PHP]private, protectedなメソッド、プロパティのテスト用
<?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