23
Mar

Unittests with PHPUnit

categories Computer, PHP, Tutorials    

A little private encounter with the community of an open source project opened my eyes, how good and easy life as a coder is if you’re working with unit tests. Not everybody uses them, but everybody should. For php programming the ideal software for this is PHPUnit.

The software can be obtained from GitHub verteilt, is well documented, widely used and very popular. It is also in the PEAR project and can be installed using the PEAR installer (it has some dependencies for additional features).

To demonstrate the basic procedural manner I’ll use a simple example. Imagine… somewhere within a php project there is a function, which returns easter sunday given any year. To make things easy, we’ll name it easter($year). Such a function could be used for determining holidays and therefore could have a certain criticality. How can this be tested with PHPUnit?

Let’s delve into code:

require_once 'includes/functions.php';
 
class EasterTest extends PHPUnit_Framework_TestCase {
  public function testEasterOk() {
    $easter2011 = easter(2011);
    $this->assertEquals($easter2011, gmmktime(0,0,0,4,24,2011));
  }
 
  public function testEasterFalse() {
    $easter2011 = easter(2011);
    $this->assertNotEquals($easter2011, gmmktime(0,0,0,12,24,2011));
  }
} 

First there is a link to the script containing the easter-function (here: functions.php). Then I generate a new class (“EasterTest”), which extends PHPUnit. In this class there are two basic tests:

Now to the boring theoretical part…
The solution isn’t the aim here – one glance at a calender should help you way better. The goal is, to compare the functions result with a defined result. I know for sure, that this years easter sunday is on 24.04. (as long as my calendar isn’t lying!) … und I know for sure, 24.12. ain’t somewhere near easter.

PHPUnit – like many other software products around unit testing – works with “assertions”. There is a library of simple functions for assertions in different constructs – for comparisms (like the here used assertEquals and assertNotEquals), arrays, bools and so on.

How much work is it?
Please decide for yourself. An easy test should be coded within less than five minutes. Not too difficult, ain’t it?

Back to work.

How can you now run those tests? Easiest way: use your shell (whatever, either Dos, Bash or anything else php runs on).

D:\htdocs\>phpunit EasterTest.php
PHPUnit 3.5.11 by Sebastian Bergmann.

..
 
Time: 0 seconds, Memory: 3.75Mb
 
OK (2 tests, 2 assertions)

PHPUnit can express itself using different output formats, for example TAP (“Test Anything Protocol”), JSON, XML and some more.

D:\htdocs\>phpunit --tap EasterTest.php
TAP version 13
ok 1 - EasterTest::testEasterOk
ok 2 - EasterTest::testEasterFalse
1..2

Now… how does it look like if there is an error? To test this I slighly changed my first test and assumed, 23.04.2011 is easter sunday – in reality it is the saturday.

D:\htdocs\>phpunit --tap EasterTest.php
TAP version 13
not ok 1 - Failure: EasterTest::testEasterOk
  ---
  message: 'Failed asserting that <integer:1303516800> matches expected <integer:1303603200>.'
  severity: fail
  data:
    got: 1303516800
    expected: 1303603200
  ...
ok 2 - EasterTest::testEasterFalse
1..2

If I return coding on the easter-function in the far future, then I still have my unit tests and can run them whenever I like (or whenever I need!). If the tests then return errors, either my tests are wrong or my function is (guess what is more likely). And this is what this is all about. Unit testing makes your code stronger and additions much easier.

And…if you encounter bugs… just write a test first and then fix it!

8
Mar

qTranslate

categories Computer    

I’m just giving qTranslate a try. Lets find out how it works!

5
Mar

PHP-Commandline-Parameter with Console_Getopt

categories PHP, Tutorials    

Mainly due to historical reasons my guess is most developers are using php as a language to build web pages and web applications. PHP can do alot more: it has a commandline interface, which enables you to write scripts using several different enviroments.

Parameters are stored in a special variable $argv.

Example 1: test_argv.php

<?php
var_dump($argv);
?>

Output Example 1:

d:\php5> php.exe test_argv.php IchBinParam1 UndIch2
array(3) {
  [0]=>
  string(13) "test_argv.php"
  [1]=>
  string(12) "IchBinParam1"
  [2]=>
  string(7) "UndIch2"
}

Not too bad so far. If you now want to start parsing and checking parameters, such a structure can easily get complicated. To make things easier, there is a little class called Console_Getopt in the PEAR project.

Installation
If you’re using PEAR, simply use the pear installer.

d:\php5> pear install Console_Getopt-1.3.0

But you don’t have to. It is sufficent to download the class here and include it into your project.

Usage
Not only installing this class is easy, so is the usage of it. Just include the class into your programm, initialize the class object, define allowed options and then check the parameters.

Here’s a simple example.

Beispiel 2: demo_getopt.php

<?php
// include class
include ("Getopt.php");

// initialize class
$cg = new Console_Getopt();

/** 
 * allowed options:
 * -a Abba
 * -b Beatles
 * -q Queen
 */
$allowedOptions = "abq";

// read commandline
$args = $cg->readPHPArgv();

// filter options
$ret = $cg->getopt($args, $allowedOptions);

// error message
if (PEAR::isError($ret)) {
    die ("error in commandline: " . $ret->getMessage() . "\n");
}

// print the options
print_r($ret);
?> 

Output Example 2:

d:\temp\getopt>php.exe demo_getopt.php -ab Who Genesis
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] =>
                )

            [1] => Array
                (
                    [0] => b
                    [1] =>
                )

        )

    [1] => Array
        (
            [0] => Who
            [1] => Genesis
        )

)

d:\temp\getopt>d:\php5\php.exe demo_getopt.php -c
Error in command line: Console_Getopt: unrecognized option -- c

After filtering the options you can now retrieve the command line parameters and work with them.

Example 3: demo_getopt_erweitert.php

.
.
.
// filter options
$ret = $cg->getopt($args, $allowedOptions);

$optionen = $ret[0];
if (sizeof($optionen) > 0) {
    // at least one option has been used
    foreach ($optionen as $o) {
        switch ($o[0]) {
            case 'a':
                echo "you choose: Abba\n"; 
                break;
            case 'b':
                echo "you choose: Beatles\n"; 
                break;
            case 'q':
                echo "you choose: Queen\n"; 
                break;
        }
    }
} 

The class can do a few things more, for example it is possible to check for optional or necessary parameters, check for strings or numeric values and things like this.

If you want to take a closer look into the class, it is well documented. A more detailed article with a few more examples can be found here at Zend.