lchrusciel / ApiTestCase

Handy PHPUnit test case for testing JSON APIs in your Symfony applications.
MIT License
410 stars 35 forks source link

Seperate test database #138

Closed chucky2305 closed 4 years ago

chucky2305 commented 5 years ago

Hello together,

i really like this libraby, but i have a problem. Maybe to due missing symfony skills, but i think this library does it on purpose.

In my projects i like to have to databases. One for local development. Lets call this myproject_dev. I can reset this database any time i want with the same contents.

For my functional / database tests i want to use a second database. Lets call is myproject_test.

It seems like this library always uses the "default" database, right? Is there a way, maybe with a setting in phpunit.xml.dist to control this behaviour?

Greetings, Chucky2305

lchrusciel commented 5 years ago

Hello Max, thanks for kind words.

What you expect is the default behavior of the Symfony application and has nothing in common with this library. If it is affecting dev database it means, depending on Symfony version, that either you didn't have test database configured in config files or DATABASE_URL env has been configured in dev mode.

I hope this will help you ;)

chucky2305 commented 5 years ago

You put me to the right point. I have a local ENV Variable that was pointing to my dev database. But after removing this env var it does not work either.

When i run the tests now, the database connection params are all filled with default values

In ApiTestCase.php on Line 106: var_dump($this->entityManager->getConnection());

gives me this:

...
private $_params =>
  array(11) {
    'driver' =>
    string(9) "pdo_mysql"
    'charset' =>
    string(7) "utf8mb4"
    'url' =>
    string(0) ""
    'host' =>
    string(9) "localhost"
    'port' =>
    NULL
    'user' =>
    string(4) "root"
    'password' =>
    NULL
    'driverOptions' =>
    array(0) {
    }
    'serverVersion' =>
    string(3) "5.7"
    'defaultTableOptions' =>
    array(2) {
      'charset' =>
      string(7) "utf8mb4"
      'collate' =>
      string(18) "utf8mb4_unicode_ci"
    }
    'dbname' =>
    string(0) ""
  }

...

although i have defined this in my test/doctrine.yaml

doctrine:
    dbal:
        url: 'mysql://project_user:project_pass@mariadb:3306/project_int'

and this is my phpunit.xml.dist

under <php>

<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="1" />
<env name="APP_SECRET" value="s$cretf0rt3st" />
<env name="SHELL_VERBOSITY" value="-1" />
<env name="SYMFONY_PHPUNIT_VERSION" value="7.4.3" />
<!--<env name="DATABASE_URL" value="mysql://premium_user:premium_pass@mariadb:3306/premium_int" />-->
<server name="FIXTURES_DIR" value="../tests/fixtures" />
<server name="IS_DOCTRINE_ORM_SUPPORTED" value="true" />

I can only run my tests with valid database settings, when i remove the comments from DATABASE_URL in phpunit.xml.dist

Any ideas , why the doctrine.yaml in my test folder is ignored?

lchrusciel commented 5 years ago

You may be right, that <!--<env name="DATABASE_URL" value="mysql://premium_user:premium_pass@mariadb:3306/premium_int" />--> may override config. Should be documented.

chucky2305 commented 5 years ago

I found out that APP_ENV in phpunit.xml.dist is not interpreted in all cases. Especially when you run you code on docker it can happen, that env variables that are stored in the php-container cannot be overwritten by code.

So setting DATABASE_URL in phpunit.xml.dist is one way to go. Another is documented here: https://github.com/symfony/flex/issues/251#issuecomment-364113857

lchrusciel commented 4 years ago

So I would consider it as solved