Permalink
Browse files

Finalized first version of Doctrine PHPUnit extension

  • Loading branch information...
beberlei committed Feb 18, 2010
0 parents commit 8445f00a169ba0d940d756aa998af6604d3973cb
@@ -0,0 +1,3 @@
+nbproject
+tests/phpunit.xml
+build/
@@ -0,0 +1,85 @@
+# Some Doctrine 2 Extensions
+
+This package contains several extensions to Doctrine 2 that hook into the facilities of Doctrine and
+offer new functionality or tools to use Doctrine 2 more efficently.
+
+## Including DoctrineExtensions
+
+To include the DoctrineExtensions should fire up an autoloader, for example:
+
+ $classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', "/path/to/extensions");
+ $classLoader->register();
+
+## PHPUnit
+
+The PHPUnit Extension for Doctrine offers several hooks into PHPUnits Database extension and offers a
+very convenient way to test your Doctrine 2 code against a Database.
+
+### Using the OrmTestCase:
+
+#### An Example
+
+ namespace MyProject\Tests;
+
+ use DoctrineExtensions\PHPUnit\OrmTestCase
+
+ class EntityFunctionalTest extends OrmTestCase
+ {
+ protected function createEntityManager()
+ {
+ return Doctrine\ORM\EntityManager::create(..);
+ }
+
+ protected function getDataSet()
+ {
+ return $this->createFlatXmlDataSet(__DIR__."/_files/entityFixture.xml");
+ }
+ }
+
+For more information see the PHPUnit Documentation on this topic: http://www.phpunit.de/manual/current/en/database.html
+
+#### Notes
+
+This PHPUnit extension does not create the database schema for you. It has to be created before you run the tests.
+If you want to dynamically create the schema you have to listen to the 'preTestSetUp' and 'postTestSetUp' events
+that are called before and after the fixture is loaded respectively.
+
+ namespace MyProject\Tests;
+
+ use DoctrineExtensions\PHPUnit\Event\EntityManagerEventArgs,
+ Doctrine\ORM\Tools\SchemaTool;
+
+ class SchemaSetupListener
+ {
+ public function preTestSetUp(EntityManagerEventArgs $eventArgs)
+ {
+ $em = $eventArgs->getEntityManager();
+
+ $schemaTool = new SchemaTool($em);
+
+ $cmf = $em->getMetadataFactory();
+ $classes = $cmf->getAllMetadata();
+
+ $schemaTool->dropSchema($classes);
+ $schemaTool->createSchema($classes);
+ }
+ }
+
+ class EntityFunctionalTest extends OrmTestCase
+ {
+ protected function createEntityManager()
+ {
+ $eventManager = new EventManager();
+ $eventManager->addEventListener(array("preTestSetUp"), new SchemaSetupListener());
+
+ return Doctrine\ORM\EntityManager::create(.., $eventManager);
+ }
+ }
+
+### TODOs:
+
+* Add a Filter to transform between Entity-Speak and database columns for the DataSet and DataTable interfaces.
+ This could also be used as basis for "fixture" management. XML or YAML fixture formats could be used to
+ fill the database initially.
+* Add a test-case that automatically tests the persistence of all your entities against the current mapping
+ schema and database, aswell as related entities.
@@ -0,0 +1,26 @@
+<?php
+/**
+ * DoctrineExtensions PHPUnit
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace DoctrineExtensions\PHPUnit\DataSet;
+
+class QueryDataSet extends \PHPUnit_Extensions_Database_DataSet_QueryDataSet
+{
+ public function addTable($tableName, $query = NULL)
+ {
+ if ($query === NULL) {
+ $query = 'SELECT * FROM ' . $tableName;
+ }
+
+ $this->tables[$tableName] = new QueryTable($tableName, $query, $this->databaseConnection);
+ }
+}
@@ -0,0 +1,46 @@
+<?php
+/**
+ * DoctrineExtensions PHPUnit
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace DoctrineExtensions\PHPUnit\DataSet;
+
+class QueryTable extends \PHPUnit_Extensions_Database_DataSet_QueryTable
+{
+ protected function loadData()
+ {
+ if ($this->data === NULL) {
+ $this->data = $this->databaseConnection->getConnection()->fetchAll($this->query);
+ }
+ }
+
+ protected function createTableMetaData()
+ {
+ if ($this->tableMetaData === null) {
+ $this->loadData();
+
+ /* @var $table Doctrine\DBAL\Schema\Table */
+ $table = $this->databaseConnection
+ ->getConnection()
+ ->getSchemaManager()
+ ->listTableDetails($this->tableName);
+
+ $columnNames = array();
+ foreach ($table->getColumns() AS $column) {
+ $columnNames[] = $column->getName();
+ }
+
+ $this->tableMetaData = new \PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
+ $this->tableName, $columnNames
+ );
+ }
+ }
+}
@@ -0,0 +1,79 @@
+<?php
+/**
+ * DoctrineExtensions PHPUnit
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace DoctrineExtensions\PHPUnit;
+use DoctrineExtensions\PHPUnit\Operations\Truncate;
+
+require_once "PHPUnit/Extensions/Database/TestCase.php";
+require_once 'PHPUnit/Extensions/Database/DataSet/QueryDataSet.php';
+require_once 'PHPUnit/Extensions/Database/DataSet/QueryTable.php';
+
+abstract class DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
+{
+ /**
+ * @var DoctrineExtensions\PHPUnit\TestConnection
+ */
+ private $_connection = null;
+
+ /**
+ * @return Doctrine\DBAL\Connection
+ */
+ abstract protected function getDoctrineConnection();
+
+ /**
+ * @return TestConnection
+ */
+ final protected function getConnection()
+ {
+ if ($this->_connection == null) {
+ $this->_connection = new TestConnection($this->getDoctrineConnection());
+ }
+ return $this->_connection;
+ }
+
+ /**
+ * Returns the database operation executed in test setup.
+ *
+ * @return \PHPUnit_Extensions_Database_Operation_DatabaseOperation
+ */
+ protected function getSetUpOperation()
+ {
+ return new \PHPUnit_Extensions_Database_Operation_Composite(array(
+ new Truncate(),
+ new \PHPUnit_Extensions_Database_Operation_Insert()
+ ));
+ }
+
+ /**
+ * @param array $tableNames
+ * @return \PHPUnit_Extensions_Database_DataSet_QueryDataSet
+ */
+ protected function createQueryDataSet(array $tableNames = null)
+ {
+ return $this->getConnection()->createDataSet($tableNames);
+ }
+
+ /**
+ * @param string $tableName
+ * @param string $sql
+ * @return \PHPUnit_Extensions_Database_DataSet_QueryTable
+ */
+ protected function createQueryDataTable($tableName, $sql = null)
+ {
+ if ($sql == null) {
+ $sql = 'SELECT * FROM '.$tableName;
+ }
+
+ return $this->getConnection()->createQueryTable($tableName, $sql);
+ }
+}
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Whitewashing
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace DoctrineExtensions\PHPUnit;
+
+require_once "PHPUnit/Extensions/Database/ITester.php";
+
+use Doctrine\ORM\EntityManager;
+use PHPUnit_Extensions_Database_DB_IDatabaseConnection AS IDatabaseConnection;
+use PHPUnit_Extensions_Database_DefaultTester AS DefaultTester;
+use PHPUnit_Extensions_Database_Operation_Factory AS OperationFactory;
+
+class DatabaseTester extends DefaultTester
+{
+ public function __construct(IDatabaseConnection $connection)
+ {
+ $this->connection = $connection;
+ $this->setUpOperation = new \PHPUnit_Extensions_Database_Operation_Composite(array(
+ new Operations\Truncate($connection->allowsCascading()),
+ OperationFactory::INSERT()
+ ));
+ $this->tearDownOperation = OperationFactory::NONE();
+ }
+}
Oops, something went wrong.

0 comments on commit 8445f00

Please sign in to comment.