Codeception / module-laravel

Modern Laravel module for Codeception
MIT License
5 stars 13 forks source link

failed to use testing environment file specified in suite config #20

Closed krtorio closed 1 year ago

krtorio commented 7 years ago

What are you trying to achieve?

run all tests after adding a new suite called 'api', forgot to point environment file to .env.testing (sqlite + test mongodb database) in the suite config api.suite.yml.

What do you get instead?

The unit and functional tests used local .env instead which uses my staging/demo mariadb and local mongodb database. Encountered errors due to trying to insert rows with duplicate ids. Mongodb local data wiped out because I use truncate in tearDown function. Mariadb data filled up with test seed data that I did not want to be put in the local database.

The unit and functional tests returned to using .env.testing settings only after removing all api suite files.

Provide console output if related. Use -vvv mode for more details.

# paste output here

Provide test source code if related

// paste test

Details

# paste suite config here
actor: UnitTester
modules:
    enabled:
        - Laravel5:
            environment_file: .env.testing
            part: ORM
        - Db:
            dsn: 'sqlite:storage/testing.sqlite'
            user: ''
            password: ''
        - Asserts
        - \Helper\Unit

actor: FunctionalTester
modules:
    enabled:
        - Laravel5:
            environment_file: .env.testing
            run_database_migrations: true
            cleanup: true
        - REST:
           depends: PhpBrowser
           url: 'http://127.0.0.1:8000'
        - \Helper\Functional

actor: ApiTester
modules:
    enabled:
        - \Helper\Api
        - REST:
            url: 'http://127.0.0.1:8000/api/'
            depends: Laravel5
Naktibalda commented 7 years ago

.env is a default value of environment_file setting so what you described is an expected behaviour.

https://github.com/Codeception/Codeception/blob/2.3.5/src/Codeception/Module/Laravel5.php#L122

krtorio commented 7 years ago

If you look at my unit and functional suite configs you can see they are using .env.testing Does this mean all suites go back to using default if one suite uses .env?

Naktibalda commented 7 years ago

Ok, it makes sense now. I will take a look at it, unless @DavertMik gets it first.

DavertMik commented 7 years ago

Laravel5 module is recreated for each suite. Class is instantiated and configs do not overlap. Could you add codecept_debug($this->config) to trace the configuration changes?

krtorio commented 7 years ago

Hi,

Where am I supposed to put this? In one of the tests? It gives me an undefined property error exception when I do that

[ErrorException] Undefined property: ProductListCest::$config

Naktibalda commented 7 years ago

In Laravel5.php file

krtorio commented 7 years ago

codecept_debug($this->config) shows nothing.

I used var_dump instead, showed that suite config uses .env.testing as indicated in the yml file. Also I could not recreate the issue, maybe because I upgraded to 2.3.5?

besingamkb commented 4 years ago

i got the same issue on fresh laravel 7.X.. it works properly until I add an environment_file on unit.suite.yml or codeception.yml please see below

// unit.suite.yml
actor: UnitTester
modules:
    enabled:
        - Asserts
        - \Helper\Unit
        - Laravel5:
            environment_file: .env.testing
//codeception.yml
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    enabled:
        - Laravel5
            environment_file: .env.testing

Screenshot_3

barmax commented 2 years ago

I have the same problem with API tests. I have loaded .env.testing into the suite, but the Laravel does not use it. Api suite:

- REST:
    url: /
    depends: Laravel
- Laravel:
    environment_file: .env.testing

Laravel version is 7.18 Codeception version is 4.2.2

barmax commented 2 years ago

I have found the problem source. The Laravel creates an immutable environment repository instance \Dotenv\Repository\RepositoryInterface. The instance is RepositoryBuilder from the package vlucas/phpdotenv. When the Laravel tries change ENV-values to the .env-file from the Codeception, the immutable RepositoryBuilder does not.

The RepositoryBuilder has removed since version 5.0.

My stack:

I have prepared the pull request - https://github.com/Codeception/module-laravel/pull/46