[SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

[SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby josef.van.niekerk » Mon Mar 14, 2011 9:45 pm

Netbeans has a neat feature to run Unit Tests from within the IDE, and also display test coverage data. This requires a Tests Folder to be configured in the Project Properties.

The problem I'm having is, with a Symfony2 project, there are numerous test folders, possibly one for each Bundle in my project.

Is there a way to setup Netbeans 6.9.1 to run Symfony2 unit tests from the IDE itself, and not having to worry about the Tests Folder setting in Project Properties?
Last edited by josef.van.niekerk on Wed Mar 23, 2011 11:29 am, edited 1 time in total.
josef.van.niekerk
Junior Member
 
Posts: 28
Joined: Wed Mar 09, 2011 11:46 am

Re: Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby josef.van.niekerk » Tue Mar 15, 2011 11:43 am

I've tried setting the:
- Use Bootstrap
and
- Use XML Configuration,
but when I hit Alt+F6, Netbeans still asks me to select a Test folder?

Unit tests are running perfectly from the command line, but I would really love it if I could use the Netbeans unit testing feature.
josef.van.niekerk
Junior Member
 
Posts: 28
Joined: Wed Mar 09, 2011 11:46 am

Re: Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby josef.van.niekerk » Wed Mar 16, 2011 10:04 am

Nobody writing functional tests?
josef.van.niekerk
Junior Member
 
Posts: 28
Joined: Wed Mar 09, 2011 11:46 am

Re: Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby josef.van.niekerk » Wed Mar 23, 2011 10:17 am

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!
josef.van.niekerk
Junior Member
 
Posts: 28
Joined: Wed Mar 09, 2011 11:46 am

Re: [SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby ratius » Sat May 14, 2011 9:45 am

Thanks a lot for that one, it works like a charm. I even added the following to your script to that you can even jump from source file to test file within netbeans. (It adds or remove symlinks within the tests folder according to the found bundles within the src folder). The tests folder should be ignored into the configuration to avoid duplicate code assist.

Code: Select all
<?php

use Symfony\Component\Finder\Finder;

class TestSuite extends PHPUnit_Framework_TestSuite
{
    public static function suite()
    {
        $suite = new TestSuite();
        $finder = new Finder();

        echo "Searching for installed bundles...\n";
        $bundles = array();
        foreach( $finder->directories()->in('../src/')->name('Tests') as $dir) {
            $target = $dir->getPathName();
            $bundleName = basename(dirname($target));
            $name = __DIR__ . "/{$bundleName}";
            $bundles[] = $bundleName;

            if( !file_exists($name) ) {
                echo "Adding bundle symlink: {$target}\n";
                symlink($target, $name);
            }
        }

        echo "\nPurging broken symlinks...\n";
        $finder = new Finder();
        foreach( $bundles as $bundle ) {
            /* will list only broken symlinks */
            $symlinks = $finder->files()->in(__DIR__)->name('*Bundle');
            foreach( $symlinks as $symlink ) {
                if( !in_array($symlink->getFilename(), $bundles) ) {
                    echo "Removing broken bundle symlink: {$symlink->getFilename()}\n";
                    @unlink($symlink->getPathName());
                }
            }
        }

        // ---------- COMMENT OUT TO TEST A SPECIFIC FILE ----------
        // $suite->addTestFile('../src/<yourbundle>/DefaultBundle/Tests/Controller/SomeControllerTest.php');
        // return $suite;
        // ----------

        $finder = new Finder();
        echo "\nSearching for test cases...\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;
    }
}
ratius
Junior Member
 
Posts: 1
Joined: Sat May 14, 2011 9:33 am

Re: [SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby DaRaFF » Tue May 17, 2011 12:54 pm

Good work, i like your solution!

But i have one problem.
The unit tests in netbeans are running fine with your script and all is ok.
If i try to use the code completion feature i alway get 0% code completion and no file detection.

If im working without symfony2 for example a fizzbuzz test example, code completion is working without any problems.

Have you an idea?
DaRaFF
Junior Member
 
Posts: 2
Joined: Tue May 17, 2011 12:48 pm

Re: [SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby DaRaFF » Wed May 18, 2011 11:24 am

DaRaFF wrote:Good work, i like your solution!

But i have one problem.
The unit tests in netbeans are running fine with your script and all is ok.
If i try to use the code completion feature i alway get 0% code completion and no file detection.

If im working without symfony2 for example a fizzbuzz test example, code completion is working without any problems.

Have you an idea?


Problem solved. It was an error in a Symfony Bundle and therefore the coverage.xml could not be written.
DaRaFF
Junior Member
 
Posts: 2
Joined: Tue May 17, 2011 12:48 pm

Re: [SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby hades200082 » Sun Feb 19, 2012 3:20 pm

As an update to this, Netbeans 7.2 is slated to have Symfony2 support and there is a plugin out there for 7.1 that has some basic Symfony2 support.

PHPUnit support for Symfony2 in Netbeans is still sadly lacking though :(
hades200082
Member
 
Posts: 34
Joined: Wed Jul 06, 2011 2:32 am

Re: [SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby petui » Fri Mar 30, 2012 8:25 pm

Thanks a bunch josef.van.niekerk and ratius, you saved my day!

This is probably obvious for some, but I would like to point out something to any developers running Windows. The provided scripts need to be altered in regards of the symlinking and regex parts to use backslashes instead of forward ones.
petui
Junior Member
 
Posts: 1
Joined: Fri Mar 30, 2012 8:15 pm

Re: [SOLVED] Running Symfony2 Unit Tests from Netbeans 6.9.1

Postby dchaffin » Mon Nov 05, 2012 10:56 pm

Thanks for this! I got it to find all my test files with Netbeans 7.2.1.

What I'm still not getting though is some code coverage - and maybe what I want is not possible, so I'll ask that first ... should code coverage work with Functional Testing (e.g. testing classes that extend WebTestCase)?

If so, then I'm not getting coverage information. I'm just starting with testing and Symfony, but this is what I have thus far:

Code: Select all
class UserController extends Controller
{
  public function profileAction($username){
    $user = $this->getUser();
    if($username != $user->getUsername()){
      throw new AccessDeniedException();
    }   
    return $this->render('profile.html.twig', array('username' => $username));
  }
}


... and for testing this:

Code: Select all
class UserControllerTest extends WebTestCase { 
  public function testProfileThrowsExceptionForWrongUser(){
    $client = static::createClient(array(), array(
      'PHP_AUTH_USER' => 'testuser',
      'PHP_AUTH_PW' => 'testpass',
    ));
    $client->followRedirects(true);
    $crawler = $client->request('GET', '/user/testfail');
    $this->assertEquals(403, $client->getResponse()->getStatusCode());
  } 
}


... test runs and passes, but the code in my class does not show as covered.

Thanks!
dchaffin
Junior Member
 
Posts: 3
Joined: Mon Nov 05, 2012 10:49 pm


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: No registered users and 8 guests