1111
1212namespace Symfony \Bundle \MakerBundle \Test ;
1313
14+ use Composer \InstalledVersions ;
1415use Symfony \Component \Filesystem \Filesystem ;
1516use 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}
0 commit comments