PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 18 ■ TESTING WITH PHPUNIT


woo\controller\Controller::run();


$ret = ob_get_contents();
ob_end_clean();
return $ret;


}
}


By catching the system's output in a buffer, I’m able to return it from the runCommand() method. I
apply a simple assertion to the return value to demonstrate.
Here is the view from the command line:


$ phpunit test/AddVenueTest.php


PHPUnit 3.4.11 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 4.00Mb
OK (1 test, 1 assertion)


If you are going to be running lots of tests on a system in this way, it would make sense to create a
Web UI superclass to hold runCommand().
I am glossing over some details here that you will face in your own tests. You will need to ensure that
the system works with configurable storage locations. You don’t want your tests going to the same
datastore that you use for your development environment. This is another opportunity for design
improvement. Look for hardcoded filepaths, and DSN values, push them back to the Registry, and then
ensure your tests work within a sandbox, but setting these values in your test case’s setUp() method.
Look into swapping in a MockRequestRegistry, which you can charge up with stubs, mocks, and various
other sneaky fakes.
Approaches like this are great for testing the inputs and output of a web application. There are some
distinct limitations, however. This method won’t capture the browser experience. Where a web
application uses JavaScript, Ajax, and other client-side cleverness, testing the text generated by your
system, won't tell you whether the user is seeing a sane interface.
Luckily, there is a solution.


Introducing Selenium


Selenium (http://seleniumhq.org/) consists of a set of commands (sometimes called selenese) for
defining web tests. It also provides tools for authoring and running browser tests, as well as for binding
tests to existing test platforms. Luckily for us, one of these platforms is PHPUnit.
In this brief introduction, I’ll author a quick WOO test using the Selenium IDE. Then I’ll export the
results, and run it as a PHPUnit test case.


Getting Selenium


You can download Selenium components at http://seleniumhq.org/download/. For the purposes of
this example, you will want Selenium IDE. And Selenium RC.
If you're running Firefox as your browser (and you need to be in order to run the IDE) you should
find that the Selenium IDE installs directly on download (after you've OK’d a prompt or two) and
becomes available in the Tools menu.

Free download pdf