Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit - @covers tag not working

Tags:

php

phpunit

Maybe I missed out on a detail, but I wrote a small test case for a singleton _clone method yet it is not showing up as tested in the code coverage report.

 /**
  *   @covers ErrorHandling::__clone
  */
  public function test__cloneNotCloneable(){
    $class = new ReflectionClass('ErrorHandling');
    $method = $class->getMethod('__clone');

    self::assertTrue($method->isFinal(), '__clone method is not final.');
    self::assertTrue($method->isPrivate(), '__clone method is not private.');
 }

The __clone method is the usual private/final __clone() of a regular (evil) singleton.

  /**
   *  Define __clone as final and private to dissallow cloning.
   */
   private final function  __clone(){}

I know it's probably overkill testing for this, but the code-coverage report is sort of the graphical representation of a job 'well done'. Is there a way to have this method marked as covered in the code coverage report?

like image 856
stefgosselin Avatar asked May 19 '11 06:05

stefgosselin


1 Answers

The @covers tag tells PHPUnit that you intend to test the named method; it does not mark the method as having been tested. Since you cannot call the method, Xdebug won't tell PHPUnit that its code has been executed, and it will never be covered in your report.

Your best bet is to tell PHPUnit to ignore the method in the report by using the @codeCoverageIgnore docblock tag.

/**
 * Define __clone as final and private to dissallow cloning.
 *
 * @codeCoverageIgnore
 */
private final function __clone() { }

You can ignore any range of lines by enclosing them in a pair of one-line start/stop comments.

// @codeCoverageIgnoreStart
private final function __clone() { }
// @codeCoverageIgnoreEnd

Finally, you can ignore a single line by adding a one-line comment to it.

private final function __clone() { }  // @codeCoverageIgnore
like image 199
David Harkness Avatar answered Oct 11 '22 04:10

David Harkness