Test::Simple for PHP
Via PHPDeveloper.org, I just read a post on PhpGirl that discusses a familiar topic, testing:
I'll write a Test::Simple for PHP. Yes, I know there exists one already that uses the power of Perl to test PHP files, but I didn't have time to figure out how to set that up and probably won't be able to use Perl anyways on the production system.
A few years ago, Geoff Young and I gave a talk called Testing PHP with Perl. We thought we were being funny (in our defense, those who listened to the talk seemed to agree), but I think all we did is make our project seem inaccessible to PHP developers. Perl is scary. :-)
Part of our project is a pure PHP implementation of Test::More called test-more.php. If you want to really keep things dirt simple (which seems to be Nola's goal), you can use it by itself with no framework, but more on that in a minute.
In a follow-up post, Nola describes how she uses her simple testing library. The first argument to ok() is a boolean that indicates whether the test passes. She demonstrates some tests using conditionals that compare two values expected to be equal:
<?php
$t->ok($user->getRealName() == 'John Doe Test', 'get RealName');
$t->ok($user->getUserName() == 'jdoe', 'get UserName');
$t->ok($user->getEmail() == 'john@doe.com', 'get Email');
$t->ok($user->getPermission() == 1, 'get Permission');
?>
She also demonstrates some tests using conditionals that compare two values expected to not be equal:
<?php
$t->ok($user->getRealName() != 'Susie Doe', 'Not Realname Suzie Doe');
$t->ok($user->getUserName() != 'sdoe', 'Not Username sdoe');
$t->ok($user->getEmail() != 'suz@doe.com', 'Not email suz@doe.com');
$t->ok($user->getPermission() != 0, 'Not Permission 0');
?>
You can do the same sort of thing with test-more.php:
<?php
include './test-more.php';
include './user-class.php';
plan(8);
ok($user->getRealName() == 'John Doe Test', 'get RealName');
ok($user->getUserName() == 'jdoe', 'get UserName');
ok($user->getEmail() == 'john@doe.com', 'get Email');
ok($user->getPermission() == 1, 'get Permission');
ok($user->getRealName() != 'Susie Doe', 'Not Realname Suzie Doe');
ok($user->getUserName() != 'sdoe', 'Not Username sdoe');
ok($user->getEmail() != 'suz@doe.com', 'Not email suz@doe.com');
ok($user->getPermission() != 0, 'Not Permission 0');
?>
I created a simple user-class.php file to implement the methods she uses:
<?php
class user
{
public function getRealName() { return 'John Doe Test'; }
public function getUserName() { return 'jdoe'; }
public function getEmail() { return 'john@doe.com'; }
public function getPermission() { return 1; }
}
$user = new user;
?>
If you just run this PHP script, you'll see the raw TAP output:
1..8 ok 1 - get RealName ok 2 - get UserName ok 3 - get Email ok 4 - get Permission ok 5 - Not Realname Suzie Doe ok 6 - Not Username sdoe ok 7 - Not email suz@doe.com ok 8 - Not Permission 0
If a test fails, the last line lets you know about it (in addition to one of those lines being not ok instead of ok):
# Looks like you failed 1 tests of 8.
So, even without a testing framework, you can use test-more.php as a simple testing facility. If you want to try it yourself, you can download Apache-Test (it's bundled with the distribution), look at the very bottom of TestConfigPHP.pm, or grab it from my code repository:
P.S. Using Test::Harness To Test PHP Applications, a talk being given at PHP Quebec, is supposed to cover this same topic. Hopefully Ken is keeping up with what's going on. If so, it should be a good talk. If not, it will be disappointing. :-)





6 Comments
1.
Nola said:
2.
Chris Shiflett said:
3.
Pedro Cardoso said:
4.
Matt said:
5.
Chris Shiflett said:
6.
Matt said: