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

Adding test bootstrap.php file so .env vars are read #366

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions phpunit/phpunit/4.7/phpunit.xml.dist
Expand Up @@ -5,16 +5,16 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="SHELL_VERBOSITY" value="-1" />

<!-- override or set env variables for the test env here -->
<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" />
<!-- define your env variables for the test env here -->
</php>

<testsuites>
Expand Down
23 changes: 23 additions & 0 deletions phpunit/phpunit/4.7/tests/bootstrap.php
@@ -0,0 +1,23 @@
<?php

require __DIR__.'/../vendor/autoload.php';

use Symfony\Component\Dotenv\Dotenv;

/*
* Environment variables can also be specified in phpunit.xml.dist.
* Those variables will override any defined in .env.
*/

if (!isset($_SERVER['APP_ENV']) || 'prod' !== $_SERVER['APP_ENV']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use getenv('APP_ENV') instead of $_SERVER['APP_ENV']. Are there any reasons behind? Symfony recommends to use getenv() in docs but has $_SERVER[] in code which is not consistent as for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #156 about that. It should probably be updated in the docs then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getenv is an issue because of not being thread safe (probably less an issue in the PHPUnit bootstrap as this is used only in the CLI)

if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}

$debug = $_SERVER['APP_DEBUG'] ?? true;

if ($debug) {
umask(0000);
}
2 changes: 1 addition & 1 deletion symfony/console/3.3/bin/console
Expand Up @@ -15,7 +15,7 @@ if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}

if (!isset($_SERVER['APP_ENV'])) {
if (!isset($_SERVER['APP_ENV']) || 'prod' !== $_SERVER['APP_ENV']) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
Expand Down
2 changes: 1 addition & 1 deletion symfony/framework-bundle/3.3/public/index.php
Expand Up @@ -8,7 +8,7 @@
require __DIR__.'/../vendor/autoload.php';

// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
if (!isset($_SERVER['APP_ENV']) || 'prod' !== $_SERVER['APP_ENV']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had an issue trying to warm-up my cache on the CI.
My APP_ENV was test and I was overriding the env with the --env option like so :

bin/console --env=prod cache:clear --no-warmup
[08-Mar-2018 16:54:49 Europe/Paris] PHP Fatal error:  Uncaught RuntimeException: APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file. in /app/bin/console:20

I can see that my case is kind of weird and I am going to change the way I ran the command but we could do something to improve DX in that case perhaps ?

if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
Expand Down
8 changes: 4 additions & 4 deletions symfony/phpunit-bridge/3.3/phpunit.xml.dist
Expand Up @@ -5,16 +5,16 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="SHELL_VERBOSITY" value="-1" />

<!-- override or set env variables for the test env here -->
<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" />
<!-- define your env variables for the test env here -->
</php>

<testsuites>
Expand Down
22 changes: 22 additions & 0 deletions symfony/phpunit-bridge/3.3/tests/bootstrap.php
@@ -0,0 +1,22 @@
<?php

require __DIR__.'/../vendor/autoload.php';

use Symfony\Component\Dotenv\Dotenv;

/*
* Environment variables can also be specified in phpunit.xml.dist.
* Those variables will override any defined in .env.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

*/
if (!isset($_SERVER['APP_ENV'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouln't this be the same as for version 4.7?

if (!isset($_SERVER['APP_ENV']) || 'prod' !== $_SERVER['APP_ENV']) {

if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}

$debug = $_SERVER['APP_DEBUG'] ?? true;

if ($debug) {
umask(0000);
}