Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Code coverage discrepancy using getObjectForTrait #478

Closed
rdlowrey opened this Issue Feb 1, 2012 · 1 comment

Comments

Projects
None yet
2 participants

rdlowrey commented Feb 1, 2012

When using getObjectForTrait PHPUnit reports a different code coverage % for the same test when run in isolation as compared to when the entire test suite is run. So, when I run only the specific test in question using phpunit test/src/artax/UsesRequestTraitTest I get the following coverage:

isolation

However, if I run my full suite of tests using phpunit without specifying a specific test, I receive the following for the same file:

full_suite

Here's the source code for the test:

<?php

class UsesRequestTraitTest extends PHPUnit_Framework_TestCase
{
  public function testIsInitiallyEmpty()
  {
    $traitObj = $this->getObjectForTrait('artax\UsesRequestTrait');
    $this->assertAttributeEmpty('request', $traitObj);
    return $traitObj;
  }

  /**
   * @depends testIsInitiallyEmpty
   * @covers artax\UsesRequestTrait::setRequest
   * @covers artax\UsesRequestTrait::getRequest
   */
  public function testSetterAssignsRequestProperty($traitObj)
  {
    $r = new artax\blocks\http\HttpRequest();
    $traitObj->setRequest($r);
    $this->assertEquals($r, $traitObj->getRequest());
  }
}

And the actual graphical coverage report for the 60% coverage version. The lines shown as uncovered below are green (covered) when the test is run in isolation:

coverage


However, the discrepancy disappears when using a concrete implementation class for testing the trait in place of the built-in getObjectForTrait method. So, the following changes to the above code resolve the issue:

<?php

class UsesRequestTraitTest extends PHPUnit_Framework_TestCase
{
  public function testIsInitiallyEmpty()
  {
    // trait code coverage not reported correctly using getObjectForTrait
    //$traitObj = $this->getObjectForTrait('artax\UsesRequestTrait');

    // use a concrete implementation instead (see bottom for class definition)
    $traitObj = new TraitImplementationClass();
    $this->assertAttributeEmpty('request', $traitObj);
    return $traitObj;
  }

  /**
   * @depends testIsInitiallyEmpty
   * @covers artax\UsesRequestTrait::setRequest
   * @covers artax\UsesRequestTrait::getRequest
   */
  public function testSetterAssignsRequestProperty($traitObj)
  {
    $r = new artax\blocks\http\HttpRequest();
    $traitObj->setRequest($r);
    $this->assertEquals($r, $traitObj->getRequest());
  }
}

// using this class to test instead of getObjectForTrait resolves the issue
class TraitImplementationClass
{
  use artax\UsesRequestTrait;
}

The environment used is PHP_CodeCoverage 1.1.1 using PHP 5.4.0RC6 and PHPUnit 3.6.10

Collaborator

edorian commented Sep 24, 2012

I've cooked up a small reproduce using CodeCoverage 1.2, PHP 5.4.7 and PHPUnit 3.7 and the issue seems to be fix.

If it doesn't work for you let me know. Maybe give me a complete reproduce if possible or point me to your lib where the issue happens :)

@edorian edorian closed this Sep 24, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment