Home > Software engineering >  mock DB for api tests in symfony
mock DB for api tests in symfony

Time:06-30

I'm trying write API tests for Symfony

    public function testRegistration(): void
    {
        $response = static::createClient()->request(
            'POST',
            '/register',
            [
                'body' => json_encode([
                    'email' => '[email protected]',
                    'password' => '12345678'
                ])
            ]
        );

        $this->assertResponseIsSuccessful();
        $this->assertJsonContains([]);
    }

and I have this config

when@test:
    doctrine:
        dbal:
            driver:  pdo_sqlite
            memory:  true
            charset: UTF8

but get an error

An exception occurred while executing a query: SQLSTATE[HY000]: General error: 1 no such table: user

How to resolve this issue?

CodePudding user response:

It doesn't happen automatically. You need to update the schema in the database.

Use this (either in a setUp function or in your test functions) to setup the database.

$entityManager = $kernel->getContainer()->get('doctrine.orm.entity_manager');

$metadata = $entityManager->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool($entityManager);
$schemaTool->updateSchema($metadata);
  • Related