How to Use Behat with Liip\FunctionalTestBundle in Symfony2?

430 Views Asked by At

I'm using Liip\FunctionalTestBundle for Unit testing, it works very well.

AppCategoryControllerTest.php:

class AppCategoryControllerTest extends BoEditoAuthWebTestCase
{
    public function setUp()
    {
        parent::setUp();

        // It returns an array of class paths
        $this->loadFixtures($this->getAllDataFixtures());
    }
    //...
}

Now I would like to use my test fixtures with Behat.

How is that possible?

FeatureContext.php:

/**
 * @BeforeScenario @createSchema
 *
 * load my fixtures with Liip\FunctionalTestBundle\Test\WebTestCase
 */
public function createDatabase()
{
    // What can I do here?
}
2

There are 2 best solutions below

0
On

solution for the problem

// ApiContext.php
<?php

  declare(strict_types=1);

  use Behat\Behat\Context\Context;

  class ApiContext extends \Liip\FunctionalTestBundle\Test\WebTestCase implements Context
  {
    public function __construct($kernelDir)
    {
      parent::__construct();
      $_SERVER['KERNEL_DIR'] = $kernelDir;
    }

  /**
   * @BeforeScenario
   *
   */
  public function loadFixturesData(): void
  {
    $fixturesLocation = '@AppBundle/DataFixtures/ORM/hautelook_alice/';
    $this->loadFixtureFiles([
        $fixturesLocation . 'fixture1.yml',
        $fixturesLocation . 'fixture2.yml',
    ]);
}

}

// behat.yml
....
  suites:
    default:
      contexts:
        - ApiContext:
            kernelDir: "app/"
....
0
On

As this is a PHPUnit test case, you can't use it directly in Behat. You'll need to replicate the behaviour in a Behat context.

Have a look at the SymfonyDoctrineContext from Behat/CommonContexts. It should be a good place to start. The context is written for Behat 2 so you'll need to tweak it for Behat 3.

I'm using Liip\FunctionalTestBundle for Unit testing, it works very well.

If you're using the FunctionalTestBundle, then you're not writing unit tests. Unit tests are isolated. Functional tests are type of an integration test (not isolated). If you haven't noticed yet, this kind of testing is brittle and slow. I recommend you to learn how to focus more of your efforts on true unit testing.