1
0
Fork 0
composer/tests/Composer/Test/Package/LockerTest.php

271 lines
8.5 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
/*
* 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;
use Composer\Package\Locker;
use Composer\Plugin\PluginInterface;
use Composer\IO\NullIO;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase;
class LockerTest extends TestCase
{
public function testIsLocked(): void
{
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,
$this->createInstallationManagerMock(),
2018-07-24 12:32:52 +00:00
$this->getJsonContent()
);
$json
2012-04-15 17:05:50 +00:00
->expects($this->any())
->method('exists')
->will($this->returnValue(true));
2012-04-15 17:05:50 +00:00
$json
->expects($this->any())
->method('read')
->will($this->returnValue(array('packages' => array())));
$this->assertTrue($locker->isLocked());
}
public function testGetNotLockedPackages(): void
{
$json = $this->createJsonFileMock();
2012-06-20 10:05:18 +00:00
$inst = $this->createInstallationManagerMock();
2017-12-27 12:20:12 +00:00
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
$json
->expects($this->once())
->method('exists')
->will($this->returnValue(false));
2021-12-09 19:55:26 +00:00
self::expectException('LogicException');
$locker->getLockedRepository();
}
public function testGetLockedPackages(): void
{
$json = $this->createJsonFileMock();
2012-06-20 10:05:18 +00:00
$inst = $this->createInstallationManagerMock();
2017-12-27 12:20:12 +00:00
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
$json
->expects($this->once())
->method('exists')
->will($this->returnValue(true));
$json
->expects($this->once())
->method('read')
->will($this->returnValue(array(
'packages' => array(
2013-03-28 19:42:25 +00:00
array('name' => 'pkg1', 'version' => '1.0.0-beta'),
2015-09-28 09:51:14 +00:00
array('name' => 'pkg2', 'version' => '0.1.10'),
),
)));
2013-03-28 19:42:25 +00:00
$repo = $locker->getLockedRepository();
$this->assertNotNull($repo->findPackage('pkg1', '1.0.0-beta'));
$this->assertNotNull($repo->findPackage('pkg2', '0.1.10'));
}
public function testSetLockData(): void
{
$json = $this->createJsonFileMock();
2012-06-20 10:05:18 +00:00
$inst = $this->createInstallationManagerMock();
$jsonContent = $this->getJsonContent() . ' ';
2017-12-27 12:20:12 +00:00
$locker = new Locker(new NullIO, $json, $inst, $jsonContent);
2022-02-22 21:10:52 +00:00
$package1 = $this->getPackage('pkg1', '1.0.0-beta');
$package2 = $this->getPackage('pkg2', '0.1.10');
$contentHash = md5(trim($jsonContent));
$json
->expects($this->once())
->method('write')
->with(array(
'_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#installing-dependencies',
2015-09-28 09:51:14 +00:00
'This file is @gener'.'ated automatically', ),
'content-hash' => $contentHash,
'packages' => array(
2022-02-22 21:10:52 +00:00
array('name' => 'pkg1', 'version' => '1.0.0-beta', 'type' => 'library'),
array('name' => 'pkg2', 'version' => '0.1.10', 'type' => 'library'),
),
2012-04-14 13:45:25 +00:00
'packages-dev' => array(),
'aliases' => array(),
'minimum-stability' => 'dev',
'stability-flags' => array(),
'platform' => array(),
'platform-dev' => array(),
'platform-overrides' => array('foo/bar' => '1.0'),
'prefer-stable' => false,
2014-12-13 22:56:15 +00:00
'prefer-lowest' => false,
'plugin-api-version' => PluginInterface::PLUGIN_API_VERSION,
));
$locker->setLockData(array($package1, $package2), array(), array(), array(), array(), 'dev', array(), false, false, array('foo/bar' => '1.0'));
}
public function testLockBadPackages(): void
{
$json = $this->createJsonFileMock();
2012-06-20 10:05:18 +00:00
$inst = $this->createInstallationManagerMock();
2017-12-27 12:20:12 +00:00
$locker = new Locker(new NullIO, $json, $inst, $this->getJsonContent());
$package1 = $this->createPackageMock();
$package1
->expects($this->once())
2011-11-20 14:46:15 +00:00
->method('getPrettyName')
->will($this->returnValue('pkg1'));
2021-12-09 19:55:26 +00:00
self::expectException('LogicException');
$locker->setLockData(array($package1), array(), array(), array(), array(), 'dev', array(), false, false, array());
}
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
$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')
->will($this->returnValue(array('hash' => md5($jsonContent))));
2012-01-06 12:26:25 +00:00
$this->assertTrue($locker->isFresh());
}
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')
->will($this->returnValue(array('hash' => $this->getJsonContent(array('name' => 'test2')))));
2012-01-06 12:26:25 +00:00
$this->assertFalse($locker->isFresh());
}
public function testIsFreshWithContentHash(): void
{
$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);
$json
->expects($this->once())
->method('read')
->will($this->returnValue(array('hash' => md5($jsonContent . ' '), 'content-hash' => md5($jsonContent))));
$this->assertTrue($locker->isFresh());
}
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')
->will($this->returnValue(array('content-hash' => md5($jsonContent))));
$this->assertTrue($locker->isFresh());
}
public function testIsFreshFalseWithContentHash(): void
{
$json = $this->createJsonFileMock();
$inst = $this->createInstallationManagerMock();
2017-12-27 12:20:12 +00:00
$locker = new Locker(new NullIO, $json, $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());
2012-01-06 12:26:25 +00:00
}
2021-11-01 20:44:12 +00:00
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Json\JsonFile
*/
private function createJsonFileMock()
{
return $this->getMockBuilder('Composer\Json\JsonFile')
->disableOriginalConstructor()
->getMock();
}
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
*/
private function createPackageMock()
{
2021-12-10 12:14:04 +00:00
return $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
}
2021-11-01 20:44:12 +00:00
/**
* @param array<string, string> $customData
*/
2021-12-10 12:14:04 +00:00
private function getJsonContent(array $customData = array()): string
{
$data = array_merge(array(
'minimum-stability' => 'beta',
'name' => 'test',
), $customData);
ksort($data);
2021-12-10 12:14:04 +00:00
return JsonFile::encode($data, 0);
}
}