Commit 0061bc82 authored by romanb's avatar romanb

new test setup refactorings

parent 45235a15
...@@ -1277,7 +1277,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1277,7 +1277,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @return boolean whether or not the export operation was successful * @return boolean whether or not the export operation was successful
* false if table already existed in the database * false if table already existed in the database
*/ */
public function exportTable(Doctrine_Table $table) public function exportTable(Doctrine_ClassMetadata $metadata)
{ {
/** /**
TODO: maybe there should be portability option for the following check TODO: maybe there should be portability option for the following check
...@@ -1287,10 +1287,10 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1287,10 +1287,10 @@ class Doctrine_Export extends Doctrine_Connection_Module
*/ */
try { try {
$data = $table->getExportableFormat(); $data = $metadata->getExportableFormat();
$this->conn->export->createTable($data['tableName'], $data['columns'], $data['options']); $this->conn->export->createTable($data['tableName'], $data['columns'], $data['options']);
} catch(Doctrine_Connection_Exception $e) { } catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors // we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
throw $e; throw $e;
......
...@@ -17,7 +17,7 @@ class Orm_Component_AllTests ...@@ -17,7 +17,7 @@ class Orm_Component_AllTests
public static function suite() public static function suite()
{ {
$suite = new Doctrine_TestSuite('Doctrine Dbal Component'); $suite = new Doctrine_TestSuite('Doctrine Orm Component');
$suite->addTestSuite('Orm_Component_TestTest'); $suite->addTestSuite('Orm_Component_TestTest');
......
...@@ -3,6 +3,11 @@ require_once 'lib/DoctrineTestInit.php'; ...@@ -3,6 +3,11 @@ require_once 'lib/DoctrineTestInit.php';
class Orm_Component_TestTest extends Doctrine_OrmTestCase class Orm_Component_TestTest extends Doctrine_OrmTestCase
{ {
protected function setUp()
{
$this->loadFixture('forum', 'someusers');
}
public function testTest() public function testTest()
{ {
$this->assertEquals(0, 0); $this->assertEquals(0, 0);
......
---
CmsUser:
CmsUser_1:
username: jwage
password: changeme
\ No newline at end of file
<?php
$fixture = array(
'tableName' => 'dummy',
'rows' => array(
array(
'column1' => 'value1',
'column2' => 'value2',
'column3' => 'value3'
),
array(
'column1' => 'value4',
'column2' => 'value5',
'column3' => 'value6'
)
)
);
\ No newline at end of file
<?php
$fixture = array(
'model' => 'ForumUser',
'rows' => array(
array(
'id' => 1,
'username' => 'romanb'
),
array(
'id' => 2,
'username' => 'jwage'
)
)
);
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* Base testcase class for all dbal testcases. * Base testcase class for all dbal testcases.
*/ */
class Doctrine_DbalTestCase extends PHPUnit_Framework_TestCase class Doctrine_DbalTestCase extends Doctrine_TestCase
{ {
} }
\ No newline at end of file
...@@ -21,9 +21,5 @@ class Doctrine_DbalTestSuite extends Doctrine_TestSuite ...@@ -21,9 +21,5 @@ class Doctrine_DbalTestSuite extends Doctrine_TestSuite
} }
protected function tearDown() protected function tearDown()
{ {}
Doctrine_Manager::getInstance()->getConnection('sqlite_memory')->close();
$this->sharedFixture = NULL;
}
} }
\ No newline at end of file
...@@ -2,10 +2,63 @@ ...@@ -2,10 +2,63 @@
/** /**
* Base testcase class for all orm testcases. * Base testcase class for all orm testcases.
* *
* Provides the testcases with fixture support and other orm related capabilities.
*/ */
class Doctrine_OrmTestCase extends Doctrine_TestCase class Doctrine_OrmTestCase extends Doctrine_TestCase
{ {
private $_loadedFixtures = array();
private static $_fixtures = array();
private static $_exportedTables = array();
protected function loadFixture($package, $name)
{
$uniqueName = $package . '/' . $name;
if ( ! isset(self::$_fixtures[$uniqueName])) {
// load fixture file
$fixtureFile = 'fixtures' . DIRECTORY_SEPARATOR . 'orm' . DIRECTORY_SEPARATOR
. $package . DIRECTORY_SEPARATOR . $name . '.php';
require $fixtureFile;
self::$_fixtures[$uniqueName] = $fixture;
// load model file
$modelFile = 'models' . DIRECTORY_SEPARATOR . $package . DIRECTORY_SEPARATOR .
$fixture['model'] . '.php';
require $modelFile;
}
$fixture = self::$_fixtures[$uniqueName];
$this->_loadedFixtures[] = $fixture['model'];
$conn = $this->sharedFixture['connection'];
$classMetadata = $conn->getClassMetadata($fixture['model']);
$tableName = $classMetadata->getTableName();
if ( ! in_array($tableName, self::$_exportedTables)) {
$conn->export->exportClasses(array($fixture['model']));
self::$_exportedTables[] = $tableName;
}
foreach ($fixture['rows'] as $row) {
$conn->insert($classMetadata, $row);
}
}
protected function loadFixtures($package, array $names)
{
foreach ($names as $name) {
$this->loadFixture($package, $name);
}
}
protected function tearDown()
{
$conn = $this->sharedFixture['connection'];
foreach (array_reverse($this->_loadedFixtures) as $model) {
$conn->exec("DELETE FROM " . $conn->getClassMetadata($model)->getTableName());
}
}
/*
public function loadFixturesPackage($package, $models = array()) public function loadFixturesPackage($package, $models = array())
{ {
$packagePath = 'fixtures' . DIRECTORY_SEPARATOR . $package; $packagePath = 'fixtures' . DIRECTORY_SEPARATOR . $package;
...@@ -23,4 +76,5 @@ class Doctrine_OrmTestCase extends Doctrine_TestCase ...@@ -23,4 +76,5 @@ class Doctrine_OrmTestCase extends Doctrine_TestCase
$data = new Doctrine_Data(); $data = new Doctrine_Data();
$data->importData($dataPath, 'yml', $models); $data->importData($dataPath, 'yml', $models);
} }
*/
} }
\ No newline at end of file
...@@ -20,8 +20,5 @@ class Doctrine_OrmTestSuite extends Doctrine_TestSuite ...@@ -20,8 +20,5 @@ class Doctrine_OrmTestSuite extends Doctrine_TestSuite
} }
protected function tearDown() protected function tearDown()
{ {}
Doctrine_Manager::getInstance()->getConnection('sqlite_memory')->close();
$this->sharedFixture = NULL;
}
} }
\ No newline at end of file
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
*/ */
class Doctrine_TestCase extends PHPUnit_Framework_TestCase class Doctrine_TestCase extends PHPUnit_Framework_TestCase
{ {
} }
\ No newline at end of file
<?php
class ForumUser extends Doctrine_Record
{
public static function initMetadata($class)
{
$class->setColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true));
$class->setColumn('username', 'string', 255);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment