123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- /*
- * 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\Installer;
- use Composer\Installer\LibraryInstaller;
- use Composer\DependencyResolver\Operation;
- class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
- {
- private $dir;
- private $dm;
- private $registry;
- private $library;
- protected function setUp()
- {
- $this->dir = sys_get_temp_dir().'/composer';
- if (is_dir($this->dir)) {
- rmdir($this->dir);
- }
- $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->registry = $this->getMockBuilder('Composer\Installer\Registry\RegistryInterface')
- ->disableOriginalConstructor()
- ->getMock();
- }
- public function testInstallerCreation()
- {
- $this->registry
- ->expects($this->once())
- ->method('open');
- $this->registry
- ->expects($this->once())
- ->method('close');
- $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
- $this->assertTrue(is_dir($this->dir));
- $file = sys_get_temp_dir().'/file';
- touch($file);
- $this->setExpectedException('UnexpectedValueException');
- $library = new LibraryInstaller($file, $this->dm, $this->registry);
- }
- public function testExecuteOperation()
- {
- $library = $this->getMockBuilder('Composer\Installer\LibraryInstaller')
- ->setConstructorArgs(array($this->dir, $this->dm, $this->registry))
- ->setMethods(array('install', 'update', 'uninstall'))
- ->getMock();
- $packageToInstall = $this->createPackageMock();
- $packageToRemove = $this->createPackageMock();
- $packageToUpdate = $this->createPackageMock();
- $updatedPackage = $this->createPackageMock();
- $library
- ->expects($this->once())
- ->method('install')
- ->with($packageToInstall);
- $library
- ->expects($this->once())
- ->method('uninstall')
- ->with($packageToRemove);
- $library
- ->expects($this->once())
- ->method('update')
- ->with($packageToUpdate, $updatedPackage);
- $library->executeOperation(new Operation\InstallOperation($packageToInstall));
- $library->executeOperation(new Operation\UninstallOperation($packageToRemove));
- $library->executeOperation(new Operation\UpdateOperation($packageToUpdate, $updatedPackage));
- }
- public function testIsInstalled()
- {
- $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
- $package = $this->createPackageMock();
- $this->registry
- ->expects($this->exactly(2))
- ->method('isPackageRegistered')
- ->with($package)
- ->will($this->onConsecutiveCalls(true, false));
- $this->assertTrue($library->isInstalled($package));
- $this->assertFalse($library->isInstalled($package));
- }
- public function testInstall()
- {
- $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
- $package = $this->createPackageMock();
- $package
- ->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('some/package'));
- $this->dm
- ->expects($this->once())
- ->method('download')
- ->with($package, $this->dir.'/some/package')
- ->will($this->returnValue('source'));
- $this->registry
- ->expects($this->once())
- ->method('registerPackage')
- ->with($package, 'source');
- $library->install($package);
- }
- public function testUpdate()
- {
- $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
- $initial = $this->createPackageMock();
- $target = $this->createPackageMock();
- $initial
- ->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('package1'));
- $this->registry
- ->expects($this->exactly(2))
- ->method('isPackageRegistered')
- ->with($initial)
- ->will($this->onConsecutiveCalls(true, false));
- $this->registry
- ->expects($this->once())
- ->method('getRegisteredPackageInstallerType')
- ->with($initial)
- ->will($this->returnValue('dist'));
- $this->dm
- ->expects($this->once())
- ->method('update')
- ->with($initial, $target, $this->dir.'/package1', 'dist');
- $this->registry
- ->expects($this->once())
- ->method('unregisterPackage')
- ->with($initial);
- $this->registry
- ->expects($this->once())
- ->method('registerPackage')
- ->with($target, 'dist');
- $library->update($initial, $target);
- $this->setExpectedException('UnexpectedValueException');
- $library->update($initial, $target);
- }
- public function testUninstall()
- {
- $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
- $package = $this->createPackageMock();
- $package
- ->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('pkg'));
- $this->registry
- ->expects($this->exactly(2))
- ->method('isPackageRegistered')
- ->with($package)
- ->will($this->onConsecutiveCalls(true, false));
- $this->registry
- ->expects($this->once())
- ->method('getRegisteredPackageInstallerType')
- ->with($package)
- ->will($this->returnValue('source'));
- $this->dm
- ->expects($this->once())
- ->method('remove')
- ->with($package, $this->dir.'/pkg', 'source');
- $this->registry
- ->expects($this->once())
- ->method('unregisterPackage')
- ->with($package);
- $library->uninstall($package);
- $this->setExpectedException('UnexpectedValueException');
- $library->uninstall($package);
- }
- private function createPackageMock()
- {
- return $this->getMockBuilder('Composer\Package\MemoryPackage')
- ->setConstructorArgs(array(md5(rand()), '1.0.0'))
- ->getMock();
- }
- }
|