From f589d318eee4224b886a63c7e52ae05ca0696565 Mon Sep 17 00:00:00 2001 From: Toni Uebernickel Date: Mon, 5 Mar 2012 12:31:32 +0100 Subject: [PATCH] add tests for DataFixtures --- DataFixtures/Loader/AbstractDataLoader.php | 4 +- .../Dumper/YamlDataDumperTest.php | 41 +------------ Tests/DataFixtures/Loader/DataWiperTest.php | 44 ++++---------- .../DataFixtures/Loader/XmlDataLoaderTest.php | 49 ++++++--------- .../Loader/YamlDataLoaderTest.php | 60 ++++++++----------- Tests/DataFixtures/TestCase.php | 60 +++++++++++++++++++ Tests/TestCase.php | 2 +- 7 files changed, 123 insertions(+), 137 deletions(-) create mode 100644 Tests/DataFixtures/TestCase.php diff --git a/DataFixtures/Loader/AbstractDataLoader.php b/DataFixtures/Loader/AbstractDataLoader.php index c8886cc..e35f804 100644 --- a/DataFixtures/Loader/AbstractDataLoader.php +++ b/DataFixtures/Loader/AbstractDataLoader.php @@ -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); } } diff --git a/Tests/DataFixtures/Dumper/YamlDataDumperTest.php b/Tests/DataFixtures/Dumper/YamlDataDumperTest.php index 946a039..e4aa627 100644 --- a/Tests/DataFixtures/Dumper/YamlDataDumperTest.php +++ b/Tests/DataFixtures/Dumper/YamlDataDumperTest.php @@ -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 + * @author Toni Uebernickel */ class YamlDataDumperTest extends TestCase { - public function setUp() - { - parent::setUp(); - - $this->loadPropelQuickBuilder(); - - $schema = << - - - - - - - - -
- - - - -
- -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) diff --git a/Tests/DataFixtures/Loader/DataWiperTest.php b/Tests/DataFixtures/Loader/DataWiperTest.php index f533c5e..0da8b52 100644 --- a/Tests/DataFixtures/Loader/DataWiperTest.php +++ b/Tests/DataFixtures/Loader/DataWiperTest.php @@ -10,45 +10,27 @@ namespace Propel\PropelBundle\Tests\DataFixtures\Loader; -use Propel\PropelBundle\Tests\TestCase; +use Propel\PropelBundle\Tests\DataFixtures\TestCase; /** * @author Toni Uebernickel */ class DataWiperTest extends TestCase { - protected $con = null; - - public function setUp() - { - $this->loadPropelQuickBuilder(); - - $schema = << - - - - -
- -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)); } -} \ No newline at end of file +} diff --git a/Tests/DataFixtures/Loader/XmlDataLoaderTest.php b/Tests/DataFixtures/Loader/XmlDataLoaderTest.php index 9050126..b5f57fd 100644 --- a/Tests/DataFixtures/Loader/XmlDataLoaderTest.php +++ b/Tests/DataFixtures/Loader/XmlDataLoaderTest.php @@ -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 + * @author Toni Uebernickel */ class XmlDataLoaderTest extends TestCase { protected $tmpfile; - public function setUp() + protected function setUp() { + parent::setUp(); + $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()); } } diff --git a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php index adbd0a1..b09f761 100644 --- a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php +++ b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php @@ -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 + * @author Toni Uebernickel */ class YamlDataLoaderTest extends TestCase { protected $tmpfile; - public function setUp() + protected function setUp() { - $fixtures = <<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()); } } diff --git a/Tests/DataFixtures/TestCase.php b/Tests/DataFixtures/TestCase.php new file mode 100644 index 0000000..6111db3 --- /dev/null +++ b/Tests/DataFixtures/TestCase.php @@ -0,0 +1,60 @@ + + */ +class TestCase extends BaseTestCase +{ + /** + * @var \PropelPDO + */ + protected $con; + + protected function setUp() + { + parent::setUp(); + + $this->loadPropelQuickBuilder(); + + $schema = << + + + + + + + + +
+ + + + +
+ +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(); + } +} diff --git a/Tests/TestCase.php b/Tests/TestCase.php index bfd5bca..25625cf 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -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.');