Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for DataFixtures #117

Merged
merged 1 commit into from
Mar 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DataFixtures/Loader/AbstractDataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ protected function loadDataFromArray($data = null)
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);
}
}
Expand Down
41 changes: 3 additions & 38 deletions Tests/DataFixtures/Dumper/YamlDataDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 13 additions & 31 deletions Tests/DataFixtures/Loader/DataWiperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,8 +39,8 @@ public function testWipesExistingData()
->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);
Expand All @@ -69,8 +51,8 @@ public function testWipesExistingData()
->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));
}
}
}
49 changes: 19 additions & 30 deletions Tests/DataFixtures/Loader/XmlDataLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
60 changes: 25 additions & 35 deletions Tests/DataFixtures/Loader/YamlDataLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
60 changes: 60 additions & 0 deletions Tests/DataFixtures/TestCase.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
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.');
Expand Down