3

I try to do some tests with Symfony 4 and phpunit 6, but i have an error message :

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected" at /var/www/userDemo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 108 F
2 / 2 (100%)

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class RegistrationControllerTest extends WebTestCase
{
    public function testSomething()
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/');

        $this->assertSame(200, $client->getResponse()->getStatusCode());
        $this->assertSame(0, $crawler->filter('html:contains("Hello World")')->count());
    }

    public function testCheckPassword(){
        $client = static::createClient();

        $crawler = $client->request(
            'GET',
            '/register'
        );

        $form = $crawler->selectButton('S\'inscrire')->form();

        $form['user[email]'] = '[email protected]';
        $form['user[username]'] = 'usernametest';
        $form['user[fullName]'] = 'John Doe';
        $form['user[password][first]'] = 'pass1';
        $form['user[password][second]'] = 'pass2';

        $crawler = $client->submit($form);

        //echo $client->getResponse()->getContent();


        $this->assertEquals(1,
            $crawler->filter('li:contains("This value is not valid.")')->count()
        );


    }

}

My env file :

APP_ENV=dev APP_SECRET=XXXX DATABASE_URL=mysql://root:@127.0.0.1:3306/userdemo

This application work well in dev environnement

Thank you !

[EDIT] Just add in phpunit.xml.dist

<env name="DATABASE_URL" value="mysql://root:@127.0.0.1/userDemo" />

Thank you to @dbrumann

5
  • 1
    Can you add your .env especially the DATABASE_URL looks like? I assume it's missing the database name there. Make sure to also check the config/packages/tests/ folderand your phpunit.xml in case it overwrites the value. Commented Jan 3, 2018 at 21:35
  • done. ty. phpunit.xml.dist is the original file, i dont edit this one. Commented Jan 3, 2018 at 21:40
  • In that case check the phpunit.xml.dist for an env-entry in the <php> section. It should match the database url in your env file. Commented Jan 4, 2018 at 8:48
  • 2
    That's work !! ty, just add : <env name="DATABASE_URL" value="mysql://root:@127.0.0.1/userDemo" /> in phpunit.xml.dist Commented Jan 4, 2018 at 14:47
  • Glad I could help Commented Jan 4, 2018 at 14:58

1 Answer 1

3

Documentation explains how to fix your problem pretty well in Changing Database Settings for Functional Tests:

If you have functional tests, you want them to interact with a real database. Most of the time you want to use a dedicated database connection to make sure not to overwrite data you entered when developing the application and also to be able to clear the database before every test.

To do this, you can override the value of the DATABASE_URL env var in the phpunit.xml.dist to use a diferent database for your tests:

<?xml version="1.0" charset="utf-8" ?>
<phpunit>
    <php>
        <!-- the value is the Doctrine connection string in DSN format -->
        <env name="DATABASE_URL" value="mysql://USERNAME:[email protected]/DB_NAME" />
    </php>
</phpunit>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Jakub, but document dont explain how to create database + schema with command line, by example : php bin/console doctrine:database:create --env=test doesnt work :( (in sf4)
That wasn't your original question. If you feel something is missing from the documentation create an issue or a pull request in the docs repo: github.com/symfony/symfony-docs

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.