0

Question: How to start symfony server within test env?

The command takes no --env parameter and it does not recognize the option configured in .env.local. Prefixing the command with environment variables also has no effect. It always starts with the dev environment.

Isn't the server intended to be used for functional testing (I'm using panther)?

I start the server this way:

APP_ENV=test symfony local:server:start

But then then %kernel.environment% stil points to dev, is that acceptable/to be expected?

5
  • APP_ENV=test symfony local:server:start Commented Jan 15 at 17:21
  • OK, but then %kernel.environment% stil points to dev, is that acceptable/to be expected? Commented Jan 15 at 18:03
  • That is not to be expected. Where is it getting that value from? Commented Jan 15 at 18:31
  • 1
    Perhaps this is a misconception? How do you start phpunit? The tests are normally not run in the server environment, but on the command line... How do you check the value of %kernel.environment% and where? Commented Jan 15 at 19:46
  • Yeah, maybe a misconception. It's not about the phpunit side. I want the symfony:server also to be running on the test database, etc. I checked the %kernel.environment% by outputing it via a simple controller which is served by the symfony webserver. Commented Jan 16 at 2:01

1 Answer 1

0

The symfony local:server:start defaults to the dev environment and doesn’t respect the test configuration. The best approach is to either use php -S with APP_ENV=test, Panther’s internal web server, or configure public/index.php to detect test and adjust accordingly.

Solution 1 - php -S:

APP_ENV=test php -S 127.0.0.1:8000 -t public

Solution 2 - Use Symfony Panther:

1- Use Symfony CLI with --no-tls and proxy: When using Symfony Panther, instead of relying on the Symfony CLI, use Panther’s internal server:

$client = PantherTestCase::createPantherClient(['server' => ['APP_ENV' => 'test']]);

2- Or Functional Testing with Panther:

Panther automatically uses the test environment if properly set up, so make sure:

  • The .env.test file exists with the correct settings.
  • In phpunit.xml.dist: <env name="APP_ENV" value="test"/>

Learn more about symfony/panther

Solution is 3 - Update index.php:

Edit public/index.php to use the test environment if needed:

if ($_SERVER['APP_ENV'] === 'test') {
    $_SERVER['APP_DEBUG'] = false;
    putenv('APP_ENV=test');
}

Then, when starting:

APP_ENV=test symfony local:server:start --no-tls
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I didn't know that Panther has it's own webserver, which is probably the right thing to use for functional tests. Also, the setup is described in detail here: symfonycasts.com/screencast/last-stack/testing So I will stick to that. He also mentions that symfony server is running in dev mode, and switches to panther build in server right after mentioning it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.