Test::Simple for PHP

26 Jan 2006

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. 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:

  1. <?php
  2.  
  3. $t->ok($user->getRealName() == 'John Doe Test', 'get RealName');
  4. $t->ok($user->getUserName() == 'jdoe', 'get UserName');
  5. $t->ok($user->getEmail() == 'john@doe.com', 'get Email');
  6. $t->ok($user->getPermission() == 1, 'get Permission');
  7.  
  8. ?>

She also demonstrates some tests using conditionals that compare two values expected to not be equal:

  1. <?php
  2.  
  3. $t->ok($user->getRealName() != 'Susie Doe', 'Not Realname Suzie Doe');
  4. $t->ok($user->getUserName() != 'sdoe', 'Not Username sdoe');
  5. $t->ok($user->getEmail() != 'suz@doe.com', 'Not email suz@doe.com');
  6. $t->ok($user->getPermission() != 0, 'Not Permission 0');
  7.  
  8. ?>

You can do the same sort of thing with Testmore:

  1. <?php
  2.  
  3. include './testmore.php';
  4. include './userclass.php';
  5.  
  6. plan(8);
  7.  
  8. ok($user->getRealName() == 'John Doe Test', 'get RealName');
  9. ok($user->getUserName() == 'jdoe', 'get UserName');
  10. ok($user->getEmail() == 'john@doe.com', 'get Email');
  11. ok($user->getPermission() == 1, 'get Permission');
  12.  
  13. ok($user->getRealName() != 'Susie Doe', 'Not Realname Suzie Doe');
  14. ok($user->getUserName() != 'sdoe', 'Not Username sdoe');
  15. ok($user->getEmail() != 'suz@doe.com', 'Not email suz@doe.com');
  16. ok($user->getPermission() != 0, 'Not Permission 0');
  17.  
  18. ?>

I created a simple userclass.php file to implement the methods she uses:

  1. <?php
  2.  
  3. class user
  4. {
  5.     public function getRealName() { return 'John Doe Test'; }
  6.     public function getUserName() { return 'jdoe'; }
  7.     public function getEmail() { return 'john@doe.com'; }
  8.     public function getPermission() { return 1; }
  9. }
  10.  
  11. $user = new user;
  12.  
  13. ?>

If you just run this PHP script, you'll see the raw TAP output:

  1. 1..8
  2. ok 1 - get RealName
  3. ok 2 - get UserName
  4. ok 3 - get Email
  5. ok 4 - get Permission
  6. ok 5 - Not Realname Suzie Doe
  7. ok 6 - Not Username sdoe
  8. ok 7 - Not email suz@doe.com
  9. 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):

  1. # 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.