Eclipse, PHPUnit, xdebug, MakeGood and Ubuntu (PHP Unit testing)

Well after about 2 hours of head desking I have I got the above to all work nicely together.

Firstly you need to install all the prerequisites. If you are using you can install most of these though apt-get. I assume you have LAMP set up locally and you will be testing on this

sudo apt-get install php5-dev php-pear php5-cli php5-xdebug phpunit

This will install a bunch of stuff you will need. If you want to install from source feel free.

So next you need eclipse installed. I would not recommend apt-get install eclipse as this version is quite old. It is quite easy to ‘install’ eclipse (all you have to do is download it from their website, and then run the executable and boom (you need java installed)). There are loads of tutorials online about what java to install, etc so I won’t cover that now.

Once you have eclipse install you need PDT installed in eclipse. Again loads of tutorials here so go google it. You also need MakeGood installed (from the market place).

Once that is all done you can tell eclipse about it!

Window -> Preferences -> PHP -> PHP Executables
Click the Add button and fill in like the picture below

Window -> Preferences -> PHP ->Debug
Select the debugger and PHP Executable

Now add the PHP Libraries
Window -> Preferences -> PHP -> PHP Libraries
Click the New button, give the Library a name (Doesn’t matter what)
Then Click on this and then the Add External folder. You will want to add /usr/share/php

You now need to tell your PHP project to use the library and MakeGood about your tests.
Right click on the project -> Properties -> PHP Include Path -> Libraries (Tab).
Click the Add Library button -> Select User Library -> Next -> Tick whatever you called your library -> Finish

Still in the Project Properties -> MakeGood
Select PHPUnit, you can enter a XML Configuration File but it seems to work without it. See this for more about PHPUnit XML files
Then add some Test Folders.

The MakeGood ‘view’ may not have shown up so you will want to add it
Window -> Show View -> Other… -> MakeGood -> MakeGood

You also need to tell apache to use xdebug
Edit /etc/php/conf.d/xdebug.ini and add

xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"

to the bottom. Then restart apache

sudo service apache2 restart

Now you are ready to get testing!

Troubleshooting

You may come across this bug when running tests.

PHP Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/bin/phpunit on line 38

Follow the steps listed in the bug report (basically reinstall PHPUnit from pear and apt-get)


9 thoughts on “Eclipse, PHPUnit, xdebug, MakeGood and Ubuntu (PHP Unit testing)

midnight

Hi, it seems that picking IDE for php can be a real problem. In case of PDT I’m almost satisfied and the last thing i want to enable is stopping at breakpoints(with ability of inspecting variables’ values) during test-runs, I’ve made everything you’ve written – tests run and complete with reports, but breakpoints are ignored – any suggestions ?
Otherwise using makegood in preference to command line just doesn’t make any sense.
Thanks.

Reply

    Chris

    On my machine if I run my PHP scripts in debug the break points work fine, I just have to tell it the URL of the page. (EG http://localhost/index.php)

    If I run the tests in debug mode they fail with

    PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /home/chris/workspace/tests/myTest.php on line 4

    So it might not be possible to debug during test runs, however if your tests fail then you should be able to run the actual script that was being tested in debug. I would suggest to just ‘Run As PHP Web Page’, and the content should be displayed in the Internal Web Browser. You should now be able to run the script in debug mode, then you can inspect variables, etc.

    Reply

midnight

Hi, when I run makegood on contents like this, it doesn’t seem to bother about breakpoints, but when I debug script through plain debug menu it doesn’t step inside the class. This functionality does exist in netbeans “out of the box” which i don’t want to for other reasons.

Reply

Robert Phillips

Hi!

Thanks for a great article. Exactly what I needed. Just found a pitfall and workaround that may help others.

Pitfall to avoid for Ubuntu 11.10 with Eclipse 3.7 (maybe other?) systems:

Before adding the MakeGood view by doing the “Window -> Show View -> Other… -> MakeGood -> MakeGood” step, restart Eclipse. Otherwise you may get (or at least I did) a Java null pointer exception.

Thanks!

Reply

    Chris

    Hi,

    Thanks for this, I’ll try and check if this effects others, and update the post to reflect this shortly.

    Cheers

    Reply

Pablo Bueti

Does anyone knows how to run and debug unit tests with Makegood when the server is on another computer than eclipse?

If i run a php script from cli in the server, eclipse recieves the xdebug session and I can debug. Same for a browser. But i cannot do the same with phpunit.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *