* @author Toni Uebernickel */ class YamlDataLoaderTest extends TestCase { public function testYamlLoadOneToMany() { $fixtures = <<getTempFile($fixtures); $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); $this->assertCount(1, $books); $book = $books[0]; $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor()); } public function testYamlLoadOneToManyExternalReference() { $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array()); $fixtures = <<getTempFile($fixtures); $loader->load(array($filename), 'default'); $fixtures = <<getTempFile($fixtures); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); $this->assertCount(1, $books); $book = $books[0]; $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor()); } public function testLoadSelfReferencing() { $fixtures = <<getTempFile($fixtures); $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); $this->assertCount(0, $books); $authors = \Propel\Bundle\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', array()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); $this->assertCount(0, $books); $authors = \Propel\Bundle\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', array()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); $this->assertCount(0, $books); $authors = \Propel\Bundle\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); $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array(), \Faker\Factory::create()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\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 testYamlLoadManyToMany() { $schema = <<
XML; $fixtures = <<getTempFile($fixtures); $builder = new QuickBuilder(); $builder->setSchema($schema); $con = $builder->build(); $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookQuery::create()->find($con); $this->assertCount(2, $books); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBook', $books[0]); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBook', $books[1]); $authors = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthorQuery::create()->find($con);; $this->assertCount(2, $authors); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthor', $authors[0]); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthor', $authors[1]); $bookAuthors = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthorQuery::create()->find($con);; $this->assertCount(2, $bookAuthors); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthor', $bookAuthors[0]); $this->assertInstanceOf('Propel\Bundle\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', array()); $loader->load(array($filename1, $filename2), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookQuery::create()->find($con); $this->assertCount(2, $books); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBook', $books[0]); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBook', $books[1]); $authors = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesAuthorQuery::create()->find($con); $this->assertCount(2, $authors); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesAuthor', $authors[0]); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesAuthor', $authors[1]); $bookAuthors = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookAuthorQuery::create()->find($con); $this->assertCount(2, $bookAuthors); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookAuthor', $bookAuthors[0]); $this->assertInstanceOf('Propel\Bundle\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 testLoadWithInheritedRelationship() { $schema = <<
XML; $fixtures = <<getTempFile($fixtures); $builder = new QuickBuilder(); $builder->setSchema($schema); $con = $builder->build(); $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array()); $loader->load(array($filename), 'default'); $books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipBookQuery::create()->find($con); $this->assertCount(1, $books); $book = $books[0]; $author = $book->getAuthor(); $this->assertInstanceOf('Propel\Bundle\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', array()); $loader->load(array($filename), 'default'); $book = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlBookWithObjectQuery::create(null)->findOne($con); $this->assertInstanceOf('\Propel\Bundle\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', array()); $loader->load(array($filename), 'default'); $authors = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyAuthorQuery::create()->find($con); $this->assertCount(1, $authors); $author = $authors[0]; $person = $author->getYamlDelegateOnPrimaryKeyPerson(); $this->assertInstanceOf('Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyPerson', $person); } }