views:

805

answers:

3

Hello, I use Eclipse PDT for PHP. I can run my PhpUnit tests : works fine.

But I can not debug my unit tests. Has someby already done this ? Can somebody help doing this ?

Thanx, Messaoud

+1  A: 

An example is more worth than 1000 words :

require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';

class MyTestCase extends PHPUnit_Framework_TestCase {

 protected function setUp() {
  parent::setUp ();

 }

 function testSimple() {
  echo "horray !";
 }

 protected function tearDown() {

  parent::tearDown();
 }

 static function main() {

  $suite = new PHPUnit_Framework_TestSuite( __CLASS__);
  PHPUnit_TextUI_TestRunner::run( $suite);
 }
}

if (!defined('PHPUnit_MAIN_METHOD')) {
    MyTestCase::main();
}

the key thing is :

  1. provide a main method in your testcase

  2. test if the test is executed directly (via php MyTestCase.php) or by phpunit itself. if executed directly - just start the testrunner.

know you can debug your testcase.

A: 

For others who are wondering if there are simple instructions for configuring Eclipse/Aptana with phpunit, here's a website I have found:

http://pkp.sfu.ca/wiki/index.php/Configure_Eclipse_for_PHPUnit

What you have to do basically is:

  1. Make sure your PEAR libraries are in your project's include path. Right click on the project in the navigator window and click Properties. You'll see there's a section for PHP Include Path (or PHP Build Path in Aptana for my version), open that and add your PEAR libraries to your include/build path so that Eclipse knows about phpunit.
  2. Create a debug configuration which runs the phpunit.php file (you might need to add the .php extension to the file if it is running with a shebang, as the case is in Mac OS X). So with phpunit.php file as the "Start Action" script, set "PHP Script Arguments" so that the PHPUnit testing file you are interested with is run by phpunit.php. Add any other command line arguments, to suit you. eg. --verbose is a good option. You can also use variables like ${resource_loc} to have Eclipse replace it with the current file for example.
  3. Run your debug configuration and enjoy debugging!

You do not need to modify your test files or anything, they'll work out of the box.

tayfun
+1  A: 

I finally run debugging parallel to command line in eclipse 3.4. Debugging i run as "PHP web page", my minimal code

require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
class XTest extends PHPUnit_Framework_TestCase{
    public function testX(){
        //...
    }
}
if (!defined('PHPUnit_MAIN_METHOD')) { 
    header('Content-type:text/plain; charset=utf-8');
    PHPUnit_TextUI_TestRunner::run( new PHPUnit_Framework_TestSuite( 'XTest'));
}
riasol