Search code examples
phpphpunitabstract-classcode-coveragexdebug

PHPUnit code coverage for class that extends abstract class


I have an abstract class Exception in custom namespace which has no abstract methods. All methods are shown as covered by its tests (mocked in a standard way). The reason to make it abstract is unimportant.

Then I have about 10 classes that extend Exception but don't add or override any methods, properties, constants, etc. It is obvious that all of them should be covered too, but they are shown as not fully covered. I read the docs and googled for a while but didn't find the answer.

  • PHP_CodeCoverage 1.1.3
  • PHPUnit 3.6.12
  • PHP 5.4.4
  • Xdebug 2.2.1-5.4-vc9

While annotating with @codeCoverageIgnore is a solution I want to find out why it happens this way rather then get 100% coverage.

UPD: sources.

------------- abstract class ----------

namespace Jade\Core\Base;


abstract class Exception extends \Exception {
    /**
     * Additional info for exception
     * @var array|null
     */
    protected $info;

    public function __construct($message, array $info = null, $code = 0, \Exception $previous = null) {
        parent::__construct($message, $code, $previous);
        $this->info = $info;
    }

    /**
     * Get the Exception additional info
     * @return mixed
     */
    public function getInfo() {
        return $this->info;
    }
}

------------- Tests for abstract class ----------

class ExceptionTest extends \PHPUnit_Framework_TestCase {
    /**
     * @covers \Jade\Core\Base\Exception
     *
     * @group  public
     */
    public function testGetInfo() {
        $info = array('info' => 'test');
        $stub = $this->getMockForAbstractClass('\Jade\Core\Base\Exception', array('message', $info));

        $this->assertEquals($info, $stub->getInfo());
    }

    /**
     * @covers \Jade\Core\Base\Exception
     *
     * @group  public
     */
    public function testConstructWithDefaultInfo() {
        $stub = $this->getMockForAbstractClass('\Jade\Core\Base\Exception', array('message'));
        $this->assertEmpty($stub->getInfo());
    }
}

------------- class that extends abstract class ----------

namespace Jade\Core\Exceptions;


class Dict extends \Jade\Core\Base\Exception {
}

Code coverage tool output

Update

Renaming \Jade\Core\Base\Exception to avoid possible collision with \Exception didn't help (i tried \Jade\Core\Base\Ixception).

Making Exception class a regular class, not abstract also didn't help.


Solution

  • The problem was entirely in class autoloading. To get work i had to manually include files with classes sources in bootstrap file.