Reputation: 1203
I'm currently working on Bitcoin Payment Bundle for Symfony2. You can view it on my github. It is not (yet, I hope) fully test-driven developed but I try to make it as test-covered as possible.
Bundle contains the AbstractCommand
class that implements the CommandInterface
interface. Because it is an abstract class, it doesn't need to implement every of interface's methods. Great.
Next, I have the AbstractCommandTest
test case. It tests that validateParameters
method works well. Test covers every line of the class. But...when I run test-coverage analysis on my IDE (IntelliJ Idea with PHP Plugin) it shows that coverage is magic 93%. This is doubly strange because no line is marked with red color (I've changed default color scheme, so I see it well ;) ).
Question is: why? Is it PHPUnit bug? Or maybe it is class-model construction issue? When I had abstract methods inside AbstractCommand (and no interface) they were also not covered.
Upvotes: 1
Views: 961
Reputation: 1203
I've resolved that issue when looked into the clover coverage file. It shows:
<file name="/srv/bundles-src/payment-bitcoin/Bitcoin/Client/Command/AbstractCommand.php">
<class name="AbstractCommand" namespace="Wikp\Payment\BitcoinBundle\Bitcoin\Client\Command">
<metrics methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="13" coveredstatements="13" elements="15" coveredelements="15"/>
</class>
<line num="0" type="stmt" count="0"/>
<!-- coverage data for methods -->
<line num="36" type="stmt" count="0"/>
<metrics loc="35" ncloc="35" classes="1" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="13" coveredstatements="13" elements="15" coveredelements="15"/>
</file>
When you look at the AbstractCommand file, line 36 is a blank line after the class definition. Removing it causes the coverage to be 100%. I think this is the IntelliJ's/PHPStorm's issue.
Upvotes: 0
Reputation: 11374
As I can see, You probably didn't cover the catch
statement from validateParameters
method.
Try make your ParameterBug
argument as mock in order to throw an \InvalidArgumentException
exception in another test. This should cover what you want (but in order to make it possible you have to change your validateName
method to protected):
$parameterSetMock = $this->getMock('AppropraiteNamespace\ParameterSet', array('validateName'));
$parameterSetMock->expects($this->any())
->method('validateName')
->will($this->throwException(new \InvalidArgumentException);
One more thing:
You don't have use try/catch block in your tests. If you use:
$this->setExpectedException('ExpectedException', 'Expected Message');
at the beginning of your test, test will fail if the expected exception won't be thrown.
Upvotes: 2