Php test functions (not classes) with netbeans and PHPUnit

I want to run unit test for a function library file ...

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

for example, I created a php project in ~ / www / test

and 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 give me the following error:

PHPUnit 3.4.5 by Sebastian Bergman.

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

Now, if I code (manually!) the file ~ / www / test / tests / Library / 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 a test file from format.php, I will get

No test file found for selected source file

any idea?

Saludos

SAS

ps: another question, is there any way to update the generated tests without deleting them manually?

ps2: using netbeans 2.8 dev

+6
php unit-testing netbeans phpunit
source share
1 answer

As you wrote your case, Unit test is 100% correct. The problem is the general convention and how PHPUnit and Netbeans rely on them.

The best practice these days is to write all your code in an object-oriented style. Therefore, instead of having a PHP file full of utility functions like you, you transfer these functions to a class and put them as static functions. Here is an example of 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)); } } 

Now you will use your functions,

 Format::toProper('something'); 

PHPUnit and Netbeans depend on this object-oriented philosophy. When you try to automatically generate a PHPUnit test case, PHPUnit looks for a class in your file. He then creates a test case based on this class and a public API, and calls its 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 it creates a relationship between them in the IDE.

So my advice is to always use classes where you can. If you have utility functions that are not dependent on anything and are used globally, make them static.

Side note: I would get rid of your two functions toUpper() and toLower() . There is no need to wrap PHP built-in functions when not necessary. There is also no need to test them, as they are thoroughly tested.

Site Note 2:. There is a built-in PHP equivalent of your toProper() function called ucfirst() .

+5
source share

All Articles