Setting up a PHP Development Environment – Part 2


Introduction

Note that this was published back in 2014 and is obsolete. I plan on doing a more modern development environment tutorial at a later date. Images are missing from this entry.

In the last part, I showed you how to set up your basic development environment using Eclipse and The Uniform Server, but we didn’t really go into configuring PEAR or any more advanced functionality. We will start with adding PHPUnit2 so that we can write tests to help prevent bugs.

1. Install PEAR module

Make sure your Apache service is started in The Uniform Server and then, still in the Uniform Server GUI go to Server Configuration -> PHP -> Pear control panel or go direct to http://localhost/us_pear/index.php
NOTE: Do not add the PHPUnit package which is available in the package manager when you first start. This is the old PHPUnit and should not be used. Follow the steps below to get PHPUnit2.
Click on the Channel Management button and, at the bottom, add two new channel servers: pear.symphony.com and pear.phpunit.de

PEAR Channels
PEAR Channels installed (other modules are also installed)


Go back to the Package Management page and add pear.phpunit.de/PHPUnit  You should end up with PHPUnit2 installed OK, but if you have any error messages, try installing pear.symfony.com/Yaml  first and then try PHPUnit again.
PHPUnit2 is now installed, but needs to be configured in Eclipse.

2. Set up in Eclipse

If you don’t have the Eclipse module MakeGood already installed, you will need to install it via the Eclipse Marketplace (Help -> Eclipse Marketplace). After searching for MakeGood, you may have several results. Install the version with the green logo.

MakeGood installed
MakeGood already installed. Note the green version is the one used.


I’m going to use an example to show you how to configure MakeGood to run your unit tests, but you can modify the paths etc to suit your own needs. How to write tests and the advanced features of PHPUnit2 are beyond the scope of this post.
Create a file ExampleClass.php in your lib folder and put the code for a simple class in it.

<?php
class ExampleClass {
	public static function MakeUpperCase($string) {
		if (is_object($string)) return false;
		return strtoupper($string);
	}
}
?>


Create a folder to hold your tests and create another PHP file in it called ExampleClassTest.php

<?php
require_once(__DIR__ . '/../lib/ExampleClass.php');
class ExampleClassTest extends PHPUnit_Framework_TestCase {
	public function testMakeUpperCase() {
		$this->assertTrue(ExampleClass::MakeUpperCase('SomeMixedText') === 'SOMEMIXEDTEXT');
	}
}
?>;
File Structure
File structure used by this example


Open the MakeGood View in Eclipse. You will notice that in the top left corner of the window is some red text informing you that MakeGood is not configured yet. Click the link to ‘fix’ it. This will bring up the Properties window with the MakeGood tab selected. Ensure that PHPUnit is selected as the Testing Framework and use the Add button to add your tests folder.

MakeGood Test Run
A completed MakeGood Test Run


You can now run the test by pressing the icon with the play symbol in the MakeGood view, although the tests were probably run automatically for you as the default is set to Continuous Testing. You can change this with the drop-down arrow next to the icon which looks like a power button.
You can also see the actual output of PHPUnit in the Console view.

Conclusion

You should now be able to use PHPUnit2 from within Eclipse. If you have any problems, check that you have followed all the steps in part 1. For instance, did you add the PEAR library to your project?
As above, the details of how to write tests properly is outside the scope of this post. The documentation for PHPUnit2 is excellent.
In part 3, we will cover adding PHPDoc.


Leave a Reply

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