From db8ed4da4cf85a4a07e291681c47cd9c6f3a26aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez=20Pinto?= Date: Tue, 7 Feb 2012 16:15:54 +0100 Subject: [PATCH] Fix: fixtures ordering was not handled by the FixturesLoad command --- Command/FixturesLoadCommand.php | 24 ++++++++-- Tests/FixturesLoadCommandTest.php | 77 +++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 Tests/FixturesLoadCommandTest.php diff --git a/Command/FixturesLoadCommand.php b/Command/FixturesLoadCommand.php index 878fcb40..381eb0a6 100644 --- a/Command/FixturesLoadCommand.php +++ b/Command/FixturesLoadCommand.php @@ -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; @@ -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); @@ -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); + } } diff --git a/Tests/FixturesLoadCommandTest.php b/Tests/FixturesLoadCommandTest.php new file mode 100644 index 00000000..2a854149 --- /dev/null +++ b/Tests/FixturesLoadCommandTest.php @@ -0,0 +1,77 @@ + + */ +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); + } +}