PHP: Setting up Eclipse with PHPUnit

2013-11-12

In this post I will go through setting up XAMPP and Eclipse to let you work with PHP and one of its unit testing frameworks PHPUnit.

I’m assuming you already know how to use PHPUnit (or will discover that knowledge somewhere else). These steps will still work if you don’t use XAMPP (you will just need to download Xdebug and move the dll into your php/ext folder)

XAMPP

  1. Install XAMPP
  2. Edit the php.ini file in your XAMPP installation (<XAMPP Install Directory>\php\php.ini). At the bottom of the file, uncomment (i.e. delete the semicolon) from these lines:[xml]
    [XDebug]
    zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
    xdebug.profiler_append = 0
    xdebug.profiler_enable = 1
    xdebug.profiler_enable_trigger = 0
    xdebug.profiler_output_dir = "C:\xampp\tmp"
    xdebug.profiler_output_name = "cachegrind.out.%t-%s"
    xdebug.remote_enable = 0
    xdebug.remote_handler = "dbgp"
    xdebug.remote_host = "127.0.0.1"
    xdebug.trace_output_dir = "C:\xampp\tmp"
    [/xml]

    Save the file and then restart Apache Server (using the XAMPP Control Panel).

  3. Add the XAMPP PHP directory to your path variable (<XAMPP Install Directory>\php)

Eclipse

  1. Download and unzip Eclipse
  2. Install PHP Development Tools (PDT) into Eclipse
    1. In Eclipse, Help > Install New Software
    2. Choose Work With: –All Available Sites
    3. In the filter box, type PHP and select PHP Development Tools (PDT)
    4. Choose Next and follow the prompts
      Showing the Install pop-up to add PDT to Eclipse
  3. Configure Eclipse to accept debugging sessions from XDebug. Follow the steps below:
    1. In Eclipse, Window > Preferences
    2. Select PHP >Debug
    3. For PHP Debugger select XDebug and click Apply
    4. Click Configure to the right of XDebug in the same window
    5. Select XDebug and click Configure
    6. For Accept remote session (JIT) select any and click OK
  4. Install MakeGood (Eclipse plugin that allows you to run UnitTests directly in Eclipse)
    1. In Eclipse, Help > Install New Software
    2. Choose Work With: and enter http://eclipse.piece-framework.com/
    3. Select MakeGood
    4. Choose Next and follow the prompts (when it asks if you trust a certificate Piece Project; Piece Project; Piece Project choose OK)
  5. You will need to add the PHP CLI to your Eclipse settings for MakeGood to work:
    1. Window > Preferences
    2. PHP > PHP Executables
    3. Click Add
    4. For the Name you can type in XAMPP.
    5. For the Executable path, browse for the XAMPP php.exe file (<XAMPP Install Directory>\php)
    6. For the PHP ini file path, browse for the XAMPP php.ini file (<XAMPP Install Directory>\php)
    7. For PHP debugger choose XDebug
    8. Click OK to accept
      Eclipse Preferences dialog
  6. Next you will need to add the PEAR as part of your library path:
    1. Window > Preferences
    2. PHP > PHP Libraries
    3. Click New
    4. Call the library PEAR
    5. Select the new library and click Add External folder…
    6. Select the location of PEAR for your XAMPP install (<XAMPP Install Directory>\php\pear)
    7. Click OK to accept
  7. Now you will need to configure your Eclipse Project to make use of the PEAR library (to give you access to PHPUnit_Framework_TestCase class)
    1. Select your Project (in the PHP Explorer), right-click and select Properties (or ALT+ENTER)
    2. Select PHP Include Path
    3. Click Add library…
    4. Choose User Library and click Next
    5. Select PEAR (from step 6 above) and click Finish
      Eclipse Project Preferences dialog
  8. Finally, you will configure your project to work with MakeGood
    1. Again from the Project Properties window, select MakeGood
    2. Under the General tab, select PHPUnit as your Testing Framework
    3. Under Test Folders, click Add and add your test folder
    4. Click OK to accept
  9. To show the MakeGood window in Eclipse, WindowShow View Other. Select MakeGood and click OK.

eclipse-makegood-panel

You should now be able to run your PHPUnit tests through Eclipse via MakeGood. By default Continuous Testing is turned on, so MakeGood will run your tests whenever you save a file in the project.