diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 0a741cf..11b979c 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -461,7 +461,7 @@ jobs: with: coverage: xdebug extensions: intl, zip - ini-values: memory_limit=-1, error_reporting=E_ALL, display_errors=On + ini-values: memory_limit=-1, error_reporting=E_ALL, display_errors=On, xdebug.mode="develop,coverage" php-version: ${{ matrix.php-version }} tools: composer diff --git a/src/Config.php b/src/Config.php index ec17a23..5d03b5b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -13,6 +13,7 @@ namespace GsTYPO3\CorePatches; +use Composer\Config\ConfigSourceInterface; use Composer\Config\JsonConfigSource; use Composer\Factory; use Composer\Json\JsonFile; @@ -71,7 +72,7 @@ final class Config implements PersistenceInterface private JsonFile $jsonFile; - private JsonConfigSource $jsonConfigSource; + private ConfigSourceInterface $configSource; private Changes $changes; @@ -83,10 +84,12 @@ final class Config implements PersistenceInterface private Patches $patches; - public function __construct(?JsonFile $jsonFile = null, ?JsonConfigSource $jsonConfigSource = null) - { + public function __construct( + ?JsonFile $jsonFile = null, + ?ConfigSourceInterface $configSource = null + ) { $this->jsonFile = $jsonFile ?? new JsonFile(Factory::getComposerFile()); - $this->jsonConfigSource = $jsonConfigSource ?? new JsonConfigSource($this->jsonFile); + $this->configSource = $configSource ?? new JsonConfigSource($this->jsonFile); $this->changes = new Changes(); $this->preferredInstallChanged = new PreferredInstallChanged(); @@ -165,9 +168,9 @@ public function save(): self ); if ($installMethod === '') { - $this->jsonConfigSource->removeConfigSetting($name); + $this->configSource->removeConfigSetting($name); } else { - $this->jsonConfigSource->addConfigSetting($name, $installMethod); + $this->configSource->addConfigSetting($name, $installMethod); } } @@ -179,9 +182,9 @@ public function save(): self ); if ($this->patches->isEmpty()) { - $this->jsonConfigSource->removeProperty($name); + $this->configSource->removeProperty($name); } else { - $this->jsonConfigSource->addProperty($name, $this->patches); + $this->configSource->addProperty($name, $this->patches); } // Save plugin configuration @@ -192,9 +195,9 @@ public function save(): self ); if ($this->isEmpty()) { - $this->jsonConfigSource->removeProperty($name); + $this->configSource->removeProperty($name); } else { - $this->jsonConfigSource->addProperty($name, $this); + $this->configSource->addProperty($name, $this); } // Rewrite configuration to enforce correct formatting diff --git a/src/Utility/ComposerUtils.php b/src/Utility/ComposerUtils.php index b95e475..85bff78 100644 --- a/src/Utility/ComposerUtils.php +++ b/src/Utility/ComposerUtils.php @@ -48,17 +48,29 @@ final class ComposerUtils private PatchUtils $patchUtils; - public function __construct(Composer $composer, IOInterface $io) - { + public function __construct( + Composer $composer, + IOInterface $io, + ?Config $config = null, + ?Application $application = null, + ?RestApi $restApi = null, + ?PatchUtils $patchUtils = null + ) { $this->composer = $composer; $this->io = $io; - $this->config = new Config(new JsonFile(Factory::getComposerFile(), null, $this->io)); - $this->application = new Application(); + $httpDownloader = Factory::createHttpDownloader($this->io, $this->composer->getConfig()); + + $this->config = $config ?? new Config( + new JsonFile(Factory::getComposerFile(), $httpDownloader, $this->io), + $this->composer->getConfig()->getConfigSource() + ); + + $this->application = $application ?? new Application(); $this->application->setAutoExit(false); - $this->gerritRestApi = new RestApi(Factory::createHttpDownloader($this->io, $this->composer->getConfig())); - $this->patchUtils = new PatchUtils($this->composer, $this->io); + $this->gerritRestApi = $restApi ?? new RestApi($httpDownloader); + $this->patchUtils = $patchUtils ?? new PatchUtils($this->composer, $this->io); } /** diff --git a/src/Utility/PatchUtils.php b/src/Utility/PatchUtils.php index 90a4682..fe7dce9 100644 --- a/src/Utility/PatchUtils.php +++ b/src/Utility/PatchUtils.php @@ -33,6 +33,16 @@ public function __construct(Composer $composer, IOInterface $io) $this->io = $io; } + public function getPatchFileName(string $destination, string $packageName, int $numericId): string + { + return sprintf( + '%s/%s-review-%s.patch', + $destination, + str_replace('/', '-', $packageName), + $numericId + ); + } + public function patchIsPartOfChange(string $patchFileName, int $numericId): bool { return strpos($patchFileName, '-review-' . $numericId . '.patch') !== false; @@ -221,12 +231,7 @@ public function save( foreach ($patches as $packageName => $chunks) { $content = implode('', $chunks); - $patchFileName = sprintf( - '%s/%s-review-%s.patch', - $destination, - str_replace('/', '-', $packageName), - $numericId - ); + $patchFileName = $this->getPatchFileName($destination, $packageName, $numericId); $this->io->write(sprintf(' - Creating patch %s', $patchFileName)); file_put_contents($patchFileName, $content); $composerChanges[$packageName] = [$subject => $patchFileName]; diff --git a/tests/Unit/Utility/PatchUtilsTest.php b/tests/Unit/Utility/PatchUtilsTest.php new file mode 100644 index 0000000..0258708 --- /dev/null +++ b/tests/Unit/Utility/PatchUtilsTest.php @@ -0,0 +1,89 @@ +previousWorkingDir = $previousWorkingDir; + $this->testWorkingDir = self::getTestPath(); + chdir($this->testWorkingDir); + + self::createFiles($this->testWorkingDir, [ + 'composer.json' => 'FIX:composer.json', + ]); + + $this->bufferIO = new BufferIO(); + $this->composer = Factory::create($this->bufferIO); + + $this->patchUtils = new PatchUtils($this->composer, $this->bufferIO); + } + + protected function tearDown(): void + { + chdir($this->previousWorkingDir); + + parent::tearDown(); + } + + /** + * @medium + */ + public function testGetPatchFileName(): void + { + self::assertSame( + 'destination/vendor-package-review-12345.patch', + $this->patchUtils->getPatchFileName('destination', 'vendor/package', 12345) + ); + } + + /** + * @medium + */ + public function testPatchIsPartOfChange(): void + { + self::assertTrue($this->patchUtils->patchIsPartOfChange( + $this->patchUtils->getPatchFileName('destination', 'vendor/package', 12345), + 12345 + )); + self::assertFalse($this->patchUtils->patchIsPartOfChange('vendor-package.patch', 12345)); + } +}