0

I've installed all of the necessary bundles with composer :

"behat/behat": "*",
"behat/symfony2-extension": "*",
"behat/mink": "*",
"behat/mink-bundle": "dev-master",
"behat/mink-extension": "*",
"behat/mink-goutte-driver": "*",
"behat/mink-selenium2-driver": "*",
"phpunit/phpunit": "4.1.*",
"phpunit/phpunit-selenium": ">=1.2",

Then I followed the documentation of the MinkBundle about Code Coverage (https://github.com/Behat/MinkBundle/blob/master/Resources/doc/coverage.rst)

But I can't get to have the coverage of my functionnal tests.

Here's an example of a ControllerTest :

namespace Keep\AccountBundle\Tests;
use Behat\MinkBundle\Test\MinkTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;

class UserControllerTest extends MinkTestCase
{
    protected $base;

    protected function setUp()
    {
        $this->base = $this->getKernel()
            ->getContainer()
            ->getParameter('mink.base_url');

        //fixtures loading code
    }

    private function doLogin($username, $password) {
        $session = $this->getMink()->getSession();
        $session->visit($this->base.'/login');
        $page = $session->getPage();
        $page->fillField('username', $username);
        $page->fillField('password', $password);
        $page->pressButton('Login');

        $this->assertEquals($this->base.'/', $session->getCurrentUrl());
    }

    public function testList() {
        $this->doLogin('superadmin', 'superadmin');

        $session = $this->getMink()->getSession();
        $session->visit($this->base.'/account/user');

        $this->assertEquals('200', $session->getStatusCode());
        $this->assertCount(9, $session->getPage()->findAll('css', '#list-table tbody tr'));
    }
}

When I run the command :

php phpunit.phar --coverage-html ./mink-report -c app/ src/Bundle/AccountBundle/Tests/Controller/UserControllerTest.php

All tests are OK, but the coverage of Controller is 0%. What is covered is only fixtures code (DataFixtures folder, and entities setters.

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
onesteve
  • 1
  • 1

1 Answers1

0

My therory is that Mink uses his self php environment and thus phpunit can't trace the code.

So, in your yml config (config_test.yml in my case) :

#config/config_test.yml

// ...    

mink:
    base_url:           http://myproject.test
    #default_session:    goutte
    browser_name:       firefox
    #goutte:             ~
    #selenium2:          ~

I just commented the default_session choice, and disabled goutte and selenium2 driver just to be sure neither is used.

Now I got my code coverage, BUT ! Here comes fail asserts errors.

The base_url of my test url is myproject.test/app_test.php

When, in one of my tests, I try to log in by filling my auth form, it redirects me to myproject.test/ ! and not myproject.test/app_test.php

The only way I could solve this problem is by creating a host that points directly on app_test.php instead of myproject.test/.

And now my myproject.test/login (which is using app_test.php thanks to my vhost pointing on that), redirects on myproject.test/

onesteve
  • 1
  • 1