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 ($column->isForeignKey() && null !== $value) {
$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(
sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getPhpName())
);
}
$value = $this
->object_references[$relatedTable->getPhpName().'_'.$value]
->object_references[$relatedTable->getClassname().'_'.$value]
->getByName($column->getRelatedName(), BasePeer::TYPE_COLNAME);
}
}

View file

@ -10,56 +10,21 @@
namespace Propel\PropelBundle\Tests\DataFixtures\Dumper;
use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Tests\DataFixtures\TestCase;
use Propel\PropelBundle\DataFixtures\Dumper\YamlDataDumper;
/**
* @author William Durand <william.durand1@gmail.com>
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
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()
{
$author = new \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor();
$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
->setName('An important one')
->setAuthorId(1)

View file

@ -10,45 +10,27 @@
namespace Propel\PropelBundle\Tests\DataFixtures\Loader;
use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Tests\DataFixtures\TestCase;
/**
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
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()
{
$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
->setName('Armageddon is near')
->setSlug('armageddon-is-near')
->setBookAuthor($author)
->save($this->con)
;
$savedBook = \WipeTestBookPeer::doSelectOne(new \Criteria(), $this->con);
$this->assertInstanceOf('WipeTestBook', $savedBook, 'The fixture has been saved correctly.');
$savedBook = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelectOne(new \Criteria(), $this->con);
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book', $savedBook, 'The fixture has been saved correctly.');
$builder = $this->getMockBuilder('Propel\PropelBundle\DataFixtures\Loader\DataWiper');
$wipeout = $builder
@ -57,8 +39,8 @@ SCHEMA;
->getMock()
;
$dbMap = new \DatabaseMap('book');
$dbMap->addTableFromMapClass('WipeTestBookTableMap');
$dbMap = new \DatabaseMap('default');
$dbMap->addTableFromMapClass('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\map\BookTableMap');
$reflection = new \ReflectionObject($wipeout);
$property = $reflection->getProperty('dbMap');
$property->setAccessible(true);
@ -69,8 +51,8 @@ SCHEMA;
->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;
use Propel\PropelBundle\Tests\TestCase;
use Propel\PropelBundle\Tests\DataFixtures\TestCase;
use Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader;
/**
* @author William Durand <william.durand1@gmail.com>
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class XmlDataLoaderTest extends TestCase
{
protected $tmpfile;
public function setUp()
protected function setUp()
{
parent::setUp();
$fixtures = <<<XML
<Fixtures>
<Bar Namespace="\Foo">
<fb1 Id="10" Title="Hello" />
<fb2 Id="20" Title="World" />
</Bar>
<BookAuthor Namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader">
<BookAuthor_1 id="1" name="A famous one" />
</BookAuthor>
<Book Namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader">
<Book_1 id="1" name="An important one" author_id="BookAuthor_1" />
</Book>
</Fixtures>
XML;
$this->tmpfile = (string) tmpfile();
file_put_contents($this->tmpfile, $fixtures);
}
public function tearDown()
protected function tearDown()
{
unlink($this->tmpfile);
}
public function testTransformDataToArray()
public function testXmlLoad()
{
$loader = new TestableXmlDataLoader();
$array = $loader->transformDataToArray($this->tmpfile);
$loader = new XmlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$loader->load(array($this->tmpfile), 'default');
$this->assertTrue(is_array($array), 'Result is an array');
$this->assertEquals(1, count($array), 'There is one class');
$this->assertArrayHasKey('\Foo\Bar', $array);
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
$this->assertCount(1, $books);
$subarray = $array['\Foo\Bar'];
$this->assertTrue(is_array($subarray), 'Result contains a sub-array');
$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);
$book = $books[0];
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor', $book->getBookAuthor());
}
}

View file

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

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
{
public function setUp()
protected function setUp()
{
if (!file_exists($file = __DIR__.'/../vendor/propel/runtime/lib/Propel.php')) {
$this->markTestSkipped('Propel is not available.');