LibraryInstallerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Installer;
  12. use Composer\Installer\LibraryInstaller;
  13. use Composer\DependencyResolver\Operation;
  14. class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $dir;
  17. private $dm;
  18. private $repository;
  19. private $library;
  20. protected function setUp()
  21. {
  22. $this->dir = sys_get_temp_dir().'/composer';
  23. if (is_dir($this->dir)) {
  24. rmdir($this->dir);
  25. }
  26. $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $this->repository = $this->getMockBuilder('Composer\Repository\WritableRepositoryInterface')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. }
  33. public function testInstallerCreation()
  34. {
  35. $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
  36. $this->assertTrue(is_dir($this->dir));
  37. $file = sys_get_temp_dir().'/file';
  38. touch($file);
  39. $this->setExpectedException('UnexpectedValueException');
  40. $library = new LibraryInstaller($file, $this->dm, $this->repository);
  41. }
  42. public function testExecuteOperation()
  43. {
  44. $library = $this->getMockBuilder('Composer\Installer\LibraryInstaller')
  45. ->setConstructorArgs(array($this->dir, $this->dm, $this->repository))
  46. ->setMethods(array('install', 'update', 'uninstall'))
  47. ->getMock();
  48. $packageToInstall = $this->createPackageMock();
  49. $packageToRemove = $this->createPackageMock();
  50. $packageToUpdate = $this->createPackageMock();
  51. $updatedPackage = $this->createPackageMock();
  52. $library
  53. ->expects($this->once())
  54. ->method('install')
  55. ->with($packageToInstall);
  56. $library
  57. ->expects($this->once())
  58. ->method('uninstall')
  59. ->with($packageToRemove);
  60. $library
  61. ->expects($this->once())
  62. ->method('update')
  63. ->with($packageToUpdate, $updatedPackage);
  64. $library->executeOperation(new Operation\InstallOperation($packageToInstall));
  65. $library->executeOperation(new Operation\UninstallOperation($packageToRemove));
  66. $library->executeOperation(new Operation\UpdateOperation($packageToUpdate, $updatedPackage));
  67. }
  68. public function testIsInstalled()
  69. {
  70. $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
  71. $package = $this->createPackageMock();
  72. $this->repository
  73. ->expects($this->exactly(2))
  74. ->method('hasPackage')
  75. ->with($package)
  76. ->will($this->onConsecutiveCalls(true, false));
  77. $this->assertTrue($library->isInstalled($package));
  78. $this->assertFalse($library->isInstalled($package));
  79. }
  80. public function testInstall()
  81. {
  82. $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
  83. $package = $this->createPackageMock();
  84. $package
  85. ->expects($this->once())
  86. ->method('getName')
  87. ->will($this->returnValue('some/package'));
  88. $this->dm
  89. ->expects($this->once())
  90. ->method('download')
  91. ->with($package, $this->dir.'/some/package');
  92. $this->repository
  93. ->expects($this->once())
  94. ->method('addPackage')
  95. ->with($package);
  96. $library->install($package);
  97. }
  98. public function testUpdate()
  99. {
  100. $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
  101. $initial = $this->createPackageMock();
  102. $target = $this->createPackageMock();
  103. $initial
  104. ->expects($this->once())
  105. ->method('getName')
  106. ->will($this->returnValue('package1'));
  107. $this->repository
  108. ->expects($this->exactly(2))
  109. ->method('hasPackage')
  110. ->with($initial)
  111. ->will($this->onConsecutiveCalls(true, false));
  112. $this->dm
  113. ->expects($this->once())
  114. ->method('update')
  115. ->with($initial, $target, $this->dir.'/package1');
  116. $this->repository
  117. ->expects($this->once())
  118. ->method('removePackage')
  119. ->with($initial);
  120. $this->repository
  121. ->expects($this->once())
  122. ->method('addPackage')
  123. ->with($target);
  124. $library->update($initial, $target);
  125. $this->setExpectedException('InvalidArgumentException');
  126. $library->update($initial, $target);
  127. }
  128. public function testUninstall()
  129. {
  130. $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
  131. $package = $this->createPackageMock();
  132. $package
  133. ->expects($this->once())
  134. ->method('getName')
  135. ->will($this->returnValue('pkg'));
  136. $this->repository
  137. ->expects($this->exactly(2))
  138. ->method('hasPackage')
  139. ->with($package)
  140. ->will($this->onConsecutiveCalls(true, false));
  141. $this->dm
  142. ->expects($this->once())
  143. ->method('remove')
  144. ->with($package, $this->dir.'/pkg');
  145. $this->repository
  146. ->expects($this->once())
  147. ->method('removePackage')
  148. ->with($package);
  149. $library->uninstall($package);
  150. $this->setExpectedException('InvalidArgumentException');
  151. $library->uninstall($package);
  152. }
  153. private function createPackageMock()
  154. {
  155. return $this->getMockBuilder('Composer\Package\MemoryPackage')
  156. ->setConstructorArgs(array(md5(rand()), '1.0.0'))
  157. ->getMock();
  158. }
  159. }