Symfony 6 UnitTest cannot create database for test environnement

Issue

I am working on a symfony project and I’m trying to write so unit test. I’ve followed every tutorials and they all end with having to do the same command at some point which is :

php bin/console doctrine:database:create --env=test

But I keep getting this error and don’t know what to do.

An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'user'@'%' to database 'db_test'

Here is .env:

APP_ENV=dev
APP_SECRET=4ad7f4a1f3a580dd3dbcf5636f045612

DATABASE_URL="mysql://user:[email protected]:3306/db?serverVersion=mariadb-10.5.8"

CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'

and .env.test:

KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
DATABASE_URL="mysql://user:[email protected]:3306/db?serverVersion=mariadb-10.5.8"

my phpunit.xml.dist is untouched but here is the important part

<php>
    <ini name="display_errors" value="1" />
    <ini name="error_reporting" value="-1" />
    <server name="APP_ENV" value="test" force="true" />
    <server name="SHELL_VERBOSITY" value="-1" />
    <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
    <server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>

I found this error because when I first tried to launch this unit test :

<?php

namespace App\Tests;

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;

class UserApiTest extends ApiTestCase
{
    public function getAllUsers(): void
    {
        $response = static::createClient()->request('GET', '/api/users');

        $this->assertResponseIsSuccessful();
    }
}

with this command I got this error:

C:\User\api> ./vendor/bin/phpunit --testsuite "Project Test Suite"


There was 1 error:

1) App\Tests\UserEntityTest::testSearchByName
Doctrine\DBAL\Exception\ConnectionException: An exception occurred in the driver: SQLSTATE[HY000] [1044] Access denied for user 'user'@'%' to database 'db_test'

I should also add that I’m using Docker for my database:

version: '3'

services:
  db:
    image: mariadb
    ports:
      - "3306:3306"
    environment:
      MYSQL_USER: user
      MYSQL_DATABASE: db
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - db-data:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - db:db
    depends_on:
      - db
    ports:
      - 9000:80
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - /sessions

volumes:
    db-data:

I don’t know what to do I tried everything that I found and nothing seems to work.

Any help or suggestion would be greatly appreciated 🙂

Solution

Your MySql User don’t have got a right to create a database ?

An article how to grant your user for this :
https://chartio.com/resources/tutorials/how-to-grant-all-privileges-on-a-database-in-mysql/

Answered By – Gary Houbre

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published