From b6d9c31ed5e36be40b88a0b4e72bbbf56edfe9bc Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 21 Aug 2020 15:25:03 +0200 Subject: [PATCH] BC break: removed deprecated `PackageVersions\Versions::ROOT_PACKAGE_NAME` constant This simplifies the tool to no longer be a plugin: `PackageVersions\Versions` is now a much simpler class that is no longer generated/written to disk at installation, and `ocramius/package-versions` is no longer a `"type": "composer-plugin"`, but rather a more usual `"type": "library"` package. This: * Fixes #138 - this library no longer changes `vendor` post-installation * Fixes #142 - the `rootPackage` version is now detected via composer, and no longer leads to changes in `vendor/ocramius/package-versions/src/PackageVersions/Versions.php` at each change of source root in a project * Fixes #152 - when `"lock": false` is used in composer, since we no longer access lock file information from sources of this package * Fixes #107 - writing to `vendor` is no longer happening from this library, so no file access rights should be needed. --- src/PackageVersions/ComposerV2Plugin.php | 20 - src/PackageVersions/Installer.php | 234 ----- src/PackageVersions/Versions.php | 21 - test/PackageVersionsTest/InstallerTest.php | 989 --------------------- test/PackageVersionsTest/VersionsTest.php | 2 +- 5 files changed, 1 insertion(+), 1265 deletions(-) delete mode 100644 src/PackageVersions/ComposerV2Plugin.php delete mode 100644 src/PackageVersions/Installer.php delete mode 100644 test/PackageVersionsTest/InstallerTest.php diff --git a/src/PackageVersions/ComposerV2Plugin.php b/src/PackageVersions/ComposerV2Plugin.php deleted file mode 100644 index 8a8c887..0000000 --- a/src/PackageVersions/ComposerV2Plugin.php +++ /dev/null @@ -1,20 +0,0 @@ - 'dumpVersionsClass']; - } - - /** - * @throws RuntimeException - */ - public static function dumpVersionsClass(Event $composerEvent): void - { - $composer = $composerEvent->getComposer(); - $rootPackage = $composer->getPackage(); - $versions = iterator_to_array(self::getVersions($composer->getLocker(), $rootPackage)); - - if (! array_key_exists('ocramius/package-versions', $versions)) { - //plugin must be globally installed - we only want to generate versions for projects which specifically - //require ocramius/package-versions - return; - } - - $versionClass = self::generateVersionsClass($rootPackage->getName()); - - self::writeVersionClassToFile($versionClass, $composer, $composerEvent->getIO()); - } - - /** - * @param string[] $versions - */ - private static function generateVersionsClass(string $rootPackageName): string - { - return sprintf( - self::$generatedClassTemplate, - 'fin' . 'al ' . 'cla' . 'ss ' . 'Versions', // note: workaround for regex-based code parsers :-( - $rootPackageName - ); - } - - /** - * @throws RuntimeException - */ - private static function writeVersionClassToFile(string $versionClassSource, Composer $composer, IOInterface $io): void - { - $installPath = self::locateRootPackageInstallPath($composer->getConfig(), $composer->getPackage()) - . '/src/PackageVersions/Versions.php'; - - $installDir = dirname($installPath); - if (! file_exists($installDir)) { - $io->write('ocramius/package-versions: Package not found (probably scheduled for removal); generation of version class skipped.'); - - return; - } - - if (! is_writable($installDir)) { - $io->write( - sprintf( - 'ocramius/package-versions: %s is not writable; generation of version class skipped.', - $installDir - ) - ); - - return; - } - - $io->write('ocramius/package-versions: Generating version class...'); - - $installPathTmp = $installPath . '_' . uniqid('tmp', true); - file_put_contents($installPathTmp, $versionClassSource); - chmod($installPathTmp, 0664); - rename($installPathTmp, $installPath); - - $io->write('ocramius/package-versions: ...done generating version class'); - } - - /** - * @throws RuntimeException - */ - private static function locateRootPackageInstallPath( - Config $composerConfig, - RootPackageInterface $rootPackage - ): string { - if (self::getRootPackageAlias($rootPackage)->getName() === 'ocramius/package-versions') { - return dirname($composerConfig->get('vendor-dir')); - } - - return $composerConfig->get('vendor-dir') . '/ocramius/package-versions'; - } - - private static function getRootPackageAlias(RootPackageInterface $rootPackage): PackageInterface - { - $package = $rootPackage; - - while ($package instanceof AliasPackage) { - $package = $package->getAliasOf(); - } - - return $package; - } - - /** - * @return Generator&string[] - * - * @psalm-return Generator - */ - private static function getVersions(Locker $locker, RootPackageInterface $rootPackage): Generator - { - $lockData = $locker->getLockData(); - - $lockData['packages-dev'] ??= []; - - foreach (array_merge($lockData['packages'], $lockData['packages-dev']) as $package) { - yield $package['name'] => $package['version'] . '@' . ( - $package['source']['reference'] ?? $package['dist']['reference'] ?? '' - ); - } - - foreach ($rootPackage->getReplaces() as $replace) { - $version = $replace->getPrettyConstraint(); - if ($version === 'self.version') { - $version = $rootPackage->getPrettyVersion(); - } - - yield $replace->getTarget() => $version . '@' . $rootPackage->getSourceReference(); - } - - yield $rootPackage->getName() => $rootPackage->getPrettyVersion() . '@' . $rootPackage->getSourceReference(); - } -} diff --git a/src/PackageVersions/Versions.php b/src/PackageVersions/Versions.php index 83033ac..f622268 100644 --- a/src/PackageVersions/Versions.php +++ b/src/PackageVersions/Versions.php @@ -7,22 +7,8 @@ use Composer\InstalledVersions; use OutOfBoundsException; -/** - * This is a stub class: it is in place only for scenarios where PackageVersions - * is installed with a `--no-scripts` flag, in which scenarios the Versions class - * is not being replaced. - * - * If you are reading this docBlock inside your `vendor/` dir, then this means - * that PackageVersions didn't correctly install, and is in "fallback" mode. - */ final class Versions { - /** - * @deprecated please use {@see self::rootPackageName()} instead. - * This constant will be removed in version 2.0.0. - */ - public const ROOT_PACKAGE_NAME = 'unknown/root-package@UNKNOWN'; - private function __construct() { } @@ -48,13 +34,6 @@ public static function rootPackageName() : string */ public static function getVersion(string $packageName) : string { - /** @psalm-suppress DeprecatedConstant */ - if ($packageName === self::ROOT_PACKAGE_NAME) { - $rootPackage = InstalledVersions::getRootPackage(); - - return $rootPackage['pretty_version'] . '@' . $rootPackage['reference']; - } - return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } diff --git a/test/PackageVersionsTest/InstallerTest.php b/test/PackageVersionsTest/InstallerTest.php deleted file mode 100644 index 6ed1d86..0000000 --- a/test/PackageVersionsTest/InstallerTest.php +++ /dev/null @@ -1,989 +0,0 @@ -installer = new Installer(); - $this->io = $this->createMock(IOInterface::class); - $this->composer = $this->createMock(Composer::class); - $this->eventDispatcher = $this->createMock(EventDispatcher::class); - - $this->composer->expects(self::any())->method('getEventDispatcher')->willReturn($this->eventDispatcher); - } - - public function testGetSubscribedEvents(): void - { - $events = Installer::getSubscribedEvents(); - - self::assertSame( - ['post-autoload-dump' => 'dumpVersionsClass'], - $events - ); - - foreach ($events as $callback) { - self::assertIsCallable([$this->installer, $callback]); - } - } - - public function testDumpVersionsClassIfExistingFileIsNotWritable(): void - { - $config = $this->createMock(Config::class); - $locker = $this->createMock(Locker::class); - $repositoryManager = $this->createMock(RepositoryManager::class); - $installManager = $this->createMock(InstallationManager::class); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $expectedFileName = $expectedPath . '/Versions.php'; - file_put_contents($expectedFileName, 'NOT PHP!'); - chmod($expectedFileName, 0444); - - $locker - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - ], - ]); - - $repositoryManager->method('getLocalRepository')->willReturn($repository); - - $this->composer->method('getConfig')->willReturn($config); - $this->composer->method('getLocker')->willReturn($locker); - $this->composer->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->method('getPackage')->willReturn($this->getRootPackageMock()); - $this->composer->method('getInstallationManager')->willReturn($installManager); - - $config->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - self::assertStringStartsWith('rmDir($vendorDir); - } - - public function testDumpVersionsClassIfReadonlyFilesystem(): void - { - $config = $this->createMock(Config::class); - $locker = $this->createMock(Locker::class); - $repositoryManager = $this->createMock(RepositoryManager::class); - $installManager = $this->createMock(InstallationManager::class); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0700, true); - - $expectedFileName = $expectedPath . '/Versions.php'; - file_put_contents($expectedFileName, 'NOT PHP!'); - chmod($expectedFileName, 0400); - chmod($expectedPath, 0400); - - $locker - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - ], - ]); - - $repositoryManager->method('getLocalRepository')->willReturn($repository); - - $this->composer->method('getConfig')->willReturn($config); - $this->composer->method('getLocker')->willReturn($locker); - $this->composer->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->method('getPackage')->willReturn($this->getRootPackageMock()); - $this->composer->method('getInstallationManager')->willReturn($installManager); - - $config->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - chmod($expectedPath, 0700); - chmod($expectedFileName, 0600); - - self::assertSame('NOT PHP!', file_get_contents($expectedFileName)); - - $this->rmDir($vendorDir); - } - - public function testDumpVersionsClass(): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - [ - 'name' => 'foo/bar', - 'version' => '1.2.3', - 'source' => ['reference' => 'abc123'], - ], - [ - 'name' => 'baz/tab', - 'version' => '4.5.6', - 'source' => ['reference' => 'def456'], - ], - ], - 'packages-dev' => [ - [ - 'name' => 'tar/taz', - 'version' => '7.8.9', - 'source' => ['reference' => 'ghi789'], - ], - ], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($this->getRootPackageMock()); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - $expectedSource = <<<'PHP' -rmDir($vendorDir); - } - - public function testDumpVersionsClassNoDev(): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - [ - 'name' => 'foo/bar', - 'version' => '1.2.3', - 'source' => ['reference' => 'abc123'], - ], - [ - 'name' => 'baz/tab', - 'version' => '4.5.6', - 'source' => ['reference' => 'def456'], - ], - ], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($this->getRootPackageMock()); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - $expectedSource = <<<'PHP' -rmDir($vendorDir); - } - - /** - * @throws RuntimeException - * - * @group #12 - */ - public function testDumpVersionsWithoutPackageSourceDetails(): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - [ - 'name' => 'foo/bar', - 'version' => '1.2.3', - 'dist' => ['reference' => 'abc123'], // version defined in the dist, this time - ], - [ - 'name' => 'baz/tab', - 'version' => '4.5.6', // source missing - ], - ], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($this->getRootPackageMock()); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - $expectedSource = <<<'PHP' -rmDir($vendorDir); - } - - /** - * @throws RuntimeException - * - * @dataProvider rootPackageProvider - */ - public function testDumpsVersionsClassToSpecificLocation(RootPackageInterface $rootPackage, bool $inVendor): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/vendor'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($vendorDir, 0777, true); - - /** @noinspection RealpathInSteamContextInspection */ - $expectedPath = $inVendor - ? $vendorDir . '/ocramius/package-versions/src/PackageVersions' - : realpath($vendorDir . '/..') . '/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - ], - 'packages-dev' => [], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($rootPackage); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - $generatedSource = file_get_contents($expectedPath . '/Versions.php'); - - self::assertStringNotContainsString( - 'unknown/root-package@UNKNOWN', - $generatedSource - ); - - self::assertStringMatchesFormat( - '%Aclass Versions%AROOT_PACKAGE_NAME%A', - $generatedSource - ); - - $this->rmDir($vendorDir); - } - - /** - * @return bool[][]|RootPackageInterface[][] the root package and whether the versions class is to be generated in - * the vendor dir or not - */ - public function rootPackageProvider(): array - { - $baseRootPackage = new RootPackage('root/package', '1.2.3', '1.2.3'); - $aliasRootPackage = new RootAliasPackage($baseRootPackage, '1.2.3', '1.2.3'); - $indirectAliasRootPackage = new RootAliasPackage($aliasRootPackage, '1.2.3', '1.2.3'); - $packageVersionsRootPackage = new RootPackage('ocramius/package-versions', '1.2.3', '1.2.3'); - $aliasPackageVersionsRootPackage = new RootAliasPackage($packageVersionsRootPackage, '1.2.3', '1.2.3'); - $indirectAliasPackageVersionsRootPackage = new RootAliasPackage( - $aliasPackageVersionsRootPackage, - '1.2.3', - '1.2.3' - ); - - return [ - 'root package is not ocramius/package-versions' => [ - $baseRootPackage, - true, - ], - 'alias root package is not ocramius/package-versions' => [ - $aliasRootPackage, - true, - ], - 'indirect alias root package is not ocramius/package-versions' => [ - $indirectAliasRootPackage, - true, - ], - 'root package is ocramius/package-versions' => [ - $packageVersionsRootPackage, - false, - ], - 'alias root package is ocramius/package-versions' => [ - $aliasPackageVersionsRootPackage, - false, - ], - 'indirect alias root package is ocramius/package-versions' => [ - $indirectAliasPackageVersionsRootPackage, - false, - ], - ]; - } - - public function testVersionsAreNotDumpedIfPackageVersionsNotExplicitlyRequired(): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - $package = $this->createMock(RootPackageInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'foo/bar', - 'version' => '1.2.3', - 'dist' => ['reference' => 'abc123'], // version defined in the dist, this time - ], - [ - 'name' => 'baz/tab', - 'version' => '4.5.6', // source missing - ], - ], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($package); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $package->expects(self::any())->method('getName')->willReturn('root/package'); - $package->expects(self::any())->method('getVersion')->willReturn('1.3.5'); - $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd'); - $package->expects(self::any())->method('getReplaces')->willReturn([]); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - self::assertFileDoesNotExist($expectedPath . '/Versions.php'); - - $this->rmDir($vendorDir); - } - - /** - * @group #41 - * @group #46 - */ - public function testVersionsAreNotDumpedIfPackageIsScheduledForRemoval(): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $package = $this->createMock(RootPackageInterface::class); - $package->expects(self::any())->method('getReplaces')->willReturn([]); - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - ], - ]); - - $package->expects(self::any())->method('getName')->willReturn('root/package'); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getPackage')->willReturn($package); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - self::assertFileDoesNotExist($expectedPath . '/Versions.php'); - self::assertFileDoesNotExist($expectedPath . '/Versions.php'); - } - - public function testGeneratedVersionFileAccessRights(): void - { - if (strpos(PHP_OS, 'WIN') === 0) { - $this->markTestSkipped('Windows is kinda "meh" at file access levels'); - } - - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - $package = $this->createMock(RootPackageInterface::class); - $package->expects(self::any())->method('getReplaces')->willReturn([]); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - ], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($package); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $package->expects(self::any())->method('getName')->willReturn('root/package'); - $package->expects(self::any())->method('getVersion')->willReturn('1.3.5'); - $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd'); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - $filePath = $expectedPath . '/Versions.php'; - - self::assertFileExists($filePath); - self::assertSame('0664', substr(sprintf('%o', fileperms($filePath)), -4)); - - $this->rmDir($vendorDir); - } - - private function rmDir(string $directory): void - { - if (! is_dir($directory)) { - unlink($directory); - - return; - } - - array_map( - function ($item) use ($directory): void { - $this->rmDir($directory . '/' . $item); - }, - array_filter( - scandir($directory), - static function (string $dirItem) { - return ! in_array($dirItem, ['.', '..'], true); - } - ) - ); - - rmdir($directory); - } - - /** - * @group composer/composer#5237 - */ - public function testWillEscapeRegexParsingOfClassDefinitions(): void - { - self::assertSame( - 1, - preg_match_all( - '{^((?:final\s+)?(?:\s*))class\s+(\S+)}mi', - file_get_contents((new ReflectionClass(Installer::class))->getFileName()) - ) - ); - } - - public function testGetVersionsIsNotNormalizedForRootPackage(): void - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); - $locker = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock(); - $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock(); - $installManager = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock(); - $repository = $this->createMock(InstalledRepositoryInterface::class); - - $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); - - $expectedPath = $vendorDir . '/ocramius/package-versions/src/PackageVersions'; - - /** @noinspection MkdirRaceConditionInspection */ - mkdir($expectedPath, 0777, true); - - $locker - ->expects(self::any()) - ->method('getLockData') - ->willReturn([ - 'packages' => [ - [ - 'name' => 'ocramius/package-versions', - 'version' => '1.0.0', - ], - ], - ]); - - $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository); - - $this->composer->expects(self::any())->method('getConfig')->willReturn($config); - $this->composer->expects(self::any())->method('getLocker')->willReturn($locker); - $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager); - $this->composer->expects(self::any())->method('getPackage')->willReturn($this->getRootPackageMock()); - $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager); - - $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir); - - Installer::dumpVersionsClass(new Event( - 'post-install-cmd', - $this->composer, - $this->io - )); - - $expectedSource = <<<'PHP' -rmDir($vendorDir); - } - - private function getRootPackageMock(): RootPackageInterface - { - $package = $this->createMock(RootPackageInterface::class); - $package->expects(self::any())->method('getName')->willReturn('root/package'); - $package->expects(self::any())->method('getPrettyVersion')->willReturn('1.3.5'); - $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd'); - - $link = $this->createMock(Link::class); - $link->expects(self::any())->method('getTarget')->willReturn('some-replaced/package'); - $link->expects(self::any())->method('getPrettyConstraint')->willReturn('self.version'); - - $package->expects(self::any())->method('getReplaces')->willReturn([$link]); - - return $package; - } -} diff --git a/test/PackageVersionsTest/VersionsTest.php b/test/PackageVersionsTest/VersionsTest.php index 63d5ce7..59160eb 100644 --- a/test/PackageVersionsTest/VersionsTest.php +++ b/test/PackageVersionsTest/VersionsTest.php @@ -36,7 +36,7 @@ public function testValidVersions(): void public function testCanRetrieveRootPackageVersion(): void { /** @psalm-suppress DeprecatedConstant */ - self::assertMatchesRegularExpression('/^.+\@[0-9a-f]+$/', Versions::getVersion(Versions::ROOT_PACKAGE_NAME)); + self::assertMatchesRegularExpression('/^.+\@[0-9a-f]+$/', Versions::getVersion(Versions::rootPackageName())); } /** @group #153 */