From e0a9b9597739ab08b09ed4c75d5bc788627a48fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sat, 14 Dec 2013 12:36:10 +0000 Subject: [PATCH] Added tests for data loaders and data dumpers --- DataFixtures/Loader/AbstractDataLoader.php | 5 +- .../Dumper/YamlDataDumperTest.php | 61 +++ .../DataFixtures/Loader/XmlDataLoaderTest.php | 47 ++ .../Loader/YamlDataLoaderTest.php | 469 ++++++++++++++++++ Tests/DataFixtures/TestCase.php | 97 ++++ Tests/TestCase.php | 16 + 6 files changed, 693 insertions(+), 2 deletions(-) create mode 100644 Tests/DataFixtures/Dumper/YamlDataDumperTest.php create mode 100644 Tests/DataFixtures/Loader/XmlDataLoaderTest.php create mode 100644 Tests/DataFixtures/Loader/YamlDataLoaderTest.php create mode 100644 Tests/DataFixtures/TestCase.php diff --git a/DataFixtures/Loader/AbstractDataLoader.php b/DataFixtures/Loader/AbstractDataLoader.php index 9bbde5c..dcf6719 100644 --- a/DataFixtures/Loader/AbstractDataLoader.php +++ b/DataFixtures/Loader/AbstractDataLoader.php @@ -13,6 +13,7 @@ namespace Propel\PropelBundle\DataFixtures\Loader; use Propel\PropelBundle\DataFixtures\AbstractDataHandler; use Propel\PropelBundle\Util\PropelInflector; use Propel\Runtime\ActiveRecord\ActiveRecordInterface; +use Propel\Runtime\Map\Exception\TableNotFoundException; use Propel\Runtime\Map\TableMap; /** @@ -139,7 +140,7 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa $class = substr($class, 1); } $tableMap = $this->dbMap->getTable(constant(constant($class.'::TABLE_MAP').'::TABLE_NAME')); - $column_names = TableMap::getFieldnamesForClass($class, TableMap::TYPE_PHPNAME); + $column_names = $tableMap->getFieldnames(TableMap::TYPE_PHPNAME); foreach ($datas as $key => $data) { // create a new entry in the database @@ -166,7 +167,7 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa $this->loadManyToMany($obj, substr($name, 0, -1), $value); continue; - } catch (PropelException $e) { + } catch (TableNotFoundException $e) { // Check whether this is actually an array stored in the object. if ('Cannot fetch TableMap for undefined table: ' . substr($name, 0, -1) === $e->getMessage()) { if (PropelColumnTypes::PHP_ARRAY !== $tableMap->getColumn($name)->getType() diff --git a/Tests/DataFixtures/Dumper/YamlDataDumperTest.php b/Tests/DataFixtures/Dumper/YamlDataDumperTest.php new file mode 100644 index 0000000..7e896c3 --- /dev/null +++ b/Tests/DataFixtures/Dumper/YamlDataDumperTest.php @@ -0,0 +1,61 @@ + + * @author Toni Uebernickel + */ +class YamlDataDumperTest extends TestCase +{ + public function testYamlDump() + { + $author = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor(); + $author->setName('A famous one')->save($this->con); + + $complementary = new \stdClass(); + $complementary->first_word_date = '2012-01-01'; + + $book = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBook(); + $book + ->setName('An important one') + ->setAuthorId(1) + ->setComplementaryInfos($complementary) + ->save($this->con) + ; + + $filename = $this->getTempFile(); + + $loader = new YamlDataDumper(__DIR__.'/../../Fixtures/DataFixtures/Loader', new Propel(), array()); + $loader->dump($filename); + + $expected = <<assertEquals($expected, $result); + } +} diff --git a/Tests/DataFixtures/Loader/XmlDataLoaderTest.php b/Tests/DataFixtures/Loader/XmlDataLoaderTest.php new file mode 100644 index 0000000..c5692d3 --- /dev/null +++ b/Tests/DataFixtures/Loader/XmlDataLoaderTest.php @@ -0,0 +1,47 @@ + + * @author Toni Uebernickel + */ +class XmlDataLoaderTest extends TestCase +{ + public function testXmlLoad() + { + $fixtures = << + + + + + + + +XML; + + $filename = $this->getTempFile($fixtures); + + $loader = new XmlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', new Propel(), array()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(1, $books); + + $book = $books[0]; + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor()); + } +} diff --git a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php new file mode 100644 index 0000000..942c9e9 --- /dev/null +++ b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php @@ -0,0 +1,469 @@ + + * @author Toni Uebernickel + */ +class YamlDataLoaderTest extends TestCase +{ + public function testYamlLoadOneToMany() + { + $fixtures = <<getTempFile($fixtures); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(1, $books); + + $book = $books[0]; + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor()); + } + + public function testYamlLoadManyToMany() + { + $schema = << + + + +
+ + + + +
+ + + + + + + + + + + +
+ +XML; + + $fixtures = <<getTempFile($fixtures); + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + + $con = $builder->build(); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookQuery::create()->find($con); + $this->assertCount(2, $books); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBook', $books[0]); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBook', $books[1]); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthorQuery::create()->find($con);; + $this->assertCount(2, $authors); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthor', $authors[0]); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthor', $authors[1]); + + $bookAuthors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthorQuery::create()->find($con);; + $this->assertCount(2, $bookAuthors); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthor', $bookAuthors[0]); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthor', $bookAuthors[1]); + + $this->assertEquals('Victor Hugo', $authors[1]->getName()); + $this->assertTrue($authors[1]->getBooks()->contains($books[1])); + $this->assertEquals('Les misérables', $authors[1]->getBooks()->get(0)->getName()); + } + + public function testYamlLoadManyToManyMultipleFiles() + { + $schema = << + + + +
+ + + + +
+ + + + + + + + + + + +
+ +XML; + + $fixtures1 = <<getTempFile($fixtures1); + $filename2 = $this->getTempFile($fixtures2); + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $con = $builder->build(); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename1, $filename2), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookQuery::create()->find($con); + $this->assertCount(2, $books); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBook', $books[0]); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBook', $books[1]); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesAuthorQuery::create()->find($con); + $this->assertCount(2, $authors); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesAuthor', $authors[0]); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesAuthor', $authors[1]); + + $bookAuthors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookAuthorQuery::create()->find($con); + $this->assertCount(2, $bookAuthors); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookAuthor', $bookAuthors[0]); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookAuthor', $bookAuthors[1]); + + $this->assertEquals('Victor Hugo', $authors[1]->getName()); + $this->assertTrue($authors[1]->getBooks()->contains($books[1])); + $this->assertEquals('Les misérables', $authors[1]->getBooks()->get(0)->getName()); + } + + public function testLoadSelfReferencing() + { + $fixtures = <<getTempFile($fixtures); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(0, $books); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthorQuery::create()->find($this->con); + $this->assertCount(1, $authors); + + $author = $authors[0]; + $this->assertEquals('A famous one', $author->getName()); + } + + public function testLoaderWithPhp() + { + $fixtures = << + +YAML; + $filename = $this->getTempFile($fixtures); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(0, $books); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthorQuery::create()->find($this->con); + $this->assertCount(1, $authors); + + $author = $authors[0]; + $this->assertEquals('to be announced', $author->getName()); + } + + public function testLoadWithoutFaker() + { + $fixtures = << + +YAML; + $filename = $this->getTempFile($fixtures); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(0, $books); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthorQuery::create()->find($this->con); + $this->assertCount(1, $authors); + + $author = $authors[0]; + $this->assertEquals('word', $author->getName()); + } + + public function testLoadWithFaker() + { + if (!class_exists('Faker\Factory')) { + $this->markTestSkipped('Faker is mandatory'); + } + + $fixtures = << + description: + +YAML; + $filename = $this->getTempFile($fixtures); + $container = $this->getContainer(); + $container->set('faker.generator', \Faker\Factory::create()); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $container); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(1, $books); + + $book = $books[0]; + $this->assertNotNull($book->getName()); + $this->assertNotEquals('null', strtolower($book->getName())); + $this->assertRegexp('#[a-z]+#', $book->getName()); + $this->assertNotNull($book->getDescription()); + $this->assertNotEquals('null', strtolower($book->getDescription())); + $this->assertRegexp('#[\w ]+#', $book->getDescription()); + } + + public function testLoadWithInheritedRelationship() + { + $schema = << + + + + + + + + +
+ + + + +
+ + + + + + +
+ + +XML; + + $fixtures = <<getTempFile($fixtures); + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $con = $builder->build(); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipBookQuery::create()->find($con); + $this->assertCount(1, $books); + + $book = $books[0]; + $author = $book->getAuthor(); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipAuthor', $author); + } + + public function testLoadArrayToObjectType() + { + $schema = << + + + + +
+ +XML; + $fixtures = <<getTempFile($fixtures); + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $con = $builder->build(); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $book = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlBookWithObjectQuery::create(null, $con)->findOne(); + + $this->assertInstanceOf('\Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlBookWithObject', $book); + $this->assertEquals(array('opt1' => 2012, 'opt2' => 140, 'inner' => array('subOpt' => 123)), $book->getOptions()); + } + + public function testLoadDelegatedOnPrimaryKey() + { + $schema = << + + + +
+ + + + + + + + + + + + +
+ +XML; + + $fixtures = <<getTempFile($fixtures); + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + $con = $builder->build(); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer()); + $loader->load(array($filename), 'default'); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyAuthorQuery::create()->find($con); + $this->assertCount(1, $authors); + + $author = $authors[0]; + $person = $author->getYamlDelegateOnPrimaryKeyPerson(); + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyPerson', $person); + } +} diff --git a/Tests/DataFixtures/TestCase.php b/Tests/DataFixtures/TestCase.php new file mode 100644 index 0000000..fb95801 --- /dev/null +++ b/Tests/DataFixtures/TestCase.php @@ -0,0 +1,97 @@ + + */ +class TestCase extends BaseTestCase +{ + /** + * @var \PropelPDO + */ + protected $con; + + /** + * The list of created temp files to be removed. + * + * @var array + */ + protected $tmpFiles = array(); + + protected function setUp() + { + parent::setUp(); + + $schema = << + + + + + + + + + + +
+ + + + +
+ +XML; + + if (class_exists('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBook')) { + $classTargets = array(); + } else { + $classTargets = null; + } + + $builder = new QuickBuilder(); + $builder->setSchema($schema); + + $this->con = $builder->build($dsn = null, $user = null, $pass = null, $adapter = null, $classTargets); + } + + protected function tearDown() + { + foreach ($this->tmpFiles as $eachFile) { + @unlink($eachFile); + } + + $this->tmpFiles = array(); + } + + /** + * Return the name of a created temporary file containing the given content. + * + * @param string $content + * + * @return string + */ + protected function getTempFile($content = '') + { + $filename = tempnam(sys_get_temp_dir(), 'propelbundle-datafixtures-test'); + @unlink($filename); + + file_put_contents($filename, $content); + + $this->tmpFiles[] = $filename; + + return $filename; + } +} diff --git a/Tests/TestCase.php b/Tests/TestCase.php index 4583453..d1a2ff3 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -10,9 +10,25 @@ namespace Propel\PropelBundle\Tests; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; + /** * TestCase */ class TestCase extends \PHPUnit_Framework_TestCase { + public function getContainer() + { + $container = new ContainerBuilder(new ParameterBag(array( + 'kernel.debug' => false, + 'kernel.root_dir' => __DIR__ . '/../', + ))); + + $container->setParameter('propel.configuration', array()); + $container->setDefinition('propel', new Definition('Propel\Runtime\Propel')); + + return $container; + } }