jeFFF's avatar
Level 3

DB issues when testing

Hello, I'm working on a project and for strange reasons testing give me troubles. On my current branch all works fine, but when I switch on another one tests are not working anymore, it resets completely my database which make me think that tests are not run in memory.

This is my phpunit config :

        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>

And when I launch : ./vendor/bin/phpunit I've got this message : PDOException: SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) which not match the sqlite value found in the XML file.

I tried all the solutions implying cache clearing but nothing works at all... the setup of PHPunit can be really tricky, if someone could help me with that it would be great !

0 likes
5 replies
Sinnbeck's avatar

Any chance that you are setting the connection explicitly on some of your models in the branch?

jeFFF's avatar
Level 3

Yes that's actually the case, the other branch is an importation branch from a DB to Another and the second dev put specific connexions for that. Is there a way to override this in case of testing ?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I have the same kind of issue. I made my models extend a model which has this

 /**
     * Connection to use
     *
     * @var string
     */
    protected $connection = 'hardcoded_connection'; //your hardcoded connection

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        if (App::environment('testing')) {
            $this->connection = 'testing'; //I have made this connection inside the database.php config file
        }
1 like
jeFFF's avatar
Level 3

Ah yes, thanks for the tip, I tried it and all the tests pass now.

Sinnbeck's avatar

If it was fixed, then please mark "Best answer" to set the thread as solved

1 like

Please or to participate in this conversation.