I've managed to find a workaround for this! In order to be able to run unit tests from the NetBeans IDE, and see code coverage reports, we need to do a tiny bit of work.
First of all, create a
tests folder inside my Symfony2 project folder.
Next, open the
Project Properties dialog box, and select
Sources from the
Categories pane.
In the Tests Folder, select the above
tests folder that was just created.
Now we need to create a custom test suite for our project. Create a file inside the
tests folder called
MyProjectTestSuite.php containing the following code:
- Code: Select all
<?php
use Symfony\Component\Finder\Finder;
class MyProjectTestSuite extends PHPUnit_Framework_TestSuite
{
public static function suite()
{
$suite = new MyProjectTestSuite();
$finder = new Finder();
// ---------- COMMENT OUT TO TEST A SPECIFIC FILE ----------
// $suite->addTestFile('../src/<yourbundle>/DefaultBundle/Tests/Controller/SomeControllerTest.php');
// return $suite;
// ----------
echo "Searching for test cases...\n\n";
foreach ($finder->files()->in('../src/')->name('*Test.php') as $file) {
if (preg_match('%/Tests/[\w-/]+Test.php%i', $file->getPathName())) {
echo 'Adding test : ' . $file->getPathName() . "\n";
$suite->addTestFile($file->getPathName());
}
}
echo "\n";
return $suite;
}
}
This class uses the Symfony2 finder to locate unit tests inside the
src folder and adds them to the test suite. Next we'll tell NetBeans to use this test case.
Go back to the
Project Properties and select
PHPUnit from the
Categories panel.
Tick the
Use XML Configuration box and select the
<yourproject>/app/phpunit.xml.dist file in the
XML Configuration text field.
Now also tick
Use Custom Test Suite and select the
MyProjectTestSuite.php file we created earlier on, in the
Test Suite text field.
That's it. Now right click on your project, and select
Code Coverage > Collect and Display Code Coverage. To run your unit tests, right click the project and select
Test. You can also hit
Alt+F6 to run the tests.
Your unit tests should now run, and you'll be able to inspect code coverage right from within NetBeans!