Question

Whats the way to unit test Doctrine 2 models? I am using it with Zend Framework 1.11. It has Zend_Test which uses PHPUnit. I think the right thing to use is PHPUnit_Extensions_Database_TestCase. In Zend Framework, I can use Zend_Test_PHPUnit_Db. How can I modify the code to unit test Doctrine Models instead of Zend_Db classes.

1st, I think instead of using Zend_Db stuff, I have to use Doctrine's stuff instead

class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase
{
    private $_connectionMock;

    protected function getConnection()
    {
        if($this->_connectionMock == null) {
            $connection = Zend_Db::factory(...);
            $this->_connectionMock = $this->createZendDbConnection(
                $connection, 'zfunittests'
            );
            Zend_Db_Table_Abstract::setDefaultAdapter($connection);
        }
        return $this->_connectionMock;
    }
    ...
}
Was it helpful?

Solution 2

I found DoctrineExtensions that has extensions for PHPUnit via ORMTestCase. Usage looks something like

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");
    }
}

OTHER TIPS

What exactly do you want to test? If its just the model per se - thats not too difficult. Doctrine 2 models are just plain PHP objects with annotations containing the mapping information. Thats the good thing about Doctrine 2 being "persinstance ignorant": you can test the models just like any regular class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top