2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Package;
|
|
|
|
|
2021-12-10 12:14:04 +00:00
|
|
|
use Composer\Json\JsonFile;
|
2011-10-01 12:32:56 +00:00
|
|
|
use Composer\Package\Locker;
|
2020-02-28 09:21:24 +00:00
|
|
|
use Composer\Plugin\PluginInterface;
|
2013-08-10 00:43:40 +00:00
|
|
|
use Composer\IO\NullIO;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2017-11-04 14:52:13 +00:00
|
|
|
class LockerTest extends TestCase
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsLocked(): void
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
2017-03-08 14:07:29 +00:00
|
|
|
$json = $this->createJsonFileMock();
|
2018-07-24 12:32:52 +00:00
|
|
|
$locker = new Locker(
|
|
|
|
new NullIO,
|
|
|
|
$json,
|
2019-08-02 19:39:26 +00:00
|
|
|
$this->createInstallationManagerMock(),
|
2018-07-24 12:32:52 +00:00
|
|
|
$this->getJsonContent()
|
|
|
|
);
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
$json
|
2012-04-15 17:05:50 +00:00
|
|
|
->expects($this->any())
|
2011-10-01 12:32:56 +00:00
|
|
|
->method('exists')
|
|
|
|
->will($this->returnValue(true));
|
2012-04-15 17:05:50 +00:00
|
|
|
$json
|
|
|
|
->expects($this->any())
|
|
|
|
->method('read')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(['packages' => []]));
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($locker->isLocked());
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetNotLockedPackages(): void
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
2012-06-20 10:05:18 +00:00
|
|
|
$inst = $this->createInstallationManagerMock();
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('exists')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('LogicException');
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2012-09-14 14:43:56 +00:00
|
|
|
$locker->getLockedRepository();
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetLockedPackages(): void
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
2012-06-20 10:05:18 +00:00
|
|
|
$inst = $this->createInstallationManagerMock();
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('exists')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('read')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([
|
|
|
|
'packages' => [
|
|
|
|
['name' => 'pkg1', 'version' => '1.0.0-beta'],
|
|
|
|
['name' => 'pkg2', 'version' => '0.1.10'],
|
|
|
|
],
|
|
|
|
]));
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2013-03-28 19:42:25 +00:00
|
|
|
$repo = $locker->getLockedRepository();
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertNotNull($repo->findPackage('pkg1', '1.0.0-beta'));
|
|
|
|
self::assertNotNull($repo->findPackage('pkg2', '0.1.10'));
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSetLockData(): void
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
2012-06-20 10:05:18 +00:00
|
|
|
$inst = $this->createInstallationManagerMock();
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2015-08-21 04:54:28 +00:00
|
|
|
$jsonContent = $this->getJsonContent() . ' ';
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $jsonContent);
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$package1 = self::getPackage('pkg1', '1.0.0-beta');
|
|
|
|
$package2 = self::getPackage('pkg2', '0.1.10');
|
2016-11-25 07:28:49 +00:00
|
|
|
|
2024-08-21 15:06:42 +00:00
|
|
|
$contentHash = hash('md5', trim($jsonContent));
|
2015-08-21 04:54:28 +00:00
|
|
|
|
2011-10-01 12:32:56 +00:00
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('write')
|
2022-08-17 12:20:07 +00:00
|
|
|
->with([
|
|
|
|
'_readme' => ['This file locks the dependencies of your project to a known state',
|
2018-02-09 08:03:28 +00:00
|
|
|
'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies',
|
2022-08-17 12:20:07 +00:00
|
|
|
'This file is @gener'.'ated automatically', ],
|
2015-08-21 04:54:28 +00:00
|
|
|
'content-hash' => $contentHash,
|
2022-08-17 12:20:07 +00:00
|
|
|
'packages' => [
|
|
|
|
['name' => 'pkg1', 'version' => '1.0.0-beta', 'type' => 'library'],
|
|
|
|
['name' => 'pkg2', 'version' => '0.1.10', 'type' => 'library'],
|
|
|
|
],
|
|
|
|
'packages-dev' => [],
|
|
|
|
'aliases' => [],
|
2012-05-11 15:20:10 +00:00
|
|
|
'minimum-stability' => 'dev',
|
2024-09-18 14:32:40 +00:00
|
|
|
'stability-flags' => new \stdClass,
|
|
|
|
'platform' => new \stdClass,
|
|
|
|
'platform-dev' => new \stdClass,
|
2022-08-17 12:20:07 +00:00
|
|
|
'platform-overrides' => ['foo/bar' => '1.0'],
|
2014-07-07 17:51:00 +00:00
|
|
|
'prefer-stable' => false,
|
2014-12-13 22:56:15 +00:00
|
|
|
'prefer-lowest' => false,
|
2020-02-28 09:21:24 +00:00
|
|
|
'plugin-api-version' => PluginInterface::PLUGIN_API_VERSION,
|
2022-08-17 12:20:07 +00:00
|
|
|
]);
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$locker->setLockData([$package1, $package2], [], [], [], [], 'dev', [], false, false, ['foo/bar' => '1.0']);
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testLockBadPackages(): void
|
2011-10-01 12:32:56 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
2012-06-20 10:05:18 +00:00
|
|
|
$inst = $this->createInstallationManagerMock();
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
|
2011-10-01 12:32:56 +00:00
|
|
|
|
|
|
|
$package1 = $this->createPackageMock();
|
|
|
|
$package1
|
|
|
|
->expects($this->once())
|
2011-11-20 14:46:15 +00:00
|
|
|
->method('getPrettyName')
|
2011-10-01 12:32:56 +00:00
|
|
|
->will($this->returnValue('pkg1'));
|
|
|
|
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('LogicException');
|
2011-10-01 12:32:56 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$locker->setLockData([$package1], [], [], [], [], 'dev', [], false, false, []);
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsFresh(): void
|
2012-01-06 12:26:25 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
2012-06-20 10:05:18 +00:00
|
|
|
$inst = $this->createInstallationManagerMock();
|
2012-01-06 12:26:25 +00:00
|
|
|
|
2015-08-21 04:54:28 +00:00
|
|
|
$jsonContent = $this->getJsonContent();
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $jsonContent);
|
2012-01-06 12:26:25 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('read')
|
2024-08-21 15:06:42 +00:00
|
|
|
->will($this->returnValue(['hash' => hash('md5', $jsonContent)]));
|
2012-01-06 12:26:25 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($locker->isFresh());
|
2012-01-06 12:26:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsFreshFalse(): void
|
2012-01-06 12:26:25 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
2012-06-20 10:05:18 +00:00
|
|
|
$inst = $this->createInstallationManagerMock();
|
2012-01-06 12:26:25 +00:00
|
|
|
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
|
2012-01-06 12:26:25 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('read')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(['hash' => $this->getJsonContent(['name' => 'test2'])]));
|
2012-01-06 12:26:25 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertFalse($locker->isFresh());
|
2015-06-12 20:24:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsFreshWithContentHash(): void
|
2015-06-12 20:24:31 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
|
|
|
$inst = $this->createInstallationManagerMock();
|
|
|
|
|
2015-08-21 04:54:28 +00:00
|
|
|
$jsonContent = $this->getJsonContent();
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $jsonContent);
|
2015-06-12 20:24:31 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('read')
|
2024-08-21 15:06:42 +00:00
|
|
|
->will($this->returnValue(['hash' => hash('md5', $jsonContent . ' '), 'content-hash' => hash('md5', $jsonContent)]));
|
2015-06-12 20:24:31 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($locker->isFresh());
|
2015-06-12 20:24:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsFreshWithContentHashAndNoHash(): void
|
2016-09-10 09:41:07 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
|
|
|
$inst = $this->createInstallationManagerMock();
|
|
|
|
|
|
|
|
$jsonContent = $this->getJsonContent();
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $jsonContent);
|
2016-09-10 09:41:07 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('read')
|
2024-08-21 15:06:42 +00:00
|
|
|
->will($this->returnValue(['content-hash' => hash('md5', $jsonContent)]));
|
2016-09-10 09:41:07 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue($locker->isFresh());
|
2016-09-10 09:41:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsFreshFalseWithContentHash(): void
|
2015-06-12 20:24:31 +00:00
|
|
|
{
|
|
|
|
$json = $this->createJsonFileMock();
|
|
|
|
$inst = $this->createInstallationManagerMock();
|
|
|
|
|
2017-12-27 12:20:12 +00:00
|
|
|
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
|
2015-08-21 04:54:28 +00:00
|
|
|
|
2024-08-21 15:06:42 +00:00
|
|
|
$differentHash = hash('md5', $this->getJsonContent(['name' => 'test2']));
|
2015-06-12 20:24:31 +00:00
|
|
|
|
|
|
|
$json
|
|
|
|
->expects($this->once())
|
|
|
|
->method('read')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(['hash' => $differentHash, 'content-hash' => $differentHash]));
|
2015-06-12 20:24:31 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertFalse($locker->isFresh());
|
2012-01-06 12:26:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Json\JsonFile
|
|
|
|
*/
|
2011-10-01 12:32:56 +00:00
|
|
|
private function createJsonFileMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\Json\JsonFile')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
2019-08-02 19:39:26 +00:00
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Installer\InstallationManager
|
|
|
|
*/
|
2012-06-20 10:05:18 +00:00
|
|
|
private function createInstallationManagerMock()
|
|
|
|
{
|
|
|
|
$mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Package\PackageInterface
|
|
|
|
*/
|
2011-10-01 12:32:56 +00:00
|
|
|
private function createPackageMock()
|
|
|
|
{
|
2021-12-10 12:14:04 +00:00
|
|
|
return $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|
2015-08-21 04:54:28 +00:00
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, string> $customData
|
|
|
|
*/
|
2022-08-17 12:20:07 +00:00
|
|
|
private function getJsonContent(array $customData = []): string
|
2015-08-21 04:54:28 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$data = array_merge([
|
2015-08-21 04:54:28 +00:00
|
|
|
'minimum-stability' => 'beta',
|
|
|
|
'name' => 'test',
|
2022-08-17 12:20:07 +00:00
|
|
|
], $customData);
|
2015-08-21 04:54:28 +00:00
|
|
|
|
|
|
|
ksort($data);
|
|
|
|
|
2021-12-10 12:14:04 +00:00
|
|
|
return JsonFile::encode($data, 0);
|
2015-08-21 04:54:28 +00:00
|
|
|
}
|
2011-10-01 12:32:56 +00:00
|
|
|
}
|