views:

323

answers:

1

I'd like to run unit test for a functions library file...

that is, I don't have a class, it's just a file with helper functions in it...

for example, I've created a php project at ~/www/test

and a file ~/www/test/lib/format.php

<?php

function toUpper( $text ) {
  return strtoupper( $text );
}

function toLower( $text ) {
  return strtolower( $text );
}

function toProper( $text ) {
  return toUpper( substr( $text, 0, 1 ) ) .  toLower( substr( $text, 1) );
}
?>

tools -> create PHPUnit tests gives me the following error:

PHPUnit 3.4.5 by Sebastian Bergmann.

Could not find class "format" in "/home/sas/www/test/lib/format.php".

now, if I code (by hand!) the file ~/www/test/tests/lib/FormatTest.php

<?php
require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../../lib/format.php';

class FormatTest extends PHPUnit_Framework_TestCase {

  protected function setUp() {}

  protected function tearDown() {}

  public function testToProper() {
    $this->assertEquals(
            'Sebastian',
            toProper( 'sebastian' )
    );
  }
}
?>

it works fine, I can run it...

but if I select test file from format.php i get

Test file for the selected source file was not found

any idea?

saludos

sas

ps: another question, is there a way to update generated tests without having to manually delete them???

ps2: using netbeans 2.8 dev

+1  A: 

How you've written your Unit test case is 100% correct. The problem lies with common convention and how PHPUnit and Netbeans relies on them.

Best practice these days is to write all your code in an object orientated fashion. So instead of having a PHP file full of utility functions like you have, you wrap these functions into a class and have them as static functions. Here's an example using your code above,

<?php

class Format
{
    public static function toUpper($text)
    {
        return strtoupper($text);
    }

    public static function toLower($text)
    {
        return strtolower($text);
    }

    public static function toProper($text)
    {
        return self::toUpper(substr($text, 0, 1 )) .  self::toLower(substr($text, 1));
    }
}

You'd now use your functions like so,

Format::toProper('something');

PHPUnit, and Netbeans, depend on this object orientated philosophy. When you try to automatically generate a PHPUnit test case, PHPUnit looks in your file for a class. It then creates a test case based on this class and it's public API, and calls it ClassNameTest, where ClassName is the name of the class being tested.

Neatbeans also follows this convention and knows that ClassNameTest is a PHPUnit test case for ClassName, so creates a link between the two in the IDE.

So, my advice is to always use classes where you can. If you have utility functions that don't depend on anything and are used globally, make them static ones.

Side note: I'd get rid of your two functions toUpper() and toLower(). There's no need to wrap built in PHP functions when there's no need to. There's also no need to test them as they are thoroughly tested.

Site note 2: There's kind of a built in PHP equivalent to your function toProper() called ucfirst().

Stephen Melrose