You Are Here: Home Documentation Method Coverage

Enhance PHP Unit Testing And Mocking Framework improve the quality

Method Coverage

Enhance PHP contains simple method coverage reporting for those who want to use it. Method coverage reporting simply displays the methods in classes under test and how many times each has been called. This helps to identify methods that have no coverage and tests that have little coverage.

All you need to do to get method coverage reports is use the built in proxy, which is called a code coverage logger, to call your target methods.

This is the test fixture before we use the proxy:

class ExampleClassTests extends \Enhance\TestFixture
{
        public function addTwoNumbersWith3and2Expect5Test()
        {
                $target = new ExampleClass();
                $result = $target->addTwoNumbers(3, 2);
                \Enhance\Assert::areIdentical(5, $result);
        }
        
        public function addTwoNumbersWith4and2Expect6Test()
        {
                $target = new ExampleClass();
                $result = $target->addTwoNumbers(4, 2);
                \Enhance\Assert::areIdentical(6, $result);
        }
}

And this is the test fixture using the proxy, which logs the calls and compiles the method coverage statistics.

class ExampleClassTests extends \Enhance\TestFixture
{
        public function addTwoNumbersWith3and2Expect5Test()
        {
                $target = \Enhance\Core::getCodeCoverageWrapper('ExampleClass');
                $result = $target->addTwoNumbers(3, 2);
                \Enhance\Assert::areIdentical(5, $result);
        }
        
        public function addTwoNumbersWith4and2Expect6Test()
        {
                $target = \Enhance\Core::getCodeCoverageWrapper('ExampleClass');
                $result = $target->addTwoNumbers(4, 2);
                \Enhance\Assert::areIdentical(6, $result);
        }
}

As you can see, it is almost no additional effort and you get a report of your method coverage in return. The method coverage reports will pick up on all methods that are in the same class as a method being tested.

If you need to pass arguments to the constructor of the class, you simply pass them to the proxy as an array, like this:

$target = \Enhance\Core::getCodeCoverageWrapper('ExampleClassWithConstructor', array(1, 'ArgumentTwo'));

Sample Output

Method Coverage