
PHP TestFest
PHP TestFest 2009 is a series of events that you can go to and learn how to test PHP and make new friends and contacts in the PHP community.
One thing that concerned me was that writing these tests might require a in depth knowledge of C++ but this is not the case. Each test is just writing a few lines of PHP code (preferably no more than 10) which will have a known outcome. The tests will be run and the output compared to the expected outcome. For more information about writing tests please refer to the phpt test basics page from the PHP site.
There are events all over the world, but I will probably be attending the one in Manchester (organised by the PHPNW user group) so if you are going I’ll see you there!
This is your chance to get involved give something back to the PHP project!
JS Bin is an online tool that allows you to test JavaScript without having to muck about with files. You can just quickly cut and paste some code in and view the output. It is even possible to make the code you are looking at public and then use this as part of an ajax call in another instance of the application.
To get you started there is a nice help section, which also includes a couple of videos.

This tool really impressed me, especially the easy inclusion of several different JavaScript frameworks. I don’t mind testing JavaScript, but sometimes I just want to run a little bit of code on it’s own to see why it isn’t working as I expected it would. This tool fills that much needed gap, and the addition of allowing other people to view your code is a very nice feature. Well worth a look!
Run the following table definition to create a table called tests in the test database.
DROP TABLE IF EXISTS test.tests;
CREATE TABLE test.tests (
id int(10) unsigned NOT NULL auto_increment,
var varchar(45) NOT NULL,
PRIMARY KEY (id)
);
Fill it with some default values:
INSERT INTO tests(var) VALUES('one'), ('two'), ('three'), ('four');
Now lets say that you want to find the values in the table, but that you want to increment a value for each row, the following procedure will do this. If you are using the MySQL Query browser then open a script table and past the following and execute, changing any database/table definitions.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`Increment` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Increment`( number CHAR(64) )
BEGIN
SET @total=0;
SELECT id, @total:=@total+number AS RunningSum FROM tests;
END $$
DELIMITER ;
You will now be able to call the procedure in the following way:
CALL Increment(4);
The value that you pass to the procedure is used to work out the increment. So in this case the output would be something like the following:
id RunningSum
1 4
2 8
3 12
4 16
PHPUnit is a powerful unit testing framework written in and for PHP. Rather than testing everything as a whole the idea behind PHPUnit is to test that everything works as it is expected to work before it is integrated into the rest of the program. In this way problems are found earlier rather than later and this makes fixing them a lot easier. With tests written for every small component of the program it is then possible to test the whole thing by running all of the tests at once. It is also possible to automate PHPUnit so that everything about a program is tested before it is built. If any tests fail then the build is stopped.
The best way to install PHPUnit is to use the PEAR installer. To install PHPUnit you first need to add the correct channel so that PEAR can find PHPUnit easily. You do this by typing the following into the command line.
pear channel-discover pear.phpunit.de
You can now install PHPUnit by using the following command.
pear install phpunit/PHPUnit
You can now test your install by trying to run PHPUni through the command line.
phpunit
If properly installed this command will print out usage and parameter information for PHPUnit.
You can see which version of PHPUnit is installed by using the –version parameter.
phpunit --version
For more information about PHPUnit take a look at the PHPUnit website.
PHPUnit is a unit testing framework, written in PHP, and which is used to test PHP code. You can integrate the testing that PHPUnit does into Phing. You might want to use Phing to create a nightly build that contains the latest version of your program. The last thing you want is Phing to create a nightly build that is riddled with errors.
The way around this is to use PHPUnit to test our code whilst we are running Phing. If any tests fail then Phing will not finish the build.
Create a target at the top of your build.xml file and make sure it is run first. Then add the following code to the target, This will use PHPUnit to test your code.
<phpunit haltonfailure="true" haltonerror="true">
<batchtest>
<fileset dir="tests">
<include name="**/*Test*.php"/>
</fileset>
</batchtest>
</phpunit>
The phpunit element contains an element called batchtest, which can contain one or more fileset elements. It is the files defined by the fileset elements that define which files are to be used in the testing. The code above includes all PHP files that have the word Test in their name.
The phpunit element contains two attributes called haltonfailure and haltonerror. These attributes will cause the build to exit if any errors or failed tests are found during the testing.
Recent Comments