-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathMakerTestRunner.php
More file actions
281 lines (229 loc) · 8.98 KB
/
MakerTestRunner.php
File metadata and controls
281 lines (229 loc) · 8.98 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Test;
use Composer\InstalledVersions;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class MakerTestRunner
{
private Filesystem $filesystem;
private ?MakerTestProcess $executedMakerProcess = null;
public function __construct(
private MakerTestEnvironment $environment,
) {
$this->filesystem = new Filesystem();
}
public function runMaker(array $inputs, string $argumentsString = '', bool $allowedToFail = false, array $envVars = []): string
{
$this->executedMakerProcess = $this->environment->runMaker($inputs, $argumentsString, $allowedToFail, $envVars);
$output = $this->executedMakerProcess->getOutput();
// Allows for debugging the actual CLI output from within a test process. E.g. Manually viewing the output of the
// `make:voter` command that was run within the MakeVoterTest from your local command line.
// You should never use this in CI unless you know what you're doing - resource intensive.
if ('true' === getenv('MAKER_TEST_DUMP_OUTPUT')) {
dump(['Maker Process Output' => $output, 'Maker Process Error Output' => $this->executedMakerProcess->getErrorOutput()]);
}
return $output;
}
/**
* @return void
*/
public function copy(string $source, string $destination)
{
$path = self::getFixturesDir().$source;
if (!file_exists($path)) {
throw new \Exception(\sprintf('Cannot find file "%s"', $path));
}
if (is_file($path)) {
$this->filesystem->copy($path, $this->getPath($destination), true);
return;
}
// handle a directory copy
$finder = new Finder();
$finder->in($path)->files();
foreach ($finder as $file) {
$this->filesystem->copy($file->getPathname(), $this->getPath($file->getRelativePathname()), true);
}
}
public function renderTemplateFile(string $source, string $destination, array $variables): void
{
$twig = new Environment(
new FilesystemLoader(self::getFixturesDir())
);
$rendered = $twig->render($source, $variables);
$this->filesystem->mkdir(\dirname($this->getPath($destination)));
file_put_contents($this->getPath($destination), $rendered);
}
public function getPath(string $filename): string
{
return $this->environment->getPath().'/'.$filename;
}
public function readYaml(string $filename): array
{
return Yaml::parse(file_get_contents($this->getPath($filename)));
}
public function getExecutedMakerProcess(): MakerTestProcess
{
if (!$this->executedMakerProcess) {
throw new \Exception('Maker process has not been executed yet.');
}
return $this->executedMakerProcess;
}
/**
* @return void
*/
public function modifyYamlFile(string $filename, \Closure $callback)
{
$path = $this->getPath($filename);
$manipulator = new YamlSourceManipulator(file_get_contents($path));
$newData = $callback($manipulator->getData());
if (!\is_array($newData)) {
throw new \Exception('The modifyYamlFile() callback must return the final array of data');
}
$manipulator->setData($newData);
file_put_contents($path, $manipulator->getContents());
}
/**
* @return void
*/
public function runConsole(string $command, array $inputs, string $arguments = '')
{
$process = $this->environment->createInteractiveCommandProcess(
$command,
$inputs,
$arguments
);
$process->run();
}
public function runProcess(string $command): void
{
MakerTestProcess::create($command, $this->environment->getPath())->run();
}
public function replaceInFile(string $filename, string $find, string $replace, bool $allowNotFound = false): void
{
$this->environment->processReplacement(
$this->environment->getPath(),
$filename,
$find,
$replace,
$allowNotFound
);
}
public function removeFromFile(string $filename, string $find, bool $allowNotFound = false): void
{
$this->environment->processReplacement(
$this->environment->getPath(),
$filename,
$find,
'',
$allowNotFound
);
}
public function configureDatabase(bool $createSchema = true): void
{
$this->replaceInFile(
'.env',
'postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8',
getenv('TEST_DATABASE_DSN')
);
// Flex includes a recipe to suffix the dbname w/ "_test" - lets keep
// things simple for these tests and not do that.
$this->modifyYamlFile('config/packages/doctrine.yaml', function (array $config) {
if (isset($config['when@test']['doctrine']['dbal']['dbname_suffix'])) {
unset($config['when@test']['doctrine']['dbal']['dbname_suffix']);
}
return $config;
});
// this looks silly, but it's the only way to drop the database *for sure*,
// as doctrine:database:drop will error if there is no database
if (!$usingSqlite = str_starts_with(getenv('TEST_DATABASE_DSN'), 'sqlite')) {
// --if-not-exists not supported on SQLite
$this->runConsole('doctrine:database:create', [], '--env=test --if-not-exists');
}
$this->runConsole('doctrine:database:drop', [], '--env=test --force');
if (!$usingSqlite) {
// d:d:create not supported on SQLite
$this->runConsole('doctrine:database:create', [], '--env=test');
}
if ($createSchema) {
$this->runConsole('doctrine:schema:create', [], '--env=test');
}
}
public function updateSchema(): void
{
$this->runConsole('doctrine:schema:update', [], '--env=test --force');
}
public function runTests(): void
{
$internalTestProcess = MakerTestProcess::create(
\sprintf('php %s', $this->getPath('bin/phpunit')),
$this->environment->getPath())
->run(true)
;
if ($internalTestProcess->isSuccessful()) {
return;
}
throw new ExpectationFailedException(\sprintf("Error while running the PHPUnit tests *in* the project: \n\n %s \n\n Command Output: %s", $internalTestProcess->getErrorOutput()."\n".$internalTestProcess->getOutput(), $this->getExecutedMakerProcess()->getErrorOutput()."\n".$this->getExecutedMakerProcess()->getOutput()));
}
public function writeFile(string $filename, string $contents): void
{
$this->filesystem->mkdir(\dirname($this->getPath($filename)));
file_put_contents($this->getPath($filename), $contents);
}
/**
* @return void
*/
public function addToAutoloader(string $namespace, string $path)
{
$composerJson = json_decode(
json: file_get_contents($this->getPath('composer.json')),
associative: true,
flags: \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES
);
$composerJson['autoload-dev']['psr-4'][$namespace] = $path;
$this->filesystem->dumpFile(
$this->getPath('composer.json'),
json_encode($composerJson, \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR)
);
$this->environment->runCommand('composer dump-autoload');
}
public function deleteFile(string $filename): void
{
$this->filesystem->remove($this->getPath($filename));
}
public function manipulateClass(string $filename, \Closure $callback): void
{
$contents = file_get_contents($this->getPath($filename));
$manipulator = new ClassSourceManipulator(
sourceCode: $contents,
overwrite: true,
);
$callback($manipulator);
file_put_contents($this->getPath($filename), $manipulator->getSourceCode());
}
public function getSymfonyVersion(): int
{
return $this->environment->getSymfonyVersionInApp();
}
public function doesClassExist(string $class): bool
{
return $this->environment->doesClassExistInApp($class);
}
private static function getFixturesDir(): string
{
return realpath(InstalledVersions::getRootPackage()['install_path']).'/tests/fixtures/';
}
}