Test::Simple for PHP
26 Jan 2006Via 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
. 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 Testmore:
<?php
include './testmore.php';
include './userclass.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 userclass.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 the previous lines being not ok
instead of ok
):
# Looks like you failed 1 tests of 8.
So, even without a testing framework, you can use Testmore as a simple testing tool. If you want to try it yourself, you can download Apache-Test (it's bundled with the distribution) or grab it from GitHub.
Using Test::Harness to Test PHP Applications, a talk being given at PHP Québec, is supposed to cover this same topic.