Monday, July 9, 2012

MakeGood and Symfony2



A few days ago I was looking for an easier way for automating my PHPUnit testing in Symfony2 and stumbled into this totally awesome tool: MakeGood.

So, what does it do? If a few words, it is a "continuous testing tool", meaning that it is able to execute all your tests in background whenever any file in the project changes (as this is a potential cpu-killer feature, it can be easily disconnected and you will still be able to launch the tests manually).

It is fully integrated with Symfony2 but I stumbled into a few pitfalls while trying to make it work. I finally I succeeded and fell like writing it down, both for future reference and to help out others.


Prerequisites

  • A working Eclipse PDT installation. I got mine from http://www.eclipse.org/projects/project.php?id=tools.pdt. Only tested with the latest version available as of the time of writing (Eclipse 4.2 Juno).
  • A working Symfony2 project. Tested with Symfony 2.x but soon-to-be-upgraded to Symfony 2.1
  • Some PHPUnit tests. You should be able to run your tests suite from the command line as usual before trying this, to avoid extra work.

Installation

  1. Download and install MakeGood as directed here. Review the rest of required components and install them too.
  2. Open your Symfony2 project in Eclipse and go to menu option Project->Properties. In the MakeGood section set the following values:
    • Tab "General": 
      • Testing Framework: PHPUnit (of course).
      • Test Folders: Click "Add" and look for your project /src folder.
      • Preload Script: Click "Browse" and look for your project /app/bootstrap.php.cache file.
    • Tab "PHPUnit":
      • XML Configuration File:  Click "Browse" and look for your /app/phpunit.xml.dist file BUT... we will not be using it!. After choosing the above file, change the just the file name to "phpunit.xml" (that is, just remove the ".dist" part). More on this later.
Click "Apply" and "OK" to close the dialog and return to your project's view.

Next we need to create the /app/phpunit.xml file we just used because it is not part of a standard Symfony installation.
  1. In your project's PHP Explorer go to "/app" an create a copy of file "phpunit.xml.dist" called "phpunit.xml".
  2. Open file "phpunit.xml" with text editor (it is easier than the XML editor) and make the following change:
  • Uncomment block:
<!--
<php>
   <server name="KERNEL_DIR" value="/path/to/your/app/" />
</php>
-->
  • and change the path after "value" with the full path to  your project's /app folder:
<php>
    <server name="KERNEL_DIR" value="/home/myself/php/myproject/app/" />
</php>
  • Save the file and hopefully you'll have a working MakeGood installation.

Testing

  • Show MakeGood's view in Eclipse (Window -> Show View -> Other and look for MakeGood).
  • MakeGood's view shows a few options (buttons in the top of the view). You can connect or disconnect the automatic run of all tests or just the last one whenever any file in the projects is modified, or run the tests manually. You can even launch the tests in debug mode, pretty neat, huh?
  • The same options for running tests can be found as contextual menu items for the source files in the project.

Conclusion

I am pretty impressed by this tool. It is worth the effort of making it work. All in all, I give it 9 out of 10 in my awesomeness scale!!!

3 comments:

  1. I cant get it to work. MakeGood says: "No testable project selected."

    I don't know why... I'm running mountain lion and PHPUnit perfectly fine in the terminal.

    What do you have as pear build path in your Eclipse-PHP Library settings?

    ReplyDelete
  2. Thanks Miguel! There's setup instructions here for those running Windows 7

    http://beagile.biz/agile-sdk-php-xampp-windows-7/

    ReplyDelete
  3. Thanks ! I was able to setup my symfony2 project and test are working just fine.

    ReplyDelete