Skip to content

Instantly share code, notes, and snippets.

@Zayon
Last active November 23, 2018 17:07
Show Gist options
  • Save Zayon/deae683448996859ce149cb77c4299f9 to your computer and use it in GitHub Desktop.
Save Zayon/deae683448996859ce149cb77c4299f9 to your computer and use it in GitHub Desktop.
Step 2 - FixtureContext
default:
suites:
default:
contexts:
# other contexts
# ....
- FixtureContext:
doctrine: '@doctrine'
loader: '@fidry_alice_data_fixtures.doctrine.persister_loader'
fixturesBasePath: '%paths.base%/tests/Fixtures/'
# We need Behat's Symfony2 extension to be able to inject Symfony services
# composer require --dev behat/symfony2-extension
extensions:
Behat\Symfony2Extension:
kernel:
class: App\Kernel
<?php
declare(strict_types=1);
use Behat\Behat\Context\Context;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Fidry\AliceDataFixtures\LoaderInterface;
class FixtureContext implements Context
{
/** @var LoaderInterface */
private $loader;
/** @var string */
private $fixturesBasePath;
/**
* @var array Will contain all fixtures in an array with the fixture
* references as key
*/
private $fixtures;
public function __construct(
Registry $doctrine,
LoaderInterface $loader,
string $fixturesBasePath
) {
$this->loader = $loader;
$this->fixturesBasePath = $fixturesBasePath;
/** @var ObjectManager[] $managers */
$managers = $doctrine->getManagers(); // Note that currently,
// FidryAliceDataFixturesBundle
// does not support multiple managers
foreach ($managers as $manager) {
if ($manager instanceof EntityManagerInterface) {
$schemaTool = new SchemaTool($manager);
$schemaTool->dropDatabase();
$schemaTool->createSchema($manager->getMetadataFactory()->getAllMetadata());
}
}
}
/**
* @Given the fixtures file :fixturesFile is loaded
*/
public function theFixturesFileIsLoaded(string $fixturesFile): void
{
$this->fixtures = $this->loader->load([$this->fixturesBasePath.$fixturesFile]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment