How to Use Behat with Liip\FunctionalTestBundle in Symfony2?
Asked Answered
O

2

7

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?
}
Officer answered 8/12, 2015 at 14:55 Comment(0)
L
3

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.

Longboat answered 8/12, 2015 at 15:11 Comment(0)
U
1

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/"
....
Unionist answered 29/5, 2017 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.