1

Currently I try to integrate PHPUnit in my project. To ensure a 100% test coverage over time, I want to check if all methods that exist in the class to be tested in the testclass. So I thought I could write something like

class MyClassTest extends PHPUnit_Framework_TestCase {

    private function _getClassFunctions($class) {
        $class = new ReflectionClass($class);
        return $class->getMethods();
    }

    public function testCompareFunctionCount() {
        $this->assertEquals($this->_getClassFunctions('MyClass'), $this->_getClassFunctions(__CLASS__));
    }
}

It seems, however, that ReflectionClass::getMethods() counts not only the methods of the class itself, but also all of the extended classes. Is there a way to prevent this behaviour? Or did I get something totally wrong? I read in older articles that ReflectionClass::getMethods() doesn't work correctly on older PHP-Versions, but I thought it might be fixed by now (those articles are 4+ years old...)
I use PHP 5.4.5.

1 Answer 1

2

You must do some of the work yourself :

private function _getClassFunctions($className) {
   $class = new ReflectionClass($className);
   $result = array();
   foreach($class->getMethods() as $method) {
      if ($method->class == $className) {
         $result[]=$method;
      }
   }
   return $result;
}

Comparing $method->class to $className narrow down the result to methods contained in the class in question only.

Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what I needed. Thank you very much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.