4

I'm starting to try and test my Doctrine objects with PHPUnit, and would like to reload the DB from my model objects afresh each time.

My first attempt looks something like this:

class Tests_User extends PHPUnit_Framework_TestCase
{

    public function setUp()
    {

        Doctrine_Manager::connection('mysql://user:pass@localhost/testdb');

        Doctrine::createDatabases();
        Doctrine::createTablesFromModels('../../application/models');

    }

    public function testSavingWorks()
    {

        $user = new User();
        $user->save();

    }

    public function testSavingWorksAgain()
    {

        $user = new User();
        $user->save();

    }

    public function tearDown()
    {

        Doctrine::dropDatabases();

    }

}

The problem is that when setUp() is called again for the second test, createTablesFromModels() fails, so I get an error because none of the tables are present.

I'd really appreciate an example of how someone else has reinitialised a Doctrine connection for PHPUnit or other unit testing purposes.

2 Answers 2

1

So it turns out that createTablesFromModels includes the files in and then compares the lists of defined classes before and after, which is why it's not working twice.

A sequence like the following works when repeated:

Doctrine::loadModels($path);
Doctrine::createTablesFromArray(Doctrine::getLoadedModels());
Sign up to request clarification or add additional context in comments.

Comments

1

Check out this Gist from Ocramius, which shows that you can test using data fixtures: https://gist.github.com/Ocramius/3994325

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.