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.

share|improve this question
feedback

2 Answers

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.

share|improve this answer
I'll check that soon and write about results. Despite of them - thanks for the reply! I try...catch an exception because I want to check whether Exception's parameters are valid. setExpectedException doesn't allow such check (afaik). – wikp Dec 4 '12 at 17:44
I've checked that and...no, catch statement, as well as whole AbstractCommand class, was covered. There were no "red lines" in my IDE when I ran it with coverage. – wikp Dec 5 '12 at 15:55
I don't know if you've changed something since last two days, but I've just cloned your project and run unit test. My coverage shows that AbstractCommand is 100% covered.. PHP 5.4.9, PHPUnit 3.7.1, XDebug 2.2.1 – Cyprian Dec 7 '12 at 23:06
I didn't change anything because I must suspend work on that for a while. But when I had been creating another bundle, I fell into similar issue - resolution in answer below. Thanks for concerning, @Cyprian. – wikp Dec 16 '12 at 18:51
feedback
up vote 0 down vote accepted

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.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.