2011年9月12日月曜日

phpのリフレクションを使って、privateメソッドのテストを行う方法


5.3.2になってPHPのReflectionMethodにsetAccessibleが実装されていた。
ということで、これでprivateメソッドのテストを簡単に実現することができます。


     public function testGetMaxFile()
     {
          $this->object = TargetClass::getInstance();
          $method = new ReflectionMethod("TargetClass", "targetMethod");
          $method->setAccessible(true);
          $this->assertEquals('expected',$method->invoke($this->object,"arg"));
     }

0 件のコメント:

コメントを投稿