-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbootstrap.php
129 lines (113 loc) · 4.04 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* /tests/bootstrap.php
*
* Bootstrap for PHPUnit tests, basically we need to do following things:
* 1) Load test environment variables
* 2) Boot kernel and create console application with that
* 3) Drop test environment database
* 4) Create empty database to test environment
* 5) Run migrations to test database
* 6) Load fixture date to database
* 7) Write cache files about database initialization
*
* @package App\Tests
*/
declare(strict_types=1);
use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Filesystem\Filesystem;
require dirname(__DIR__) . '/vendor/autoload.php';
if (file_exists(dirname(__DIR__) . '/config/bootstrap.php')) {
require dirname(__DIR__) . '/config/bootstrap.php';
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv(dirname(__DIR__) . '/.env');
}
$databaseCacheFile = sprintf(
'%s%stest_database_cache%s.json',
sys_get_temp_dir(),
DIRECTORY_SEPARATOR,
(string)getenv('ENV_TEST_CHANNEL_READABLE')
);
// database is already created we don't want to do any lifting anymore
if (is_readable($databaseCacheFile) && (string)getenv('ENV_TEST_CHANNEL_READABLE') !== '') {
return;
}
// Create and boot 'test' kernel
$kernel = new Kernel((string)getenv('APP_ENV'), (bool)getenv('APP_DEBUG'));
$kernel->boot();
// Create new application
$application = new Application($kernel);
$application->setAutoExit(false);
// Add the doctrine:database:drop command to the application and run it
$dropDatabaseDoctrineCommand = static function () use ($application): void {
$input = new ArrayInput([
'command' => 'doctrine:database:drop',
'--force' => true,
'--if-exists' => true,
]);
$input->setInteractive(false);
$application->run($input, new ConsoleOutput());
};
// Add the doctrine:database:create command to the application and run it
$createDatabaseDoctrineCommand = static function () use ($application): void {
$input = new ArrayInput([
'command' => 'doctrine:database:create',
]);
$input->setInteractive(false);
$application->run($input, new ConsoleOutput());
};
// Add the doctrine:schema:update command to the application and run it
$updateSchemaDoctrineCommand = static function () use ($application): void {
$input = new ArrayInput([
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true,
]);
$input->setInteractive(false);
$application->run($input, new ConsoleOutput());
};
// Add the doctrine:fixtures:load command to the application and run it
$loadFixturesDoctrineCommand = static function () use ($application): void {
$input = new ArrayInput([
'command' => 'doctrine:fixtures:load',
'--no-interaction' => true,
]);
$input->setInteractive(false);
$application->run($input, new ConsoleOutput());
};
// Ensure that we have "clean" JWT auth cache file
$createJwtAuthCache = static function (): void {
// Specify used cache file
$filename = sprintf(
'%s%stest_jwt_auth_cache%s.json',
sys_get_temp_dir(),
DIRECTORY_SEPARATOR,
(string)getenv('ENV_TEST_CHANNEL_READABLE')
);
// Remove existing cache if exists
$fs = new Filesystem();
$fs->remove($filename);
unset($fs);
// Create empty cache file
file_put_contents($filename, '{}');
};
// Create database cache file
$createDatabaseCreateCache = static function () use ($databaseCacheFile): void {
// Create database cache file
file_put_contents($databaseCacheFile, '{"init": ' . (new DateTime())->format(DATE_RFC3339) . '}');
};
// And finally call each of initialize functions to make test environment ready
array_map(
'\call_user_func',
[
$dropDatabaseDoctrineCommand,
$createDatabaseDoctrineCommand,
$updateSchemaDoctrineCommand,
$loadFixturesDoctrineCommand,
$createJwtAuthCache,
$createDatabaseCreateCache,
]
);