Unit Testing with NetBeans and phpUnit
Huzzah! I have found a way of being able to run the Unit tests in NetBeans.
Since phpUnit is essentially a set of php scripts itself, I thought to myself there must be a way of invoking it.
What I did was setup a source file as the boostrap for the Test Runner, set this file as NetBeans index file and from there you can just run it and see the test results in the output window or debug and it step into the test you are interested in.
It is not a fantastic solution but it is definately a stop gap until they do implement php Unit testing.
Heres my boostrap file for running tests:
<php $args = array( __FILE__, "--coverage-html", "D:\\Logaan\\Documents\\XamppData\\htdocs\\FormManager\\CodeCoverage", "--verbose", "D:\\Logaan\\Documents\\XamppData\\htdocs\\FormManager\\tests\\TestSuite.php" ); $_SERVER['argv'] = $args; $_SERVER['argc'] = count($args); require_once 'PHPUnit/TextUI/Command.php'; // Exclude this file from code coverage PHPUnit_Util_Filter::addFileToFilter(__FILE__); ?>
Its very simple, what I am doing is hard coding my arguments into the server environment variable (you could use the arguments in the NetBeans configuration) which is where phpUnit gets the arguments from.
Including the phpUnit TestUI test runner which has the main entry point, excluding this file from code coverage and then I just sit back and l let phpUnit run its course.
This is only a work around until Netbeans supports phpUnit.
hello, I am trying to debug de phpUnit test with your method, but I don’t understand why your are filling the array with that data.
Which phpUnit command matches your script?
I have tried some ways to fill the array with a simple comand like,
phpunit anyFile.php –coverage-html
but I have not been successful.
thanks
That array basically contains the arguments that are passed to phpunit on the command line (essentially phpunit is a batch script running a php script).
I discovered this by basically doing some digging, I looked at the phpunit batch script:
set PHPBIN="E:\xampp\php\.\php.exe""E:\xampp\php\.\php.exe" -d safe_mode=Off "E:\xampp\php\pear/PHPUnit/TextUI/Command.php" %*
From that you can see it just calls php with the TextUI/Command.php script and passes it all the arguments (thats
%*)Now I use Windows, so if you are using Linux there might be some extra work you have to do to get this to work.
The args above is the equivilant to:
phpunit TestRunner.php --coverage-html "D:\Logaan\Documents\XamppData\htdocs\FormManager\CodeCoverage" --verbose "D:\Logaan\Documents\XamppData\htdocs\FormManager\tests\TestSuite.php"(TestRunner.php is the name of the file with the code above)
If you write a simple test case that will print the server enviroment variables to the output, like:
< ?phperror_reporting(E_ALL | E_STRICT);
date_default_timezone_set('UTC');
require_once 'PHPUnit/Framework.php';
class DeleteModeTests extends PHPUnit_Framework_TestCase
{
public function testExampleTest()
{
print_r($_SERVER);
}
}
?>
Then call it with phpUnit on the command line:
phpunit example.phpAnd look at the server args:
..
.
[argv] => Array
(
[0] => E:\xampp\php\pear/PHPUnit/TextUI/Command.php
[1] => example.php
)
[argc] => 2
.
.
.
.
That is how I discovered how to structure my arguments.
Essentially what you need to achieve is replicating the behaviour of the batch file.
So that means pretending to have arguments being passed, invoke the PHPUnit/TextUI/Command.php script and then excluding this file from code coverage (if thats important to you)
(Typing
phpunit--helpwill provide a list of available parameters, just have play with the example script and the arguments to see what results you need to achieve)I hope this helps.
Your are right, I use linux, and it is a little bit different. With your script, I saw the server variables and the first argument has to be the path to your phpunit bin. The rest of the arguments as normal.
Here is the equivalent linux script:
Thank you very much for your help
This script automatically finds and runs test files (which end in “Test”) in it’s same directory. Set it as the index of a separate test project.
In Netbeans configuration, set “Run As” to “Script” and arguments as “[whatever flags you want to send phpunit] AllTests [name of script file below]”
e.g. “–verbose AllTests main.php” (no quotes)
addTestFile(‘MySqlSessionHandlerTest.php’);
//$suite->addTestFile(‘PageProtectorTest.php’);
// Automatically add all php files ending in “Test” to the suite
//
// Make an array of the file names in this script’s directory
$testProjectFiles = scandir(dirname(__FILE__));
// Seach the array for test files
foreach ($testProjectFiles as $file) {
//Get the end of the file name for comparison
$lastEightOfFileName = substr($file, -8);
// Add file to suite if it’s a test file
if ($lastEightOfFileName == “Test.php”) {
$suite->addTestFile($file);
};
}
return $suite;
}
}
require_once ‘/path/to/phpunit’;
?>
Actually I did something very similar but I used reflection.
I created an empty interface called ITestable, which I implemented on all my tests.
The test file that I passed to phpUnit included all php files from my test folder.
Using reflection I found all the classes that implemented that interface and added them to the test suite that way.