Skip to content

Commit

Permalink
Merge pull request #102 from K-Phoen/fix-fixtures-ordering
Browse files Browse the repository at this point in the history
Fix fixtures ordering
  • Loading branch information
willdurand committed Feb 7, 2012
2 parents 89adb25 + db8ed4d commit 33fbd28
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
24 changes: 19 additions & 5 deletions Command/FixturesLoadCommand.php
Expand Up @@ -163,9 +163,7 @@ protected function loadFixtures(InputInterface $input, OutputInterface $output,
return;
}

$finder = new Finder();
$tmpdir = $this->getApplication()->getKernel()->getRootDir() . '/cache/propel';
$datas = $finder->name('*.' . $type)->in($this->absoluteFixturesPath);
$datas = $this->getFixtureFiles($type);

if (count(iterator_to_array($datas)) === 0) {
return -1;
Expand Down Expand Up @@ -210,9 +208,8 @@ protected function loadFixtures(InputInterface $input, OutputInterface $output,
*/
protected function loadSqlFixtures(InputInterface $input, OutputInterface $output)
{
$finder = new Finder();
$tmpdir = $this->getApplication()->getKernel()->getRootDir() . '/cache/propel';
$datas = $finder->name('*.sql')->in($this->absoluteFixturesPath);
$datas = $this->getFixtureFiles('sql');

$this->prepareCache($tmpdir);

Expand Down Expand Up @@ -286,4 +283,21 @@ protected function insertSql($config, $sqlDir, $schemaDir, $output)

return true;
}

/**
* Returns the fixtures files to load.
*
* @param string $type The extension of the files.
* @param string $in The directory in which we search the files. If null,
* we'll use the absoluteFixturesPath property.
*
* @return \Iterator An iterator through the files.
*/
protected function getFixtureFiles($type = 'sql', $in = null)
{
$finder = new Finder();
$finder->sortByName()->name('*.' . $type);

return $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
}
}
77 changes: 77 additions & 0 deletions Tests/FixturesLoadCommandTest.php
@@ -0,0 +1,77 @@
<?php

/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Tests\Command;

use Symfony\Component\Filesystem\Filesystem;

use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Command\FixturesLoadCommand;

/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class FixturesLoadCommandTest extends TestCase
{
protected $command;

public function setUp()
{
$this->command = new TestableFixturesLoadCommand('testable-command');

// let's create some dummy fixture files
$this->fixtures_dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'propel';
$this->fixtures_files = array(
'10_foo.yml', '20_bar.yml', '15_biz.yml', '18_boo.sql', '42_baz.sql'
);

$this->filesystem = new Filesystem();
$this->filesystem->mkdir($this->fixtures_dir);
foreach ($this->fixtures_files as $file)
{
$this->filesystem->touch($this->fixtures_dir . DIRECTORY_SEPARATOR . $file);
}
}

public function tearDown()
{
$this->filesystem->remove($this->fixtures_dir);
}

public function testOrderedFixturesFiles()
{
$this->assertEquals(
array('10_foo.yml', '15_biz.yml', '20_bar.yml',),
$this->cleanFixtureIterator($this->command->getFixtureFiles('yml', $this->fixtures_dir))
);

$this->assertEquals(
array('18_boo.sql', '42_baz.sql',),
$this->cleanFixtureIterator($this->command->getFixtureFiles('sql', $this->fixtures_dir))
);
}

protected function cleanFixtureIterator($file_iterator)
{
$tmp_dir = $this->fixtures_dir;

return array_map(function($file) use($tmp_dir) {
return str_replace($tmp_dir . DIRECTORY_SEPARATOR, '', $file);
}, array_keys(iterator_to_array($file_iterator)));
}
}

class TestableFixturesLoadCommand extends FixturesLoadCommand
{
public function getFixtureFiles($type = 'sql', $in = null)
{
return parent::getFixtureFiles($type, $in);
}
}

0 comments on commit 33fbd28

Please sign in to comment.