Simpler code coverage functionality

Hi,

I read the blog about PHPUnit code coverage in PHPstorm 4 and at first i was very exciting about it. But when I tried to get it working i found out that it was not really what i was looking for.
First of all i find it very complicated to get it working, i could not find any good tutorials about phpunit and phpstorm. Second i think phpunit (as far as i understand it) is much to complicated for what i was hoping it would do.

I do my PHP testing inside the browser. I run a page and try out a few things and observe the results. The problem is that it is very hard to see if i have tested (covered) all my code. It would be nice if all lines that were executed would be colored green so that i know that part of the code has been tested.

For example i have an index page like index.php with the following content:

<?php

$test = $_GET['test'];

if ($test == 1) {
  echo "Hello";
}

if ($test == 2) {
  echo "World";
}
?>

I would test this script in the browser. Type in: index.php?test=1 and check if the output is Hello. If not I know that something went wrong. If I look in the editor I see all the lines colored green except the echo "World"; line. Now I know i forget to test this part of the code. So I open the browser and type in index.php?test=2 and now all my lines are green. Telling me all my code has atleast runned once.

This is much simpler then PHPunit and works for me better. I dont know if this could also be achieved with PHPunit (if so please tell me :-) ).

- Roland

5 comments
Comment actions Permalink

Hi Roland,

"Code coverage", and "PHPUnit code coverage" are 2 different things. If you do not know what PHPUnit is and how it works then I suggest to start here:

  1. http://en.wikipedia.org/wiki/PHPUnit
  2. http://www.phpunit.de/manual/current/en/index.html


In short: "The goal of unit testing is to isolate each part of the program and show  that the individual parts are correct. A unit test provides a strict,  written contract that the piece of code must satisfy. As a result, unit  tests find problems early in the development cycle."

The way how you are trying to test your code (whole page at once, that relays on GET/POST parameters etc) is different to unit testing ideas (where you separate block of code into separate independent functions and pass all necessary parameters as variables).

For ordinary code coverage (so it can be applied to any code at all -- like your one) -- look for this ticket: http://youtrack.jetbrains.com/issue/WI-10289http://youtrack.jetbrains.com/issue/WI-10289

i could not find any good tutorials about phpunit and phpstorm

No need for special tutorials -- there is nothing extra special here at all. If you have your PHPUnit tests working from command line (using phpunit native command) then it will be a matter of couple of minutes (or less) to integrate it with PhpStorm (just create run configuration and press a button).

0
Comment actions Permalink

Allright PHPunit is not entirely what i thought it was. But i still believe that a simple code coverage functionality can really help in finding bugs.

I also think you misunderstood my example. I dont limit my testing by GET and POST. That was just an example. I write code, go to the browser and execute the page.
Then i click at some buttons, fill in some forms, post the data or wait for ajax responses and observe the results. If the results are what i expected then i assume their
are no bugs. This is not waterproof testing but its enough most of the times.

After testing I want to see in PHPstorm which lines have never been triggered (executed/runned). For example because i forget to test if 'delete' also works (i never pressed that button on the webpage) a part of my code (lines) have never been executed and so can still contain bugs. I just love to have an option to see which part of my code has never been executed so that i know that i still have to test that part.

0
Comment actions Permalink

No -- I perfectly understand what you want and how you test. You test whole web page (or part of it, depends on parameter, action) in live environment. This is different to what PHPUnit does (primary function at least -- testing individual / independent parts in much more controlled environment). This does not mean that PHPUnit cannot be used for testing using different approaches (e.g. Selenium tests), but I have not been using it myself and therefore cannot really comment on it.

In any case -- plain code coverage is not available in current version of PhpStorm. That ticket (or something very similar to that, in case it is meant for PHPUnit only) is what you need to watch/vote/comment/etc for -- http://youtrack.jetbrains.com/issue/WI-10289

0
Comment actions Permalink

Roland,

You are talking about the generic code coverage and it is not supported by Xdebug. The only way now to see coverage reports with Xdebug is to use PHPUnit.

Thank you for feedback!
0
Comment actions Permalink

Andriy,

Please note that http://youtrack.jetbrains.com/issue/WI-10289 is about PHPUnit code coverage too. Xdebug doesn't support collecting coverage reports for arbitrary requests to the Web Server.

Thank you for assistance!:)

0

Please sign in to leave a comment.