123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace Composer\Test\Package;
- use Composer\Package\Locker;
- class LockerTest extends \PHPUnit_Framework_TestCase
- {
- public function testIsLocked()
- {
- $json = $this->createJsonFileMock();
- $locker = new Locker($json, $this->createRepositoryManagerMock(), $this->createInstallationManagerMock(), 'md5');
- $json
- ->expects($this->any())
- ->method('exists')
- ->will($this->returnValue(true));
- $json
- ->expects($this->any())
- ->method('read')
- ->will($this->returnValue(array('packages' => array())));
- $this->assertTrue($locker->isLocked());
- }
- public function testGetNotLockedPackages()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $json
- ->expects($this->once())
- ->method('exists')
- ->will($this->returnValue(false));
- $this->setExpectedException('LogicException');
- $locker->getLockedPackages();
- }
- public function testGetLockedPackages()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $json
- ->expects($this->once())
- ->method('exists')
- ->will($this->returnValue(true));
- $json
- ->expects($this->once())
- ->method('read')
- ->will($this->returnValue(array(
- 'packages' => array(
- array('package' => 'pkg1', 'version' => '1.0.0-beta'),
- array('package' => 'pkg2', 'version' => '0.1.10')
- )
- )));
- $package1 = $this->createPackageMock();
- $package2 = $this->createPackageMock();
- $repo->getLocalRepository()
- ->expects($this->exactly(2))
- ->method('findPackage')
- ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
- ->will($this->onConsecutiveCalls($package1, $package2));
- $this->assertEquals(array($package1, $package2), $locker->getLockedPackages());
- }
- public function testGetPackagesWithoutRepo()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $json
- ->expects($this->once())
- ->method('exists')
- ->will($this->returnValue(true));
- $json
- ->expects($this->once())
- ->method('read')
- ->will($this->returnValue(array(
- 'packages' => array(
- array('package' => 'pkg1', 'version' => '1.0.0-beta'),
- array('package' => 'pkg2', 'version' => '0.1.10')
- )
- )));
- $package1 = $this->createPackageMock();
- $package2 = $this->createPackageMock();
- $repo->getLocalRepository()
- ->expects($this->exactly(2))
- ->method('findPackage')
- ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
- ->will($this->onConsecutiveCalls($package1, null));
- $this->setExpectedException('LogicException');
- $locker->getLockedPackages();
- }
- public function testSetLockData()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $package1 = $this->createPackageMock();
- $package2 = $this->createPackageMock();
- $package1
- ->expects($this->once())
- ->method('getPrettyName')
- ->will($this->returnValue('pkg1'));
- $package1
- ->expects($this->once())
- ->method('getVersion')
- ->will($this->returnValue('1.0.0-beta'));
- $package2
- ->expects($this->once())
- ->method('getPrettyName')
- ->will($this->returnValue('pkg2'));
- $package2
- ->expects($this->once())
- ->method('getVersion')
- ->will($this->returnValue('0.1.10'));
- $json
- ->expects($this->once())
- ->method('write')
- ->with(array(
- 'hash' => 'md5',
- 'packages' => array(
- array('package' => 'pkg1', 'version' => '1.0.0-beta'),
- array('package' => 'pkg2', 'version' => '0.1.10')
- ),
- 'packages-dev' => array(),
- 'aliases' => array(),
- 'minimum-stability' => 'dev',
- 'stability-flags' => array(),
- ));
- $locker->setLockData(array($package1, $package2), array(), array(), 'dev', array());
- }
- public function testLockBadPackages()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $package1 = $this->createPackageMock();
- $package1
- ->expects($this->once())
- ->method('getPrettyName')
- ->will($this->returnValue('pkg1'));
- $this->setExpectedException('LogicException');
- $locker->setLockData(array($package1), array(), array(), 'dev', array());
- }
- public function testIsFresh()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $json
- ->expects($this->once())
- ->method('read')
- ->will($this->returnValue(array('hash' => 'md5')));
- $this->assertTrue($locker->isFresh());
- }
- public function testIsFreshFalse()
- {
- $json = $this->createJsonFileMock();
- $repo = $this->createRepositoryManagerMock();
- $inst = $this->createInstallationManagerMock();
- $locker = new Locker($json, $repo, $inst, 'md5');
- $json
- ->expects($this->once())
- ->method('read')
- ->will($this->returnValue(array('hash' => 'oldmd5')));
- $this->assertFalse($locker->isFresh());
- }
- private function createJsonFileMock()
- {
- return $this->getMockBuilder('Composer\Json\JsonFile')
- ->disableOriginalConstructor()
- ->getMock();
- }
- private function createRepositoryManagerMock()
- {
- $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
- ->disableOriginalConstructor()
- ->getMock();
- $mock->expects($this->any())
- ->method('getLocalRepository')
- ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
- return $mock;
- }
- private function createInstallationManagerMock()
- {
- $mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
- ->disableOriginalConstructor()
- ->getMock();
- return $mock;
- }
- private function createPackageMock()
- {
- return $this->getMockBuilder('Composer\Package\PackageInterface')
- ->getMock();
- }
- }
|