Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony / Doctrine UnitTests with SQLite memory DB

I'm still working on PHP unit tests for testing my symfony2 controllers. My test classes are derivations of WebTestCase and the tests are doing GET or POST requests do check if everything works fine. I want to test all underlying layers, but I don't want to mess up my database with the tests. I don't want to use mock ups, but an in-memory SQLite db, where I can set up a test scenario to check all modifications. I found a lot of hints how to do this with doctrine 1.x, but they don't work any more. So I want something like this:

class BlahblahTest extends WebTestCase {
    public function testXXXYYY() {
        // 1. Setup a new database with SQLite:memory:
        // 2. create the database and all tables according the entities in my project
        $this->createTestScenario(); // 3.
        $crawler = $this->client->request('GET', '/testpage');  // 4.
        // 5. Lots of checks against the database and / or the $crawler data
    }
}

Any chance to get this work?

Thanks in advance Hennes

like image 828
Hennes Avatar asked May 08 '14 09:05

Hennes


1 Answers

The accepted answer didn't work for me, because I was using the URL to specify the connection parameters, and although I was setting the driver to pdo_sqliteand memoryto true, Doctrine was still using the url parameter from the root configuration, so I had to overwrite it.

#api/config/packages/test/doctrine.yaml
doctrine:
  dbal:
    url: "sqlite:///:memory:"
like image 200
Jefferson Lima Avatar answered Sep 25 '22 01:09

Jefferson Lima