Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In memory sqlite always empty despite setup

I followed sitepoints Testing Symfony Apps with a Disposable Database Tutorial. I added Fixtures in my Testcase and no Errors appear during SetUp. If i add an Error in the Fixtures (e.g. leaving a nullable=false field empty) the Error is shown, so this code does definitely get executed.

My Config:

doctrine:
    dbal:
        default_connection: memory
        connections:
            memory:
                driver: pdo_sqlite
                memory: true
                charset: UTF8

My SetUp in my WebTestCase:

protected function setUp() {
    parent::setUp();
    self::bootKernel();
    DatabasePrimer::prime(self::$kernel);
    $this->loadFixtures([
        'AppBundle\DataFixtures\ORM\UserData',
        'AppBundle\DataFixtures\ORM\ArtistData'
    ]);
}

Yet, in my WebTestCase it appears that no Tables exist. The output throws a Doctrine Exception saying my table does not exist.

SQLSTATE[HY000]: General error: 1 no such table: my_user_table

If i switch to sql_lite in a file, everything works fine without any other changes:

dbal:
    default_connection: file
    connections:
        file:
            driver:   pdo_sqlite
            path:     %kernel.cache_dir%/test.db
            charset: UTF8

Anyone had success with said tutorial or using a sqlite memory db for unit tests and has any hints or ideas?

Update: I changed my Setup to this to ensure the kernel is not shut down in between. It did not help:

parent::setUp();
$this->client = $this->getClient();
MemoryDbPrimer::prime(self::$kernel);
$this->loadFixtures([
    'AppBundle\DataFixtures\ORM\UserData',
    'AppBundle\DataFixtures\ORM\ArtistData'
]);
like image 517
Andresch Serj Avatar asked Jan 27 '17 11:01

Andresch Serj


2 Answers

When you

$client->request(<METHOD>, <URL>);

which calls

Symfony\Bundle\FrameworkBundleClient::doRequest($request)

After the request the kernel is shutdown by default, and your in-memory database is trashed.

If you call

client->disableReboot(); 

in the setup() function of your test, this will behavior is disabled, and you can run the whole suite.

like image 152
Lighthart Avatar answered Nov 30 '22 07:11

Lighthart


I assume you call createClient() in your test functions. The very first thing that createClient() does is call static::bootKernel(). This basically means that the kernel you booted in your setUp() gets shut down and a new kernel is booted, with a fresh instance of the memory SQLite database.

You can move the createClient() call into your setUp(), replacing the bootKernel(), to avoid this:

class MyTest extends WebTestCase
{
    private $client = null;

    public function setUp()
    {
        $this->client = static::createClient();
        // prime database
    }

    public function testSomething()
    {
        $crawler = $this->client->request('GET', '/');
        // ...
    }
}
like image 33
aferber Avatar answered Nov 30 '22 06:11

aferber