Skip to content

Commit

Permalink
Merge pull request #51 from Jean85/uniform-package-version
Browse files Browse the repository at this point in the history
Uniform version style for root package
  • Loading branch information
Ocramius authored Nov 24, 2017
2 parents 611925b + 9abc259 commit ad8a245
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ private static function getVersions(Locker $locker, RootPackageInterface $rootPa
);
}

yield $rootPackage->getName() => $rootPackage->getVersion() . '@' . $rootPackage->getSourceReference();
yield $rootPackage->getName() => $rootPackage->getPrettyVersion() . '@' . $rootPackage->getSourceReference();
}
}
120 changes: 102 additions & 18 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Composer\Installer\InstallationManager;
use Composer\IO\IOInterface;
use Composer\Package\Locker;
use Composer\Package\Package;
use Composer\Package\RootAliasPackage;
use Composer\Package\RootPackage;
use Composer\Package\RootPackageInterface;
Expand Down Expand Up @@ -45,7 +46,7 @@ final class InstallerTest extends TestCase
/**
* {@inheritDoc}
*
* @throws \PHPUnit_Framework_Exception
* @throws \PHPUnit\Framework\Exception
*/
protected function setUp()
{
Expand Down Expand Up @@ -83,7 +84,6 @@ public function testDumpVersionsClass()
$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);

Expand Down Expand Up @@ -132,12 +132,9 @@ public function testDumpVersionsClass()
$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('getPackage')->willReturn($this->getRootPackageMock());
$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);

Expand Down Expand Up @@ -201,7 +198,6 @@ public function testDumpVersionsClassNoDev()
$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);

Expand Down Expand Up @@ -241,13 +237,9 @@ public function testDumpVersionsClassNoDev()
$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('getPackage')->willReturn($this->getRootPackageMock());
$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(
Expand Down Expand Up @@ -314,7 +306,6 @@ public function testDumpVersionsWithoutPackageSourceDetails()
$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);

Expand Down Expand Up @@ -351,13 +342,9 @@ public function testDumpVersionsWithoutPackageSourceDetails()
$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('getPackage')->willReturn($this->getRootPackageMock());
$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(
Expand Down Expand Up @@ -724,4 +711,101 @@ public function testWillEscapeRegexParsingOfClassDefinitions()
)
);
}

public function testGetVersionsIsNotNormalizedForRootPackage()
{
$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'
<?php
namespace PackageVersions;
/**
* This class is generated by ocramius/package-versions, specifically by
* @see \PackageVersions\Installer
*
* This file is overwritten at every run of `composer install` or `composer update`.
*/
final class Versions
{
const VERSIONS = array (
'ocramius/package-versions' => '1.0.0@',
'root/package' => '1.3.5@aaabbbcccddd',
);
private function __construct()
{
}
/**
* @throws \OutOfBoundsException if a version cannot be located
*/
public static function getVersion(string $packageName) : string
{
if (! isset(self::VERSIONS[$packageName])) {
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
}
return self::VERSIONS[$packageName];
}
}

PHP;

self::assertSame($expectedSource, file_get_contents($expectedPath . '/Versions.php'));

$this->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');

return $package;
}
}

0 comments on commit ad8a245

Please sign in to comment.