add tests for DataFixtures

This commit is contained in:
Toni Uebernickel 2012-03-05 12:31:32 +01:00
parent 3397d7422d
commit f589d318ee
7 changed files with 123 additions and 137 deletions

View file

@ -169,13 +169,13 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
if ($isARealColumn) { if ($isARealColumn) {
if ($column->isForeignKey() && null !== $value) { if ($column->isForeignKey() && null !== $value) {
$relatedTable = $this->dbMap->getTable($column->getRelatedTableName()); $relatedTable = $this->dbMap->getTable($column->getRelatedTableName());
if (!isset($this->object_references[$relatedTable->getPhpName().'_'.$value])) { if (!isset($this->object_references[$relatedTable->getClassname().'_'.$value])) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getPhpName()) sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getPhpName())
); );
} }
$value = $this $value = $this
->object_references[$relatedTable->getPhpName().'_'.$value] ->object_references[$relatedTable->getClassname().'_'.$value]
->getByName($column->getRelatedName(), BasePeer::TYPE_COLNAME); ->getByName($column->getRelatedName(), BasePeer::TYPE_COLNAME);
} }
} }

View file

@ -10,56 +10,21 @@
namespace Propel\PropelBundle\Tests\DataFixtures\Dumper; namespace Propel\PropelBundle\Tests\DataFixtures\Dumper;
use Propel\PropelBundle\Tests\TestCase; use Propel\PropelBundle\Tests\DataFixtures\TestCase;
use Propel\PropelBundle\DataFixtures\Dumper\YamlDataDumper; use Propel\PropelBundle\DataFixtures\Dumper\YamlDataDumper;
/** /**
* @author William Durand <william.durand1@gmail.com> * @author William Durand <william.durand1@gmail.com>
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/ */
class YamlDataDumperTest extends TestCase class YamlDataDumperTest extends TestCase
{ {
public function setUp()
{
parent::setUp();
$this->loadPropelQuickBuilder();
$schema = <<<XML
<database name="default" package="vendor.bundles.Propel.PropelBundle.Tests.Fixtures.DataFixtures.Loader" namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader" defaultIdMethod="native">
<table name="book">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
<column name="author_id" type="integer" required="false" defaultValue="null" />
<foreign-key foreignTable="book_author" onDelete="RESTRICT" onUpdate="CASCADE">
<reference local="author_id" foreign="id" />
</foreign-key>
</table>
<table name="book_author">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
</table>
</database>
XML;
$builder = new \PropelQuickBuilder();
$builder->setSchema($schema);
if (!class_exists('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book')) {
$builder->setClassTargets(array('peer', 'object', 'query', 'peerstub', 'objectstub', 'querystub'));
} else {
$builder->setClassTargets(array());
}
$this->con = $builder->build();
}
public function testYamlDump() public function testYamlDump()
{ {
$author = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor(); $author = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor();
$author->setName('A famous one')->save($this->con); $author->setName('A famous one')->save($this->con);
$book = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book; $book = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book();
$book $book
->setName('An important one') ->setName('An important one')
->setAuthorId(1) ->setAuthorId(1)

View file

@ -10,45 +10,27 @@
namespace Propel\PropelBundle\Tests\DataFixtures\Loader; namespace Propel\PropelBundle\Tests\DataFixtures\Loader;
use Propel\PropelBundle\Tests\TestCase; use Propel\PropelBundle\Tests\DataFixtures\TestCase;
/** /**
* @author Toni Uebernickel <tuebernickel@gmail.com> * @author Toni Uebernickel <tuebernickel@gmail.com>
*/ */
class DataWiperTest extends TestCase class DataWiperTest extends TestCase
{ {
protected $con = null;
public function setUp()
{
$this->loadPropelQuickBuilder();
$schema = <<<SCHEMA
<database name="book" defaultIdMethod="native">
<table name="book" phpName="WipeTestBook">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" size="255" primaryString="true" />
<column name="slug" type="varchar" size="255" />
</table>
</database>
SCHEMA;
$builder = new \PropelQuickBuilder();
$builder->setSchema($schema);
$this->con = $builder->build();
}
public function testWipesExistingData() public function testWipesExistingData()
{ {
$book = new \WipeTestBook(); $author = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor();
$author->setName('Some famous author');
$book = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book();
$book $book
->setName('Armageddon is near') ->setName('Armageddon is near')
->setSlug('armageddon-is-near') ->setBookAuthor($author)
->save($this->con) ->save($this->con)
; ;
$savedBook = \WipeTestBookPeer::doSelectOne(new \Criteria(), $this->con); $savedBook = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelectOne(new \Criteria(), $this->con);
$this->assertInstanceOf('WipeTestBook', $savedBook, 'The fixture has been saved correctly.'); $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book', $savedBook, 'The fixture has been saved correctly.');
$builder = $this->getMockBuilder('Propel\PropelBundle\DataFixtures\Loader\DataWiper'); $builder = $this->getMockBuilder('Propel\PropelBundle\DataFixtures\Loader\DataWiper');
$wipeout = $builder $wipeout = $builder
@ -57,8 +39,8 @@ SCHEMA;
->getMock() ->getMock()
; ;
$dbMap = new \DatabaseMap('book'); $dbMap = new \DatabaseMap('default');
$dbMap->addTableFromMapClass('WipeTestBookTableMap'); $dbMap->addTableFromMapClass('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\map\BookTableMap');
$reflection = new \ReflectionObject($wipeout); $reflection = new \ReflectionObject($wipeout);
$property = $reflection->getProperty('dbMap'); $property = $reflection->getProperty('dbMap');
$property->setAccessible(true); $property->setAccessible(true);
@ -69,8 +51,8 @@ SCHEMA;
->method('loadMapBuilders') ->method('loadMapBuilders')
; ;
$wipeout->load(array(), 'book'); $wipeout->load(array(), 'default');
$this->assertCount(0, \WipeTestBookPeer::doSelect(new \Criteria(), $this->con)); $this->assertCount(0, \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con));
} }
} }

View file

@ -10,60 +10,49 @@
namespace Propel\PropelBundle\Tests\DataFixtures\Loader; namespace Propel\PropelBundle\Tests\DataFixtures\Loader;
use Propel\PropelBundle\Tests\TestCase; use Propel\PropelBundle\Tests\DataFixtures\TestCase;
use Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader; use Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader;
/** /**
* @author William Durand <william.durand1@gmail.com> * @author William Durand <william.durand1@gmail.com>
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/ */
class XmlDataLoaderTest extends TestCase class XmlDataLoaderTest extends TestCase
{ {
protected $tmpfile; protected $tmpfile;
public function setUp() protected function setUp()
{ {
parent::setUp();
$fixtures = <<<XML $fixtures = <<<XML
<Fixtures> <Fixtures>
<Bar Namespace="\Foo"> <BookAuthor Namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader">
<fb1 Id="10" Title="Hello" /> <BookAuthor_1 id="1" name="A famous one" />
<fb2 Id="20" Title="World" /> </BookAuthor>
</Bar> <Book Namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader">
<Book_1 id="1" name="An important one" author_id="BookAuthor_1" />
</Book>
</Fixtures> </Fixtures>
XML; XML;
$this->tmpfile = (string) tmpfile(); $this->tmpfile = (string) tmpfile();
file_put_contents($this->tmpfile, $fixtures); file_put_contents($this->tmpfile, $fixtures);
} }
public function tearDown() protected function tearDown()
{ {
unlink($this->tmpfile); unlink($this->tmpfile);
} }
public function testTransformDataToArray() public function testXmlLoad()
{ {
$loader = new TestableXmlDataLoader(); $loader = new XmlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$array = $loader->transformDataToArray($this->tmpfile); $loader->load(array($this->tmpfile), 'default');
$this->assertTrue(is_array($array), 'Result is an array'); $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
$this->assertEquals(1, count($array), 'There is one class'); $this->assertCount(1, $books);
$this->assertArrayHasKey('\Foo\Bar', $array);
$subarray = $array['\Foo\Bar']; $book = $books[0];
$this->assertTrue(is_array($subarray), 'Result contains a sub-array'); $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor', $book->getBookAuthor());
$this->assertEquals(2, count($subarray), 'There is two fixtures objects');
$this->assertArrayHasKey('fb1', $subarray);
$this->assertArrayHasKey('fb2', $subarray);
}
}
class TestableXmlDataLoader extends XmlDataLoader
{
public function __construct()
{
}
public function transformDataToArray($data)
{
return parent::transformDataToArray($data);
} }
} }

View file

@ -10,61 +10,51 @@
namespace Propel\PropelBundle\Tests\DataFixtures\Loader; namespace Propel\PropelBundle\Tests\DataFixtures\Loader;
use Propel\PropelBundle\Tests\TestCase; use Propel\PropelBundle\Tests\DataFixtures\TestCase;
use Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader; use Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader;
/** /**
* @author William Durand <william.durand1@gmail.com> * @author William Durand <william.durand1@gmail.com>
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/ */
class YamlDataLoaderTest extends TestCase class YamlDataLoaderTest extends TestCase
{ {
protected $tmpfile; protected $tmpfile;
public function setUp() protected function setUp()
{ {
$fixtures = <<<YML parent::setUp();
\Foo\Bar:
fb1: $fixtures = <<<YAML
Id: 10 Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor:
Title: Hello BookAuthor_1:
fb2: id: '1'
Id: 20 name: 'A famous one'
Title: World Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book:
YML; Book_1:
id: '1'
name: 'An important one'
author_id: BookAuthor_1
YAML;
$this->tmpfile = (string) tmpfile(); $this->tmpfile = (string) tmpfile();
file_put_contents($this->tmpfile, $fixtures); file_put_contents($this->tmpfile, $fixtures);
} }
public function tearDown() protected function tearDown()
{ {
unlink($this->tmpfile); unlink($this->tmpfile);
} }
public function testTransformDataToArray() public function testYamlLoad()
{ {
$loader = new TestableYamlDataLoader(); $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$array = $loader->transformDataToArray($this->tmpfile); $loader->load(array($this->tmpfile), 'default');
$this->assertTrue(is_array($array), 'Result is an array'); $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
$this->assertEquals(1, count($array), 'There is one class'); $this->assertCount(1, $books);
$this->assertArrayHasKey('\Foo\Bar', $array);
$subarray = $array['\Foo\Bar']; $book = $books[0];
$this->assertTrue(is_array($subarray), 'Result contains a sub-array'); $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor', $book->getBookAuthor());
$this->assertEquals(2, count($subarray), 'There is two fixtures objects');
$this->assertArrayHasKey('fb1', $subarray);
$this->assertArrayHasKey('fb2', $subarray);
}
}
class TestableYamlDataLoader extends YamlDataLoader
{
public function __construct()
{
}
public function transformDataToArray($data)
{
return parent::transformDataToArray($data);
} }
} }

View file

@ -0,0 +1,60 @@
<?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 Propel\PropelBundle\Tests\DataFixtures;
use Propel\PropelBundle\Tests\TestCase as BaseTestCase;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class TestCase extends BaseTestCase
{
/**
* @var \PropelPDO
*/
protected $con;
protected function setUp()
{
parent::setUp();
$this->loadPropelQuickBuilder();
$schema = <<<XML
<database name="default" package="vendor.bundles.Propel.PropelBundle.Tests.Fixtures.DataFixtures.Loader" namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader" defaultIdMethod="native">
<table name="book">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
<column name="author_id" type="integer" required="false" defaultValue="null" />
<foreign-key foreignTable="book_author" onDelete="RESTRICT" onUpdate="CASCADE">
<reference local="author_id" foreign="id" />
</foreign-key>
</table>
<table name="book_author">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
</table>
</database>
XML;
$builder = new \PropelQuickBuilder();
$builder->setSchema($schema);
if (!class_exists('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book')) {
$builder->setClassTargets(array('peer', 'object', 'query', 'peerstub', 'objectstub', 'querystub'));
} else {
$builder->setClassTargets(array());
}
$this->con = $builder->build();
}
}

View file

@ -20,7 +20,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/ */
class TestCase extends \PHPUnit_Framework_TestCase class TestCase extends \PHPUnit_Framework_TestCase
{ {
public function setUp() protected function setUp()
{ {
if (!file_exists($file = __DIR__.'/../vendor/propel/runtime/lib/Propel.php')) { if (!file_exists($file = __DIR__.'/../vendor/propel/runtime/lib/Propel.php')) {
$this->markTestSkipped('Propel is not available.'); $this->markTestSkipped('Propel is not available.');