If you want to load doctrine data fixtures manually in symfony you do by running $ php bin/console doctrine:fixtures:load --no-interaction --no-debug command however if you want to load them before each behat scenarios within the feature context file then you can follow the steps below.


Composer.json


{
...
"require-dev": {
"behat/behat": "3.2.2",
"behat/symfony2-extension": "2.1.1",
"behat/mink": "1.7.1",
"behat/mink-extension": "2.2",
"behat/mink-browserkit-driver": "1.3.2",
"doctrine/doctrine-fixtures-bundle": "2.3.0",
"doctrine/data-fixtures": "1.1.1"
},
...
}

AppKernel.php


$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();

LoadCountryData.php


namespace AppBundle\DataFixtures\ORM;

use AppBundle\Entity\Country;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

class LoadCountryData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
foreach ($this->getData() as $data) {
$country = new Country();
$country->setName($data['name']);

$manager->persist($country);
}

$manager->flush();
}

private function getData()
{
return [
['name' => 'Turkey',],
['name' => 'Germany',],
['name' => 'United Kingdom',],
['name' => 'France',],
['name' => 'Spain',],
['name' => 'Holland',],
];
}
}

Country.feature


Feature: Listing countries
In order to list countries
As a user
I should be able to navigate to country page

Scenario: Listing all countries
Given I am on "/country"
Then I should see "Turkey"

Scenario: Listing all countries
Given I am on "/country"
Then I should see "Germany"

FeatureContext.php


namespace AppBundle\Features\Context;

use AppBundle\DataFixtures\ORM\LoadCountryData;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Symfony\Component\HttpKernel\KernelInterface;

class FeatureContext extends MinkContext implements KernelAwareContext
{
/** @var KernelInterface */
private $kernel;

public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}

/**
* @BeforeScenario
*/
public function loadDataFixtures()
{
$container = $this->kernel->getContainer();
$entityManager = $container->get('doctrine.orm.entity_manager');

$loader = new Loader();
$loader->addFixture(new LoadCountryData());

$purger = new ORMPurger();
// You must actually enable this line but I disabled to show you MySQL output in test results.
// $purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE);
$executor = new ORMExecutor($entityManager, $purger);
$executor->execute($loader->getFixtures());
}
}

Behat.yml


default:
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://nation.dev/app_test.php
sessions:
symfony2:
symfony2: ~
suites:
app:
type: symfony_bundle
bundle: AppBundle
mink_session: symfony2
contexts:
- AppBundle\Features\Context\FeatureContext: ~

Test


$ vendor/bin/behat --suite=app
Feature: Listing countries
In order to list countries
As a user
I should be able to navigate to country page

Scenario: Listing all countries # src/AppBundle/Features/Country.feature:6
Given I am on "/country" # AppBundle\Features\Context\FeatureContext::visit()
Then I should see "Turkey" # AppBundle\Features\Context\FeatureContext::assertPageContainsText()

Scenario: Listing all countries # src/AppBundle/Features/Country.feature:10
Given I am on "/country" # AppBundle\Features\Context\FeatureContext::visit()
Then I should see "Germany" # AppBundle\Features\Context\FeatureContext::assertPageContainsText()

2 scenarios (2 passed)
4 steps (4 passed)
0m0.60s (30.40Mb)

Database


As you can see from the id field data is loaded twice.


mysql> SELECT * FROM country;
+----+----------------+
| id | name |
+----+----------------+
| 10 | France |
| 8 | Germany |
| 12 | Holland |
| 11 | Spain |
| 7 | Turkey |
| 9 | United Kingdom |
+----+----------------+
6 rows in set (0.00 sec)