Skip to content
This repository has been archived by the owner on Aug 28, 2018. It is now read-only.

Load single fixture file (issue #36) #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Command/FixturesLoadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected function configure()
->setName('khepin:yamlfixtures:load')
->setDescription('Loads all fixtures in a given context')
->addArgument('context', InputArgument::OPTIONAL, 'Specify a context from which to load additional fixtures')
->addOption('fixture-file', null, InputOption::VALUE_REQUIRED, 'Import from a single bundle fixture file')
->addOption('purge-orm', null, InputOption::VALUE_NONE, 'If set, will purge the database before importing new fixtures')
->addOption('purge-mongodb', null, InputOption::VALUE_NONE, 'If set, will purge the database before importing new fixtures')
->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Purge data by using a database-level TRUNCATE statement (only for ORM)')
Expand All @@ -24,14 +25,16 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$context = $input->getArgument('context');
$fixture_file = $input->getOption('fixture-file');

if ($input->getOption('purge-orm')) {
$this->getContainer()->get('khepin.yaml_loader')->purgeDatabase('orm', $input->getOption('purge-with-truncate'));
}
if ($input->getOption('purge-mongodb')) {
$this->getContainer()->get('khepin.yaml_loader')->purgeDatabase('mongodb');
}

$this->getContainer()->get('khepin.yaml_loader')->loadFixtures($context);
$this->getContainer()->get('khepin.yaml_loader')->loadFixtures($context, $fixture_file);

$output->writeln('done!');
}
Expand Down
41 changes: 37 additions & 4 deletions Loader/YamlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,42 @@ public function setReference($name, $object)
{
$this->references[$name] = $object;
}

/**
* Returns an array of tags from loadFixtures method arguments
* @param array $load_fixtures_args
*/
protected function extractTags(array $load_fixtures_args) {
if (count($load_fixtures_args)) {
return array(0 => $load_fixtures_args[0]);
} else {
return $load_fixtures_args;
}
}

/**
* If it was specified in the command line, returns the single bundle fixture file
* @param array $load_fixtures_args
*/
protected function extractSingleBundle(array $load_fixtures_args) {
$single_bundle = (2 == count($load_fixtures_args) ? $load_fixtures_args[1] : null);
if ($single_bundle && !in_array($single_bundle, $this->bundles)) {
throw new \Exception('Bundle fixture ' . $single_bundle . " doesn't exist.");
}

return $single_bundle;
}

/**
* Gets all fixtures files
* @param string $single_bundle Single bundle fixture file if it was specified.
*/
protected function loadFixtureFiles()
protected function loadFixtureFiles($single_bundle = null)
{
if ($single_bundle) {
$this->bundles = array($single_bundle);
}

foreach ($this->bundles as $bundle) {
$file = '*';
if (strpos($bundle, '/')) {
Expand All @@ -101,20 +131,23 @@ protected function loadFixtureFiles()
*/
public function loadFixtures()
{
$this->loadFixtureFiles();
$tags = $this->extractTags(func_get_args());
$single_bundle = $this->extractSingleBundle(func_get_args());

$this->loadFixtureFiles($single_bundle);
foreach ($this->fixture_files as $file) {
$fixture_data = Yaml::parse($file);
// if nothing is specified, we use doctrine orm for persistence
$persistence = isset($fixture_data['persistence']) ? $fixture_data['persistence'] : 'orm';
$fixture = $this->getFixtureClass($persistence);
$fixture = new $fixture($fixture_data, $this, $file);
$fixture->load($this->getManager($persistence), func_get_args());
$fixture->load($this->getManager($persistence), $tags);
}

if (!is_null($this->acl_manager)) {
foreach ($this->fixture_files as $file) {
$fixture = new YamlAclFixture($file, $this);
$fixture->load($this->acl_manager, func_get_args());
$fixture->load($this->acl_manager, $tags);
}
}
}
Expand Down