Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony: environment: 'prod' not working #88

Closed
ThomasLandauer opened this issue Jan 7, 2019 · 5 comments
Closed

Symfony: environment: 'prod' not working #88

ThomasLandauer opened this issue Jan 7, 2019 · 5 comments
Milestone

Comments

@ThomasLandauer
Copy link
Member

What are you trying to achieve?

Run tests in Symfony's PROD environment.

What do you get instead?

Tests are still run in TEST environment.

Details

To investigate this, I created a controller which just outputs the environment (return new Response($kernel->getEnvironment());). When browsing to its url I get whatever I have in Symfony's .env. Fine.

When I access the url with codeception (codecept_debug($I->grabPageSource());), I get whatever I write into functional.suite.yml, even an arbitrary string like foo.

However, if I put prod, the app isn't really run in PROD environment. The feature I want to test is if the correct error page is shown. But in DEV/TEST environment Symfony always shows its internal Exception Page, not my own error page (see https://symfony.com/doc/current/controller/error_pages.html#testing-error-pages-during-development ) - that's how I know that I am still in TEST environment in fact.

I even tried with a @prepare annotation ($symfony->_reconfigure(['environment' => 'prod']);) to switch just for this single test (cause that's what I originally intended).

  • Codeception version: 2.5.2
  • Symfony version: 4.2.1
  • Suite configuration:
actor: FunctionalTester
modules:
    enabled:
        - Symfony:
            app_path: 'src'
            environment: 'prod'
@gte451f
Copy link

gte451f commented Jan 19, 2019

I can confirm this problem but coming at it from another direction.
I'm using API-Platform and have different DB config values for Dev and Test environments.

Regardless of the environment I specify, only the main .env file is loaded.

actor: ApiTester
modules:
    enabled:
    - Symfony:
          app_path: 'src'
          environment: 'FOOBAR'

So to make things work, I have .env pointed to my test database and rely on .env.test and .env.dev to work correctly in the regular symfony app.

I've tracked the code to my-app/src/Kernel.php where the correct env value is passed in, but .env files are read in the wrong order.

@gte451f
Copy link

gte451f commented Jan 19, 2019

In the course of further research, I discovered that the .env files seem to have the desired affect over the environment value specified in the symfony configuration above. So I'm not setting environment in the Codeception > Symfony module. In its place, using .env files seems to work well enough for both the main symfony stack AND codeception

#codeception.yml
params:
    - .env.FOOBAR


# .env.test
###> symfony/framework-bundle ###
APP_ENV=FOOBAR
...

# tests/api.suite.yml
# no environment specified since it doesn't appear to have any affect
actor: ApiTester
modules:
    enabled:
    - Symfony:
          app_path: 'src'

@Naktibalda Naktibalda transferred this issue from Codeception/Codeception Jan 5, 2021
@TavoNiievez
Copy link
Member

@ThomasLandauer Can you reproduce this problem in the test project?

I need to know if this is still a problem with the supported versions of Symfony, at least Codeception 4 and version 1.6 of this module.

I am not able to reproduce or fix the problem with the versions described in this issue, so please update it.

@ThomasLandauer
Copy link
Member Author

ThomasLandauer commented Mar 16, 2021

Yes, the issue is still there. I created https://github.com/ThomasLandauer/symfony-module-tests/tree/issue_88
Run vendor/bin/codecept run Functional EnvironmentCest:try -vvv, and you'll see "foo" in the output, which is the string I put in Functional.suite.yml.
However, when you open /public/index.php/environment in a browser, you'll see "prod".

@TavoNiievez
Copy link
Member

TavoNiievez commented Apr 30, 2024

This issue will be fixed with version 3.4.0 of the module.
A new config parameter will be added:

# tests/Functional.suite.yml
modules:
    enabled:
        - Symfony:
              bootstrap: true

Then you have to make sure that your codeception.yml file is not loading .env files.

You will have to have an .env.prod file and enable it in the Functional.suite.yml file as well.
You will have to enable the services that are not public for the prod environment in config/services_prod.php, you will also have to enable the necessary bundles for the symfony module to run the tests well (WebProfiler for example) in your config/bundles.php file.

After doing all this you will have to move the necessary settings in your config/packages/prod/ and config/routes/prod/ configuration files.

I'm still not quite clear on the use case where you would want to run tests in this environment, but at least it's already possible to do so.

@TavoNiievez TavoNiievez added this to the 3.4.0 milestone Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants