Merge remote-tracking branch 'szeber/master'

main
Jordi Boggiano 9 years ago
commit 74e56fd5c6

@ -308,7 +308,7 @@ class Factory
$lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
? substr($composerFile, 0, -4).'lock'
: $composerFile . '.lock';
$locker = new Package\Locker($io, new JsonFile($lockFile, new RemoteFilesystem($io, $config)), $rm, $im, md5_file($composerFile));
$locker = new Package\Locker($io, new JsonFile($lockFile, new RemoteFilesystem($io, $config)), $rm, $im, file_get_contents($composerFile));
$composer->setLocker($locker);
}

@ -34,6 +34,7 @@ class Locker
private $repositoryManager;
private $installationManager;
private $hash;
private $contentHash;
private $loader;
private $dumper;
private $process;
@ -43,17 +44,18 @@ class Locker
* Initializes packages locker.
*
* @param IOInterface $io
* @param JsonFile $lockFile lockfile loader
* @param RepositoryManager $repositoryManager repository manager instance
* @param InstallationManager $installationManager installation manager instance
* @param string $hash unique hash of the current composer configuration
* @param JsonFile $lockFile lockfile loader
* @param RepositoryManager $repositoryManager repository manager instance
* @param InstallationManager $installationManager installation manager instance
* @param string $composerFileContents The contents of the composer file
*/
public function __construct(IOInterface $io, JsonFile $lockFile, RepositoryManager $repositoryManager, InstallationManager $installationManager, $hash)
public function __construct(IOInterface $io, JsonFile $lockFile, RepositoryManager $repositoryManager, InstallationManager $installationManager, $composerFileContents)
{
$this->lockFile = $lockFile;
$this->repositoryManager = $repositoryManager;
$this->installationManager = $installationManager;
$this->hash = $hash;
$this->hash = md5($composerFileContents);
$this->contentHash = $this->getContentHash($composerFileContents);
$this->loader = new ArrayLoader(null, true);
$this->dumper = new ArrayDumper();
$this->process = new ProcessExecutor($io);
@ -84,6 +86,11 @@ class Locker
{
$lock = $this->lockFile->read();
if (!empty($lock['content-hash'])) {
// There is a content hash key, use that instead of the file hash
return $this->contentHash === $lock['content-hash'];
}
return $this->hash === $lock['hash'];
}
@ -239,6 +246,7 @@ class Locker
'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file',
'This file is @gener'.'ated automatically'),
'hash' => $this->hash,
'content-hash' => $this->contentHash,
'packages' => null,
'packages-dev' => null,
'aliases' => array(),
@ -376,4 +384,39 @@ class Locker
return $datetime ? $datetime->format('Y-m-d H:i:s') : null;
}
/**
* Returns the md5 hash of the sorted content of the composer file.
*
* @param string $composerFileContents The contents of the composer file.
*
* @return string
*/
private function getContentHash($composerFileContents)
{
$content = json_decode($composerFileContents, true);
$relevantKeys = array(
'require',
'require-dev',
'conflict',
'replace',
'provide',
'minimum-stability',
'prefer-stable',
'repositories',
'extra',
'version',
'name',
);
$relevantContent = array();
foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
$relevantContent[$key] = $content[$key];
}
ksort($relevantContent);
return md5(json_encode($relevantContent));
}
}

@ -190,7 +190,8 @@ class InstallerTest extends TestCase
}));
}
$locker = new Locker($io, $lockJsonMock, $repositoryManager, $composer->getInstallationManager(), md5(json_encode($composerConfig)));
$contents = json_encode($composerConfig);
$locker = new Locker($io, $lockJsonMock, $repositoryManager, $composer->getInstallationManager(), $contents);
$composer->setLocker($locker);
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
@ -236,6 +237,7 @@ class InstallerTest extends TestCase
if ($expectLock) {
unset($actualLock['hash']);
unset($actualLock['content-hash']);
unset($actualLock['_readme']);
$this->assertEquals($expectLock, $actualLock);
}

@ -20,7 +20,8 @@ class LockerTest extends \PHPUnit_Framework_TestCase
public function testIsLocked()
{
$json = $this->createJsonFileMock();
$locker = new Locker(new NullIO, $json, $this->createRepositoryManagerMock(), $this->createInstallationManagerMock(), 'md5');
$locker = new Locker(new NullIO, $json, $this->createRepositoryManagerMock(), $this->createInstallationManagerMock(),
$this->getJsonContent());
$json
->expects($this->any())
@ -40,7 +41,7 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
$locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
$json
->expects($this->once())
@ -58,7 +59,7 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
$locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
$json
->expects($this->once())
@ -85,7 +86,8 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
$jsonContent = $this->getJsonContent() . ' ';
$locker = new Locker(new NullIO, $json, $repo, $inst, $jsonContent);
$package1 = $this->createPackageMock();
$package2 = $this->createPackageMock();
@ -116,6 +118,9 @@ class LockerTest extends \PHPUnit_Framework_TestCase
->method('getVersion')
->will($this->returnValue('0.1.10.0'));
$hash = md5($jsonContent);
$contentHash = md5(trim($jsonContent));
$json
->expects($this->once())
->method('write')
@ -123,7 +128,8 @@ class LockerTest extends \PHPUnit_Framework_TestCase
'_readme' => array('This file locks the dependencies of your project to a known state',
'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file',
'This file is @gener'.'ated automatically'),
'hash' => 'md5',
'hash' => $hash,
'content-hash' => $contentHash,
'packages' => array(
array('name' => 'pkg1', 'version' => '1.0.0-beta'),
array('name' => 'pkg2', 'version' => '0.1.10')
@ -148,7 +154,7 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
$locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
$package1 = $this->createPackageMock();
$package1
@ -167,12 +173,13 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
$jsonContent = $this->getJsonContent();
$locker = new Locker(new NullIO, $json, $repo, $inst, $jsonContent);
$json
->expects($this->once())
->method('read')
->will($this->returnValue(array('hash' => 'md5')));
->will($this->returnValue(array('hash' => md5($jsonContent))));
$this->assertTrue($locker->isFresh());
}
@ -183,12 +190,47 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
$locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
$json
->expects($this->once())
->method('read')
->will($this->returnValue(array('hash' => 'oldmd5')));
->will($this->returnValue(array('hash' => $this->getJsonContent(array('name' => 'test2')))));
$this->assertFalse($locker->isFresh());
}
public function testIsFreshWithContentHash()
{
$json = $this->createJsonFileMock();
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$jsonContent = $this->getJsonContent();
$locker = new Locker(new NullIO, $json, $repo, $inst, $jsonContent);
$json
->expects($this->once())
->method('read')
->will($this->returnValue(array('hash' => md5($jsonContent . ' '), 'content-hash' => md5($jsonContent))));
$this->assertTrue($locker->isFresh());
}
public function testIsFreshFalseWithContentHash()
{
$json = $this->createJsonFileMock();
$repo = $this->createRepositoryManagerMock();
$inst = $this->createInstallationManagerMock();
$locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
$differentHash = md5($this->getJsonContent(array('name' => 'test2')));
$json
->expects($this->once())
->method('read')
->will($this->returnValue(array('hash' => $differentHash, 'content-hash' => $differentHash)));
$this->assertFalse($locker->isFresh());
}
@ -227,4 +269,16 @@ class LockerTest extends \PHPUnit_Framework_TestCase
return $this->getMockBuilder('Composer\Package\PackageInterface')
->getMock();
}
private function getJsonContent(array $customData = array())
{
$data = array_merge(array(
'minimum-stability' => 'beta',
'name' => 'test',
), $customData);
ksort($data);
return json_encode($data);
}
}

Loading…
Cancel
Save