Programmatic @covers annotations #18

wants to merge 5 commits into


None yet
3 participants

I wasn't sure if you saw these commits with my one from php-code-coverage or not. This feature allows developers to specify additional @Covers annotations in PHP code to be merged with those in the docblock. It's very helpful for tests that cover files, e.g. .phtml view scripts.

dharkness added some commits Aug 13, 2010

@dharkness dharkness Added support for programmatically adding @covers annotations.
 * Added TestCase::addCoveredElement() and getCoveredElements().
 * Moved test case template methods to TestCase class in PHPUnit.

Note: This commit relies on a commit in PHP_CodeCoverage.
@dharkness dharkness Removed setUpBeforeClass and tearDownAfterClass from template methods…
… since they

are called outside of the code coverage tracking.
@dharkness dharkness Merged latest from upstream and fixed merge conflict. 0c62587

While I think that @coversFile is worth looking into, I don't see a point in programmatically adding covered elements. Won't pull, sorry.

We use Zend Framework's MVC for our site which involves having a .phtml file for each view separate from the .php controller. It took a lot of effort to convince the team to switch from SimpleTest to PHPUnit because we had to rewrite the existing tests, but I think it's paid off. I created three abstract subclasses of TestCase, each designed to ease testing of the different types of objects (controllers, views, and pages for sanity tests).

@coversfile is needed to covers the .phtml files since they don't define classes. They are designed to be included inline into the base view class's render() method. Since my abstract class already locates the view script file from the test case's class name, programmatic specification of @coversfile would remove the chance of pointing to the wrong file or needed to hard-code the base directory in the source code.

A side benefit of my implementation is that it moved knowledge of PHPUnit_Framework_TestCase's template method names from PHP_CodeCoverage_Util to the TestCase class.

It's funny, I came across this the just other day and now I find myself in a situation where having programmatic @Covers would be very useful.

See and the related base class at for an example of where this would be really useful.

I have a situation where I will have around 20-30 concrete subclasses of an abstract 'Type' class.

All of the subclasses need to be tested in the same way, but with different sets of data, so I created a custom TestCase base class, and I can simply override the data providers in my concrete test case.

But there is no way to change what PHPUnit considers 'covered' without overriding the test methods in every single one of these 'Type' test cases.

Gist of above example:

You can at least specify the @Covers at the class level to save yourself some headaches. I've started doing this regularly as covering at the method level has proven to be too much of a PITA. You need to take superclasses into consideration, update them as you refactor, make sure you don't fail when copy-n-pasting, etc. Rarely do I want to say "No, even though you can executing this code during a test, don't consider it tested."

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