Skip to content

Commit 49d3f9e

Browse files
committed
tests: Workaround to get fastest library to work with Symfony Flex. See liuggio/fastest#101
1 parent 9f28b54 commit 49d3f9e

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

Diff for: .env.dist

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ APP_SECRET=8822e9bb81605a603006c9260793d983
1212
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
1313
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
1414
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
15-
DATABASE_URL="mysql://[email protected]:3306/symfony?charset=utf8mb4&serverVersion=5.7"
15+
DATABASE_NAME="symfony"
16+
DATABASE_URL="mysql://[email protected]:3306/${DATABASE_NAME}?charset=utf8mb4&serverVersion=5.7"
1617
###< doctrine/doctrine-bundle ###
1718

1819
###> lexik/jwt-authentication-bundle ###

Diff for: .env.travis

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ APP_SECRET=7ua5cbpriabm4ptw9n8sdtwx0gpoajl0
1212
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
1313
# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
1414
# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
15-
DATABASE_URL=mysql://[email protected]:3306/travis?charset=utf8mb4&serverVersion=5.7
15+
DATABASE_NAME="travis"
16+
DATABASE_URL=mysql://[email protected]:3306/${DATABASE_NAME}?charset=utf8mb4&serverVersion=5.7
1617
###< doctrine/doctrine-bundle ###
1718

1819
###> lexik/jwt-authentication-bundle ###

Diff for: bootstrap.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* Application bootstrap file to load specified environment variables.
5+
*
6+
* @see ./public/index.php
7+
* @see ./tests/bootstrap.php
8+
*
9+
* @package App
10+
* @author TLe, Tarmo Leppänen <[email protected]>
11+
*/
12+
use Symfony\Component\Dotenv\Dotenv;
13+
14+
$environmentFile = getenv('ENVIRONMENT_FILE');
15+
16+
// Application is started against 'fastest' library, so we need to override database name manually
17+
if ($readableChannel = getenv('ENV_TEST_CHANNEL_READABLE')) {
18+
// Parse current '.env.test' file
19+
$variables = (new Dotenv())->parse(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . $environmentFile));
20+
21+
// Specify new database name for current test env
22+
$databaseName = $variables['DATABASE_NAME'] . '_' . $readableChannel;
23+
24+
// Replace DATABASE_URL variable
25+
$variables['DATABASE_URL'] = \str_replace(
26+
'/' . $variables['DATABASE_NAME'] . '?',
27+
'/' . $databaseName . '?',
28+
$variables['DATABASE_URL']
29+
);
30+
31+
// Replace DATABASE_NAME variable
32+
$variables['DATABASE_NAME'] = $databaseName;
33+
34+
// And finally populate new variables to current environment
35+
(new Dotenv())->populate($variables);
36+
} else {
37+
// Load environment variables normally
38+
(new Dotenv())->load(__DIR__ . DIRECTORY_SEPARATOR . $environmentFile);
39+
}

Diff for: config/packages/doctrine.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
doctrine:
22
dbal:
33
url: '%env(DATABASE_URL)%'
4+
# This is needed for 'fastest' see https://github.com/liuggio/fastest/issues/101
5+
dbname: '%env(DATABASE_NAME)'
46
mapping_types:
57
enum: string
68
types:

Diff for: public/index.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33
/**
44
* /public/index.php
55
*
6+
* @package App
67
* @author TLe, Tarmo Leppänen <[email protected]>
78
*/
8-
99
use App\Kernel;
10-
use Symfony\Component\Dotenv\Dotenv;
10+
use Liuggio\Fastest\Environment\FastestEnvironment;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\Debug\Debug;
1313

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

16+
// Set fastest environment
17+
FastestEnvironment::setFromRequest();
18+
1619
// The check is to ensure we don't use .env in production
1720
if (!getenv('APP_ENV')) {
18-
(new Dotenv())->load(__DIR__ . '/../.env');
21+
// Specify used environment file
22+
putenv('ENVIRONMENT_FILE=.env');
23+
24+
require __DIR__ . '/../bootstrap.php';
1925
}
2026

2127
if (getenv('APP_DEBUG')) {
@@ -32,7 +38,7 @@
3238
)
3339
) {
3440
header('HTTP/1.0 403 Forbidden');
35-
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
41+
exit('You are not allowed to access this file. Check ' . basename(__FILE__) . ' for more information.');
3642
}
3743

3844
/** @noinspection ForgottenDebugOutputInspection */

Diff for: tests/bootstrap.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* 5) Update database schema
1212
* 6) Create user roles to database
1313
*
14+
* @package App\Tests
1415
* @author TLe, Tarmo Leppänen <[email protected]>
1516
*/
1617
use App\Kernel;
@@ -20,12 +21,12 @@
2021
use Symfony\Bundle\FrameworkBundle\Console\Application;
2122
use Symfony\Component\Console\Input\ArrayInput;
2223
use Symfony\Component\Console\Output\ConsoleOutput;
23-
use Symfony\Component\Dotenv\Dotenv;
2424

25-
require __DIR__ . '/../vendor/autoload.php';
25+
// Specify used environment file
26+
putenv('ENVIRONMENT_FILE=.env.test');
2627

27-
// Load test environment variables
28-
(new Dotenv())->load(__DIR__.'/../.env.test');
28+
require __DIR__ . '/../vendor/autoload.php';
29+
require __DIR__ . '/../bootstrap.php';
2930

3031
// Create and boot 'test' kernel
3132
$kernel = new Kernel(getenv('APP_ENV'), getenv('APP_DEBUG'));

0 commit comments

Comments
 (0)