add m:n test for data loader

* refactor tests of DataFixtures
This commit is contained in:
Toni Uebernickel 2012-03-05 15:00:48 +01:00
parent e87c020313
commit eece448443
4 changed files with 106 additions and 35 deletions

View file

@ -31,8 +31,7 @@ class YamlDataDumperTest extends TestCase
->save($this->con)
;
$filename = tempnam(sys_get_temp_dir(), 'yaml_datadumper_test');
@unlink($filename);
$filename = $this->getTempFile();
$loader = new YamlDataDumper(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$loader->dump($filename);
@ -52,7 +51,5 @@ YAML;
$result = file_get_contents($filename);
$this->assertEquals($expected, $result);
@unlink($filename);
}
}

View file

@ -19,12 +19,8 @@ use Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader;
*/
class XmlDataLoaderTest extends TestCase
{
protected $tmpfile;
protected function setUp()
public function testXmlLoad()
{
parent::setUp();
$fixtures = <<<XML
<Fixtures>
<BookAuthor Namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader">
@ -35,19 +31,11 @@ class XmlDataLoaderTest extends TestCase
</Book>
</Fixtures>
XML;
$this->tmpfile = (string) tmpfile();
file_put_contents($this->tmpfile, $fixtures);
}
protected function tearDown()
{
unlink($this->tmpfile);
}
$filename = $this->getTempFile($fixtures);
public function testXmlLoad()
{
$loader = new XmlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$loader->load(array($this->tmpfile), 'default');
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
$this->assertCount(1, $books);

View file

@ -19,12 +19,8 @@ use Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader;
*/
class YamlDataLoaderTest extends TestCase
{
protected $tmpfile;
protected function setUp()
public function testYamlLoadOneToMany()
{
parent::setUp();
$fixtures = <<<YAML
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor:
BookAuthor_1:
@ -37,19 +33,10 @@ Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book:
author_id: BookAuthor_1
YAML;
$this->tmpfile = (string) tmpfile();
file_put_contents($this->tmpfile, $fixtures);
}
$filename = $this->getTempFile($fixtures);
protected function tearDown()
{
unlink($this->tmpfile);
}
public function testYamlLoad()
{
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$loader->load(array($this->tmpfile), 'default');
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
$this->assertCount(1, $books);
@ -57,4 +44,68 @@ YAML;
$book = $books[0];
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor', $book->getBookAuthor());
}
public function testYamlLoadManyToMany()
{
$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" phpName="YamlManyToManyBook">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
</table>
<table name="author" phpName="YamlManyToManyAuthor">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
</table>
<table name="book_author" phpName="YamlManyToManyBookAuthor">
<column name="book_id" type="integer" required="true" primaryKey="true" />
<column name="author_id" type="integer" required="true" primaryKey="true" />
<foreign-key foreignTable="book" phpName="Book" onDelete="CASCADE" onUpdate="CASCADE">
<reference local="book_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="author" phpName="Author" onDelete="CASCADE" onUpdate="CASCADE">
<reference local="author_id" foreign="id" />
</foreign-key>
</table>
</database>
XML;
$fixtures = <<<YAML
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthor:
Author_1:
name: 'A famous one'
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBook:
Book_1:
name: 'An important one'
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthor:
BookAuthor_1:
book_id: Book_1
author_id: Author_1
YAML;
$filename = $this->getTempFile($fixtures);
$builder = new \PropelQuickBuilder();
$builder->setSchema($schema);
$con = $builder->build();
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookPeer::doSelect(new \Criteria(), $con);
$this->assertCount(1, $books);
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBook', $books[0]);
$authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthorPeer::doSelect(new \Criteria(), $con);
$this->assertCount(1, $authors);
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyAuthor', $authors[0]);
$bookAuthors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthorPeer::doSelect(new \Criteria(), $con);
$this->assertCount(1, $bookAuthors);
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthor', $bookAuthors[0]);
}
}

View file

@ -22,6 +22,13 @@ class TestCase extends BaseTestCase
*/
protected $con;
/**
* The list of created temp files to be removed.
*
* @var array
*/
protected $tmpFiles = array();
protected function setUp()
{
parent::setUp();
@ -57,4 +64,32 @@ XML;
$this->con = $builder->build();
}
protected function tearDown()
{
foreach ($this->tmpFiles as $eachFile) {
@unlink($eachFile);
}
$this->tmpFiles = array();
}
/**
* Return the name of a created temporary file containing the given content.
*
* @param string $content
*
* @return string
*/
protected function getTempFile($content = '')
{
$filename = tempnam(sys_get_temp_dir(), 'propelbundle-datafixtures-test');
@unlink($filename);
file_put_contents($filename, $content);
$this->tmpFiles[] = $filename;
return $filename;
}
}