From d7c36b2cc71f0a2738538a09d0b6b2f675196496 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Tue, 20 Jul 2021 16:37:00 +0300 Subject: [PATCH] fix incorrect composer.json patching (#106) --- src/Component/ComposerLocator.php | 16 +++++++- .../src/Command/CompilerPromptCommandTest.php | 39 +++++++++++++++++++ tests/src/Component/ComposerLocatorTest.php | 3 +- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/Component/ComposerLocator.php b/src/Component/ComposerLocator.php index c9b9715..fcd1ab9 100644 --- a/src/Component/ComposerLocator.php +++ b/src/Component/ComposerLocator.php @@ -58,8 +58,10 @@ class ComposerLocator $dir = static::getBaseDirectory(); for (;;) { - if (file_exists($dir . '/composer.json')) { - return $dir . '/composer.json'; + $fileName = implode(DIRECTORY_SEPARATOR, [$dir, 'composer.json']); + + if (file_exists($fileName) && static::getPackageComposerJson() !== $fileName) { + return $fileName; } $counter++; @@ -80,4 +82,14 @@ class ComposerLocator { return (string) realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..'])); } + + /** + * Returns full path to the composer.json of this package. + * + * @return string + */ + private static function getPackageComposerJson(): string + { + return (string) realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'composer.json'])); + } } diff --git a/tests/src/Command/CompilerPromptCommandTest.php b/tests/src/Command/CompilerPromptCommandTest.php index d2f649a..db1e684 100644 --- a/tests/src/Command/CompilerPromptCommandTest.php +++ b/tests/src/Command/CompilerPromptCommandTest.php @@ -23,6 +23,37 @@ use Symfony\Component\Console\Tester\CommandTester; */ class CompilerPromptCommandTest extends TestCase { + public static function setUpBeforeClass(): void + { + $packageComposerJson = implode(DIRECTORY_SEPARATOR, [static::getBaseDirectory(), 'composer.json']); + $fileName = implode(DIRECTORY_SEPARATOR, [static::getBaseDirectory(), '..', 'composer.json']); + + if (!file_exists($packageComposerJson)) { + self::fail(sprintf('%s does not exist which should not happen at all. Aborting.', $packageComposerJson)); + } + + if ( + file_exists(($fileName)) && + ( + filesize($fileName) !== filesize($packageComposerJson) || + md5_file($fileName) !== md5_file($packageComposerJson) + ) + ) { + self::fail(sprintf('%s exists, cannot proceed with tests.', (string) realpath($fileName))); + } + + copy($packageComposerJson, $fileName); + } + + public static function tearDownAfterClass(): void + { + $fileName = implode(DIRECTORY_SEPARATOR, [static::getBaseDirectory(), '..', 'composer.json']); + + if (file_exists(($fileName))) { + unlink($fileName); + } + } + public function testDeactivate(): void { $tester = new CommandTester(new CompilerPromptCommand()); @@ -67,4 +98,12 @@ class CompilerPromptCommandTest extends TestCase return json_decode((string) file_get_contents($composerJson), true, 512, JSON_THROW_ON_ERROR); } + + /** + * @return string + */ + private static function getBaseDirectory(): string + { + return (string) realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', '..'])); + } } diff --git a/tests/src/Component/ComposerLocatorTest.php b/tests/src/Component/ComposerLocatorTest.php index 9152de1..506c4ac 100644 --- a/tests/src/Component/ComposerLocatorTest.php +++ b/tests/src/Component/ComposerLocatorTest.php @@ -32,7 +32,6 @@ class ComposerLocatorTest extends TestCase { $file = ComposerLocator::findComposerJson(); - self::assertStringContainsString('composer.json', $file); - self::assertFileExists($file); + self::assertEmpty($file); } }