PHPUnit and PHPStorm

Is there an "end to end" tutorial somewhere on using PHPStorm with PHPUnit?

My problem is two fold:

1) I have very little experience with PHPUnit
2) The instructions in the PHPStorm assume you know everthing the manual is talking about concerning the two.

I KNOW this is very newbie to most of you but I would really appreciate some help :)

Thanks in advance,

-John

6 comments
Comment actions Permalink

Hi John,

No, there is no such detailed manual that will teach you A-Z of PHPUnit + PhpStorm integration. If you do not know what PHPUnit for or how it works, you will have to learn it from examples (can be taken from official manual or from various online articles). The idea is -- if you know how PHPUnit works (basic concepts; how to write basic tests etc) then you will not have issues integrating with PhpStorm -- it's pretty straightforward (for local execution).

Some links:

0
Comment actions Permalink

Thank you Andriy.

I'm looking at some now and I will look at your links also.

We'll "get there" I'm sure.

Appreciate your help!

-John

0
Comment actions Permalink

I have created simple test files using the very simple and helpful tutorial: http://www.youtube.com/watch?v=Iq6wvboGU-A

It seems clear that I have PHPUnit installed and workng.  It works correctly from Terminal (I'm on a MAC running Snow Leopard).

Now doe running this al inside PHPStorm which I think is appropiate for this forum :)

So I have two very simple example files in my project:

person.php (the file to be tested consisting of)

<?php
    class person
    {
        public $name;

        public function __construct($name)
        {
            $this->name = $name;
        }

        public function getName()
        {
            return $this->name;
        }
    }
?>

And the PHPUnit test file:

<?php
require_once 'person.php';
     
class PersonTest extends PHPUnit_Framework_TestCase {
     public $test;
     
     public function setup() {
         $this->test = new Person('John');
     }
     
     public function testName() {
         $john = $this->test->getName();
         $this->assertTrue($john == 'John');
     }
}
?>

Looking at the Manual pages: http://www.jetbrains.com/phpstorm/webhelp/creating-run-debug-configuration-for-tests.html

This dialogue and set of steps seems to be there and work:

StormDocs-0.png

When I say work, I mean, I right clicked inside of the file in the code area and selected: "Run 'PersonTest' with Coverage.
I get the console for Run PersonTest with test results, The Run tab at the bottom has a pop-up in green saying "Tests passed".  I also get a window with Coverage Summary:  person.php shows Statistics, % of '100% lines'

I presume this is the intended way to run this and these are the desired results.

But this dialogue I can't seem to get to:

StormDocs-1.png

So for the first part, is this the correct way to do this and is this what we should use as the output for results?  And if I want to run test cases on all the files in a directory?  What do I do differently?

As for the second part, I am at a loss.

Thanks,

-John

0
Comment actions Permalink

Hi John,

I presume this is the intended way to run this and these are the desired results.

That's if you need code coverage data. If such data is not required -- choose approperiate option from "Create Run Configuration" or "Run" submenus (first one will only create Run/Debug Configuration of PHPUnit type while 2nd will create and execute it straight away). You can choose active Run/Debug configuration in drop-down box on Main Toolbar.

Obviously, there is no need to use "Run 'PersonTest' with Coverage" context menu all the time (although it is still ok -- all depends on your configuration and test requirements). Once Run/Debug Configuration entry is created just switch between them in toolbar and use appropriate buttons (next to that drop-down box) to run/debug your tests.

And if I want to run test cases on all the files in a directory?  What do I do differently?

The same -- just invoke context menu in different place. But in general -- two ways:

  1. With help of IDE via right click on folder (in Project View panel) that contains your test files and choose appropriate option from "Create Run Configuration" or "Run" submenus
  2. Manually via "Run | Edit Configurations..."
    • Create new entry of "PHP Unit" type
    • Test Scope = Directory


NOTE: When running ALL tests in folder (and subfolders), make sure that test files will be named *Test.php (default requirements of PHPUnit itself) otherwise it will not find them (unless you write your own test handler which will find and feed all such test files to PHPUnit).


But this dialogue I can't seem to get to:

StormDocs-1.png

This screenshot is incorrect (I mean -- description and image do not match each other).

To create PHPUnit configuration via context menu you have to do it on test file. The screenshot shown here shows icons that you will see when invoking context menu on actual class file (not test file) -- "Generate phpunit tests..." entry is proving that as well. PHPUnit configurations have a bit different icons (with green "tick" symbol).

0
Comment actions Permalink

Andriy,

I really appreciate all your help.

Your comment:

The same -- just invoke context menu in different place. But in general -- two ways:

  1. With help of IDE via right click on folder (in Project View panel) that contains your  test files and choose appropriate option from "Create Run Configuration"  or "Run" submenus


StormIDE-1.png

Actually, if I click on the project or any of the files other than the PersonTest.php file, I don't have the option in the right click menu:  "Create Run Configuration"  or "Run" submenus...

I can create a directory wide test config the "Manual" way.


Quote: "NOTE: When running ALL tests in folder (and subfolders), make sure that  test files will be named *Test.php (default requirements of PHPUnit  itself) otherwise it will not find them (unless you write your own test  handler which will find and feed all such test files to PHPUnit)."

Noted.  This is the way my tutorial said to do it also.  You can see I named the test file "PersonTest.php" and it got picked up fine.

This statement here is amusing: "This screenshot is incorrect (I mean -- description and image do not match each other)."

I got this screen shot right out of the manual. ;\

Really appreciate your time to assist!  I learn new stuff everyday.

-John

0
Comment actions Permalink

Hi John,

Actually, if I click on the project or any of the files other than the PersonTest.php file, I don't have the option in the right click menu:   "Create Run Configuration"  or "Run" submenus...

That's because of your very simple project structure (only few files in single folder). In any case -- that is correct behaviour -- what is the point of showing those entries to irrelevant files?

You better separate your code -- keep actual classes and tests in different folders -- check any open source library and see how they organizing their stuff. For example:
screen01.png

You can right click on your "tests" folder and mark it as "Test Source Root" -- it does not do much now (if anything at all), but will be useful in future versions.

This statement here is amusing: "This screenshot is incorrect (I mean -- description and image do not match each other)."

I got this screen shot right out of the manual. ;\

I know --- I have reported it and hopefully they will fix it soon -- http://youtrack.jetbrains.com/issue/WI-11828

0

Please sign in to leave a comment.