Skip to content

Commit 8e204f0

Browse files
committed
feature #1770 Make the root package name and path dynamically discovered in MakerTestCase (GromNaN)
This PR was squashed before being merged into the 1.x branch. Discussion ---------- Make the root package name and path dynamically discovered in `MakerTestCase` An issue when using the `MakerTestCase` to test maker classes provided by other packages, is that it depends on the `tests` directory of the `symfony/maker-bundle` package. In this PR, we use `Composer\InstalledVersions::getRootPackage()` to retrieve the path and name of the root package under test. This way, that's the tested package that is linked into the temporary test projects. This assumes a certain structure of the maker package (`src`, `tests` and `tests/fixtures` dirs, `tests/tmp` ignored, `tools/twigcs` subproject). Commits ------- 5d8d6fb Make the root package name and path dynamically discovered in `MakerTestCase`
2 parents e6a8518 + 5d8d6fb commit 8e204f0

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"minimum-stability": "dev",
1515
"require": {
1616
"php": ">=8.1",
17+
"composer-runtime-api": "^2.1",
1718
"doctrine/inflector": "^2.0",
1819
"nikic/php-parser": "^5.0",
1920
"symfony/config": "^6.4|^7.0|^8.0",

src/Test/MakerTestEnvironment.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Test;
1313

14+
use Composer\InstalledVersions;
1415
use Symfony\Component\Filesystem\Filesystem;
1516
use Symfony\Component\Process\InputStream;
1617

@@ -28,9 +29,11 @@ final class MakerTestEnvironment
2829
public const GENERATED_FILES_REGEX = '#(?:created|updated):\s(?:.*\\\\)*(.*\.[a-z]{3,4}).*(?:\\\\n)?#ui';
2930

3031
private Filesystem $fs;
31-
private bool|string $rootPath;
32+
private string $packageName;
33+
private string $rootPath;
3234
private string $cachePath;
3335
private string $flexPath;
36+
private string $fixturesPath;
3437
private string $path;
3538
private MakerTestProcess $runnedMakerProcess;
3639
private bool $isWindows;
@@ -41,7 +44,9 @@ private function __construct(
4144
$this->isWindows = '\\' === \DIRECTORY_SEPARATOR;
4245

4346
$this->fs = new Filesystem();
44-
$this->rootPath = realpath(__DIR__.'/../../');
47+
$composerPackage = InstalledVersions::getRootPackage();
48+
$this->packageName = $composerPackage['name'];
49+
$this->rootPath = realpath($composerPackage['install_path']);
4550
$cachePath = $this->rootPath.'/tests/tmp/cache';
4651

4752
if (!$this->fs->exists($cachePath)) {
@@ -51,6 +56,7 @@ private function __construct(
5156
$this->cachePath = realpath($cachePath);
5257
$targetVersion = $this->getTargetSkeletonVersion();
5358
$this->flexPath = $this->cachePath.'/flex_project'.$targetVersion;
59+
$this->fixturesPath = $this->rootPath.'/tests/fixtures/';
5460

5561
$directoryName = $targetVersion ?: 'current';
5662
if (str_ends_with($directoryName, '.*')) {
@@ -65,6 +71,11 @@ public static function create(MakerTestDetails $testDetails): self
6571
return new self($testDetails);
6672
}
6773

74+
public function getFixturesPath(string $path = ''): string
75+
{
76+
return $this->fixturesPath.$path;
77+
}
78+
6879
public function getPath(): string
6980
{
7081
return $this->path;
@@ -134,7 +145,7 @@ public function prepareDirectory(): void
134145
{
135146
// Copy MakerBundle to a "repo" directory for tests
136147
if (!file_exists($makerRepoPath = \sprintf('%s/maker-repo', $this->cachePath))) {
137-
MakerTestProcess::create(\sprintf('git clone %s %s', $this->rootPath, $makerRepoPath), $this->cachePath)->run();
148+
MakerTestProcess::create(['git', 'clone', $this->rootPath, $makerRepoPath], $this->cachePath)->run();
138149
}
139150

140151
if (!$this->fs->exists($this->flexPath)) {
@@ -150,15 +161,7 @@ public function prepareDirectory(): void
150161
if (!$this->fs->exists($this->path)) {
151162
try {
152163
// let's do some magic here git is faster than copy
153-
MakerTestProcess::create(
154-
$this->isWindows ? 'git clone %FLEX_PATH% %APP_PATH%' : 'git clone "$FLEX_PATH" "$APP_PATH"',
155-
\dirname($this->flexPath),
156-
[
157-
'FLEX_PATH' => $this->flexPath,
158-
'APP_PATH' => $this->path,
159-
]
160-
)
161-
->run();
164+
MakerTestProcess::create(['git', 'clone', $this->flexPath, $this->path], \dirname($this->flexPath))->run();
162165

163166
// In Window's we have to require MakerBundle in each project - git clone doesn't symlink well
164167
if ($this->isWindows) {
@@ -196,7 +199,10 @@ public function prepareDirectory(): void
196199
}
197200
}
198201

199-
public function runCommand(string $command): MakerTestProcess
202+
/**
203+
* @param string|list<string> $command
204+
*/
205+
public function runCommand(string|array $command): MakerTestProcess
200206
{
201207
return MakerTestProcess::create($command, $this->path)->run();
202208
}
@@ -236,7 +242,7 @@ public function fileExists(string $file): bool
236242

237243
public function runTwigCSLint(string $file): MakerTestProcess
238244
{
239-
if (!file_exists(__DIR__.'/../../tools/twigcs/vendor/bin/twigcs')) {
245+
if (!file_exists($this->rootPath.'/tools/twigcs/vendor/bin/twigcs')) {
240246
throw new \Exception('twigcs not found: run: "composer upgrade -W --working-dir=tools/twigcs".');
241247
}
242248

@@ -249,20 +255,20 @@ private function buildFlexSkeleton(): void
249255
$targetVersion = $this->getTargetSkeletonVersion();
250256
$versionString = $targetVersion ? \sprintf(':%s', $targetVersion) : '';
251257

252-
$flexProjectDir = \sprintf('flex_project%s', $targetVersion);
258+
$flexProjectDir = \sprintf('%s/flex_project%s', $this->cachePath, $targetVersion);
253259

254260
MakerTestProcess::create(
255261
\sprintf('composer create-project symfony/skeleton%s %s --prefer-dist --no-progress --keep-vcs', $versionString, $flexProjectDir),
256262
$this->cachePath
257263
)->run();
258264

259-
$rootPath = str_replace('\\', '\\\\', realpath(__DIR__.'/../..'));
265+
$rootPath = str_replace('\\', '\\\\', $this->rootPath);
260266

261-
$this->addMakerBundleRepoToComposer(\sprintf('%s/%s/composer.json', $this->cachePath, $flexProjectDir));
267+
$this->addMakerBundleRepoToComposer($flexProjectDir);
262268

263269
// In Linux, git plays well with symlinks - we can add maker to the flex skeleton.
264270
if (!$this->isWindows) {
265-
$this->composerRequireMakerBundle(\sprintf('%s/%s', $this->cachePath, $flexProjectDir));
271+
$this->composerRequireMakerBundle($flexProjectDir);
266272
}
267273

268274
// fetch a few packages needed for testing
@@ -411,9 +417,7 @@ public function getTargetSkeletonVersion(): ?string
411417

412418
private function composerRequireMakerBundle(string $projectDirectory): void
413419
{
414-
MakerTestProcess::create('composer require --dev symfony/maker-bundle', $projectDirectory)
415-
->run()
416-
;
420+
MakerTestProcess::create(['composer', 'require', '--dev', $this->packageName], $projectDirectory)->run();
417421

418422
$makerRepoSrcPath = \sprintf('%s/maker-repo/src', $this->cachePath);
419423

@@ -425,26 +429,20 @@ private function composerRequireMakerBundle(string $projectDirectory): void
425429
}
426430

427431
/**
428-
* Adds Symfony/MakerBundle as a "path" repository to composer.json.
432+
* Adds symfony/maker-bundle as a "path" repository to composer.json.
429433
*/
430-
private function addMakerBundleRepoToComposer(string $composerJsonPath): void
434+
private function addMakerBundleRepoToComposer(string $projectDirectory): void
431435
{
432-
$composerJson = json_decode(
433-
file_get_contents($composerJsonPath), true, 512, \JSON_THROW_ON_ERROR);
434-
435-
// Require-dev is empty and composer complains about this being an array when we encode it again.
436-
unset($composerJson['require-dev']);
437-
438-
$composerJson['repositories']['symfony/maker-bundle'] = [
436+
$repo = [
439437
'type' => 'path',
440438
'url' => \sprintf('%s%smaker-repo', $this->cachePath, \DIRECTORY_SEPARATOR),
441439
'options' => [
442440
'versions' => [
443-
'symfony/maker-bundle' => '9999.99', // Arbitrary version to avoid stability conflicts
441+
$this->packageName => '9999.99', // Arbitrary version to avoid stability conflicts
444442
],
445443
],
446444
];
447445

448-
file_put_contents($composerJsonPath, json_encode($composerJson, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
446+
MakerTestProcess::create(['composer', 'repo', 'add', $this->packageName, json_encode($repo)], $projectDirectory)->run();
449447
}
450448
}

src/Test/MakerTestProcess.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ final class MakerTestProcess
2222
{
2323
private Process $process;
2424

25-
private function __construct($commandLine, $cwd, array $envVars, $timeout)
25+
/**
26+
* @param string|list<string> $commandLine
27+
*/
28+
private function __construct(string|array $commandLine, string $cwd, array $envVars, ?float $timeout)
2629
{
2730
$this->process = \is_string($commandLine)
2831
? Process::fromShellCommandline($commandLine, $cwd, null, null, $timeout)
@@ -31,7 +34,10 @@ private function __construct($commandLine, $cwd, array $envVars, $timeout)
3134
$this->process->setEnv($envVars);
3235
}
3336

34-
public static function create($commandLine, $cwd, array $envVars = [], $timeout = null): self
37+
/**
38+
* @param string|list<string> $commandLine
39+
*/
40+
public static function create(string|array $commandLine, string $cwd, array $envVars = [], ?float $timeout = null): self
3541
{
3642
return new self($commandLine, $cwd, $envVars, $timeout);
3743
}

src/Test/MakerTestRunner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function runMaker(array $inputs, string $argumentsString = '', bool $allo
5252
*/
5353
public function copy(string $source, string $destination)
5454
{
55-
$path = __DIR__.'/../../tests/fixtures/'.$source;
55+
$path = $this->environment->getFixturesPath($source);
5656

5757
if (!file_exists($path)) {
5858
throw new \Exception(\sprintf('Cannot find file "%s"', $path));
@@ -76,7 +76,7 @@ public function copy(string $source, string $destination)
7676
public function renderTemplateFile(string $source, string $destination, array $variables): void
7777
{
7878
$twig = new Environment(
79-
new FilesystemLoader(__DIR__.'/../../tests/fixtures')
79+
new FilesystemLoader($this->environment->getFixturesPath())
8080
);
8181

8282
$rendered = $twig->render($source, $variables);

0 commit comments

Comments
 (0)