Home > Net >  Access denied for user when running laravel test
Access denied for user when running laravel test

Time:12-01

i have a problem when i run tests in parallel mode i face this error :

PDOException: SQLSTATE[HY000] [1044] Access denied for user 'default'@'%' to database 'shop_test_4'

Or

330) Tests\Feature\Qualification\QualificationTest::testGetLikeProviders
PDOException: SQLSTATE[HY000] [1044] Access denied for user 'default'@'%' to database ':memory:'

and in my phpunit.xml i got this config :

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
         printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
>

    <extensions>
        <extension  />
    </extensions>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
         <server name="DB_CONNECTION" value="mysql"/>
<!--         <server name="DB_DATABASE" value=":memory:"/>-->
<!--        <env name="DB_CONNECTION" value="sqlite"/>-->
<!--        <env name="DB_DATABASE" value=":memory:"/>-->

        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

when i run the tests in normal way i got no problem

CodePudding user response:

I suspect the problem is that you have created a MySQL user (default) that has permissions to access a database shop_test. When you run the tests normally, Laravel just uses the shop_test database as you'd expect and everything works. When you run in parallel mode, Laravel uses more than one database to prevent collisions. The easiest solution is just to grant your database user access to all databases that begin with the prefix shop_test. Since you're using MySQL, then something along the lines of this answer should be helpful:

GRANT ALL ON `shop_test\_%`.* TO `default`@`%`;

If you're using SQLite, then you should be able to just use the default config because each test instance will have its own in-memory database.

  • Related