Skip to content

Commit

Permalink
Merge pull request #146 from prestaconcept/#143_add_load_fixture_file
Browse files Browse the repository at this point in the history
#143 add loadFixtureFiles with Nelmio alice
  • Loading branch information
lsmith77 committed Mar 23, 2015
2 parents 0f7b983 + c1ada37 commit e628206
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
58 changes: 58 additions & 0 deletions Test/WebTestCase.php
Expand Up @@ -34,8 +34,11 @@
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;

use Doctrine\DBAL\Driver\PDOSqlite\Driver as SqliteDriver;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\Tools\SchemaTool;

use Nelmio\Alice\Fixtures;

/**
* @author Lea Haensenberger
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
Expand Down Expand Up @@ -159,6 +162,22 @@ protected function getContainer()
return $this->containers[$cacheKey];
}

/**
* @param string $omName
* @param string $registryName
*
* @return ObjectManager
*/
protected function getObjectManager($omName = null, $registryName = 'doctrine')
{
$registry = $this->getContainer()->get($registryName);
if ($registry instanceof ManagerRegistry) {
return $registry->getManager($omName);
}

return $registry->getEntityManager($omName);
}

/**
* This function finds the time when the data blocks of a class definition
* file were being written to, that is, the time when the content of the
Expand Down Expand Up @@ -343,6 +362,45 @@ protected function loadFixtures(array $classNames, $omName = null, $registryName
return $executor;
}

/**
* @param array $paths
* @param bool $append
* @param null $omName
* @param string $registryName
*
* @throws \BadMethodCallException
*/
public function loadFixtureFiles(array $paths = [], $append = false, $omName = null, $registryName = 'doctrine')
{
if (!class_exists('Nelmio\Alice\Fixtures')) {
throw new \BadMethodCallException('nelmio/alice should be installed to use this method.');
}

$om = $this->getObjectManager($omName, $registryName);

if ($append == false) {
//Clean database
$connection = $om->getConnection();
if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
$connection->query('SET FOREIGN_KEY_CHECKS=0');
}

$this->loadFixtures([]);

if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
$connection->query('SET FOREIGN_KEY_CHECKS=1');
}
}

$files = [];
$kernel = $this->getContainer()->get('kernel');
foreach ($paths as $path) {
$files[] = $kernel->locateResource($path);
}

Fixtures::load($files, $om);
}

/**
* Callback function to be executed after Schema creation.
* Use this to execute acl:init or other things necessary.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -23,7 +23,8 @@
"suggest": {
"doctrine/doctrine-fixtures-bundle": "Required when using the fixture loading functionality",
"doctrine/orm": "Required when using the fixture loading functionality with an ORM and SQLite",
"doctrine/dbal": "Required when using the fixture loading functionality with an ORM and SQLite"
"doctrine/dbal": "Required when using the fixture loading functionality with an ORM and SQLite",
"nelmio/alice": "Required when using loadFixtureFiles functionality"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit e628206

Please sign in to comment.