From 0061bc827b2b0a0b76858f45d1e57be23b2dc94c Mon Sep 17 00:00:00 2001 From: romanb Date: Sun, 10 Feb 2008 14:32:05 +0000 Subject: [PATCH] new test setup refactorings --- lib/Doctrine/Export.php | 6 +- tests/Orm/Component/AllTests.php | 2 +- tests/Orm/Component/TestTest.php | 5 ++ tests/fixtures/cms/data/CmsUser.yml | 5 -- tests/fixtures/dbal/dummy.php | 16 ++++++ tests/fixtures/orm/forum/someusers.php | 14 +++++ tests/lib/Doctrine_DbalTestCase.php | 2 +- tests/lib/Doctrine_DbalTestSuite.php | 6 +- tests/lib/Doctrine_OrmTestCase.php | 56 ++++++++++++++++++- tests/lib/Doctrine_OrmTestSuite.php | 5 +- tests/lib/Doctrine_TestCase.php | 2 +- .../cms/models => models/cms}/CmsUser.php | 0 tests/models/forum/ForumUser.php | 9 +++ 13 files changed, 107 insertions(+), 21 deletions(-) delete mode 100644 tests/fixtures/cms/data/CmsUser.yml create mode 100644 tests/fixtures/dbal/dummy.php create mode 100644 tests/fixtures/orm/forum/someusers.php rename tests/{fixtures/cms/models => models/cms}/CmsUser.php (100%) create mode 100644 tests/models/forum/ForumUser.php diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index a3eb6dc5d..e88b5faf5 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1277,7 +1277,7 @@ class Doctrine_Export extends Doctrine_Connection_Module * @return boolean whether or not the export operation was successful * 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 @@ -1287,10 +1287,10 @@ class Doctrine_Export extends Doctrine_Connection_Module */ try { - $data = $table->getExportableFormat(); + $data = $metadata->getExportableFormat(); $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 if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { throw $e; diff --git a/tests/Orm/Component/AllTests.php b/tests/Orm/Component/AllTests.php index 417bb10be..3294b562d 100644 --- a/tests/Orm/Component/AllTests.php +++ b/tests/Orm/Component/AllTests.php @@ -17,7 +17,7 @@ class Orm_Component_AllTests public static function suite() { - $suite = new Doctrine_TestSuite('Doctrine Dbal Component'); + $suite = new Doctrine_TestSuite('Doctrine Orm Component'); $suite->addTestSuite('Orm_Component_TestTest'); diff --git a/tests/Orm/Component/TestTest.php b/tests/Orm/Component/TestTest.php index 52af8a462..5b06bb9b1 100644 --- a/tests/Orm/Component/TestTest.php +++ b/tests/Orm/Component/TestTest.php @@ -3,6 +3,11 @@ require_once 'lib/DoctrineTestInit.php'; class Orm_Component_TestTest extends Doctrine_OrmTestCase { + protected function setUp() + { + $this->loadFixture('forum', 'someusers'); + } + public function testTest() { $this->assertEquals(0, 0); diff --git a/tests/fixtures/cms/data/CmsUser.yml b/tests/fixtures/cms/data/CmsUser.yml deleted file mode 100644 index 6dd2a8cdb..000000000 --- a/tests/fixtures/cms/data/CmsUser.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -CmsUser: - CmsUser_1: - username: jwage - password: changeme \ No newline at end of file diff --git a/tests/fixtures/dbal/dummy.php b/tests/fixtures/dbal/dummy.php new file mode 100644 index 000000000..7b937bd18 --- /dev/null +++ b/tests/fixtures/dbal/dummy.php @@ -0,0 +1,16 @@ + 'dummy', + 'rows' => array( + array( + 'column1' => 'value1', + 'column2' => 'value2', + 'column3' => 'value3' + ), + array( + 'column1' => 'value4', + 'column2' => 'value5', + 'column3' => 'value6' + ) + ) +); \ No newline at end of file diff --git a/tests/fixtures/orm/forum/someusers.php b/tests/fixtures/orm/forum/someusers.php new file mode 100644 index 000000000..6d071f401 --- /dev/null +++ b/tests/fixtures/orm/forum/someusers.php @@ -0,0 +1,14 @@ + 'ForumUser', + 'rows' => array( + array( + 'id' => 1, + 'username' => 'romanb' + ), + array( + 'id' => 2, + 'username' => 'jwage' + ) + ) +); \ No newline at end of file diff --git a/tests/lib/Doctrine_DbalTestCase.php b/tests/lib/Doctrine_DbalTestCase.php index 7cb510c45..abb227589 100644 --- a/tests/lib/Doctrine_DbalTestCase.php +++ b/tests/lib/Doctrine_DbalTestCase.php @@ -2,7 +2,7 @@ /** * 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 diff --git a/tests/lib/Doctrine_DbalTestSuite.php b/tests/lib/Doctrine_DbalTestSuite.php index 407c591ad..6eb92ce6f 100644 --- a/tests/lib/Doctrine_DbalTestSuite.php +++ b/tests/lib/Doctrine_DbalTestSuite.php @@ -21,9 +21,5 @@ class Doctrine_DbalTestSuite extends Doctrine_TestSuite } protected function tearDown() - { - Doctrine_Manager::getInstance()->getConnection('sqlite_memory')->close(); - $this->sharedFixture = NULL; - } - + {} } \ No newline at end of file diff --git a/tests/lib/Doctrine_OrmTestCase.php b/tests/lib/Doctrine_OrmTestCase.php index 8130573ae..4e663e2f6 100644 --- a/tests/lib/Doctrine_OrmTestCase.php +++ b/tests/lib/Doctrine_OrmTestCase.php @@ -2,10 +2,63 @@ /** * Base testcase class for all orm testcases. * - * Provides the testcases with fixture support and other orm related capabilities. */ 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()) { $packagePath = 'fixtures' . DIRECTORY_SEPARATOR . $package; @@ -23,4 +76,5 @@ class Doctrine_OrmTestCase extends Doctrine_TestCase $data = new Doctrine_Data(); $data->importData($dataPath, 'yml', $models); } + */ } \ No newline at end of file diff --git a/tests/lib/Doctrine_OrmTestSuite.php b/tests/lib/Doctrine_OrmTestSuite.php index 8153fc599..4c047d6cd 100644 --- a/tests/lib/Doctrine_OrmTestSuite.php +++ b/tests/lib/Doctrine_OrmTestSuite.php @@ -20,8 +20,5 @@ class Doctrine_OrmTestSuite extends Doctrine_TestSuite } protected function tearDown() - { - Doctrine_Manager::getInstance()->getConnection('sqlite_memory')->close(); - $this->sharedFixture = NULL; - } + {} } \ No newline at end of file diff --git a/tests/lib/Doctrine_TestCase.php b/tests/lib/Doctrine_TestCase.php index 65724d9e4..4d89cdfe9 100644 --- a/tests/lib/Doctrine_TestCase.php +++ b/tests/lib/Doctrine_TestCase.php @@ -4,5 +4,5 @@ */ class Doctrine_TestCase extends PHPUnit_Framework_TestCase { - + } \ No newline at end of file diff --git a/tests/fixtures/cms/models/CmsUser.php b/tests/models/cms/CmsUser.php similarity index 100% rename from tests/fixtures/cms/models/CmsUser.php rename to tests/models/cms/CmsUser.php diff --git a/tests/models/forum/ForumUser.php b/tests/models/forum/ForumUser.php new file mode 100644 index 000000000..177f4b059 --- /dev/null +++ b/tests/models/forum/ForumUser.php @@ -0,0 +1,9 @@ +setColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true)); + $class->setColumn('username', 'string', 255); + } +} \ No newline at end of file