LockerTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Package;
  12. use Composer\Package\Locker;
  13. class LockerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testIsLocked()
  16. {
  17. $json = $this->createJsonFileMock();
  18. $locker = new Locker($json, $this->createRepositoryManagerMock(), $this->createInstallationManagerMock(), 'md5');
  19. $json
  20. ->expects($this->any())
  21. ->method('exists')
  22. ->will($this->returnValue(true));
  23. $json
  24. ->expects($this->any())
  25. ->method('read')
  26. ->will($this->returnValue(array('packages' => array())));
  27. $this->assertTrue($locker->isLocked());
  28. }
  29. public function testGetNotLockedPackages()
  30. {
  31. $json = $this->createJsonFileMock();
  32. $repo = $this->createRepositoryManagerMock();
  33. $inst = $this->createInstallationManagerMock();
  34. $locker = new Locker($json, $repo, $inst, 'md5');
  35. $json
  36. ->expects($this->once())
  37. ->method('exists')
  38. ->will($this->returnValue(false));
  39. $this->setExpectedException('LogicException');
  40. $locker->getLockedRepository();
  41. }
  42. public function testGetLockedPackages()
  43. {
  44. $json = $this->createJsonFileMock();
  45. $repo = $this->createRepositoryManagerMock();
  46. $inst = $this->createInstallationManagerMock();
  47. $locker = new Locker($json, $repo, $inst, 'md5');
  48. $json
  49. ->expects($this->once())
  50. ->method('exists')
  51. ->will($this->returnValue(true));
  52. $json
  53. ->expects($this->once())
  54. ->method('read')
  55. ->will($this->returnValue(array(
  56. 'packages' => array(
  57. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  58. array('package' => 'pkg2', 'version' => '0.1.10')
  59. )
  60. )));
  61. $package1 = $this->createPackageMock();
  62. $package2 = $this->createPackageMock();
  63. $repo->getLocalRepository()
  64. ->expects($this->exactly(2))
  65. ->method('findPackage')
  66. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  67. ->will($this->onConsecutiveCalls($package1, $package2));
  68. $this->assertEquals(array($package1, $package2), $locker->getLockedRepository()->getPackages());
  69. }
  70. public function testGetPackagesWithoutRepo()
  71. {
  72. $json = $this->createJsonFileMock();
  73. $repo = $this->createRepositoryManagerMock();
  74. $inst = $this->createInstallationManagerMock();
  75. $locker = new Locker($json, $repo, $inst, 'md5');
  76. $json
  77. ->expects($this->once())
  78. ->method('exists')
  79. ->will($this->returnValue(true));
  80. $json
  81. ->expects($this->once())
  82. ->method('read')
  83. ->will($this->returnValue(array(
  84. 'packages' => array(
  85. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  86. array('package' => 'pkg2', 'version' => '0.1.10')
  87. )
  88. )));
  89. $package1 = $this->createPackageMock();
  90. $package2 = $this->createPackageMock();
  91. $repo->getLocalRepository()
  92. ->expects($this->exactly(2))
  93. ->method('findPackage')
  94. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  95. ->will($this->onConsecutiveCalls($package1, null));
  96. $this->setExpectedException('LogicException');
  97. $locker->getLockedRepository();
  98. }
  99. public function testSetLockData()
  100. {
  101. $json = $this->createJsonFileMock();
  102. $repo = $this->createRepositoryManagerMock();
  103. $inst = $this->createInstallationManagerMock();
  104. $locker = new Locker($json, $repo, $inst, 'md5');
  105. $package1 = $this->createPackageMock();
  106. $package2 = $this->createPackageMock();
  107. $package1
  108. ->expects($this->atLeastOnce())
  109. ->method('getPrettyName')
  110. ->will($this->returnValue('pkg1'));
  111. $package1
  112. ->expects($this->atLeastOnce())
  113. ->method('getPrettyVersion')
  114. ->will($this->returnValue('1.0.0-beta'));
  115. $package1
  116. ->expects($this->atLeastOnce())
  117. ->method('getVersion')
  118. ->will($this->returnValue('1.0.0.0-beta'));
  119. $package2
  120. ->expects($this->atLeastOnce())
  121. ->method('getPrettyName')
  122. ->will($this->returnValue('pkg2'));
  123. $package2
  124. ->expects($this->atLeastOnce())
  125. ->method('getPrettyVersion')
  126. ->will($this->returnValue('0.1.10'));
  127. $package2
  128. ->expects($this->atLeastOnce())
  129. ->method('getVersion')
  130. ->will($this->returnValue('0.1.10.0'));
  131. $json
  132. ->expects($this->once())
  133. ->method('write')
  134. ->with(array(
  135. 'hash' => 'md5',
  136. 'packages' => array(
  137. array('name' => 'pkg1', 'version' => '1.0.0-beta'),
  138. array('name' => 'pkg2', 'version' => '0.1.10')
  139. ),
  140. 'packages-dev' => array(),
  141. 'aliases' => array(),
  142. 'minimum-stability' => 'dev',
  143. 'stability-flags' => array(),
  144. 'platform' => array(),
  145. 'platform-dev' => array(),
  146. ));
  147. $locker->setLockData(array($package1, $package2), array(), array(), array(), array(), 'dev', array());
  148. }
  149. public function testLockBadPackages()
  150. {
  151. $json = $this->createJsonFileMock();
  152. $repo = $this->createRepositoryManagerMock();
  153. $inst = $this->createInstallationManagerMock();
  154. $locker = new Locker($json, $repo, $inst, 'md5');
  155. $package1 = $this->createPackageMock();
  156. $package1
  157. ->expects($this->once())
  158. ->method('getPrettyName')
  159. ->will($this->returnValue('pkg1'));
  160. $this->setExpectedException('LogicException');
  161. $locker->setLockData(array($package1), array(), array(), array(), array(), 'dev', array());
  162. }
  163. public function testIsFresh()
  164. {
  165. $json = $this->createJsonFileMock();
  166. $repo = $this->createRepositoryManagerMock();
  167. $inst = $this->createInstallationManagerMock();
  168. $locker = new Locker($json, $repo, $inst, 'md5');
  169. $json
  170. ->expects($this->once())
  171. ->method('read')
  172. ->will($this->returnValue(array('hash' => 'md5')));
  173. $this->assertTrue($locker->isFresh());
  174. }
  175. public function testIsFreshFalse()
  176. {
  177. $json = $this->createJsonFileMock();
  178. $repo = $this->createRepositoryManagerMock();
  179. $inst = $this->createInstallationManagerMock();
  180. $locker = new Locker($json, $repo, $inst, 'md5');
  181. $json
  182. ->expects($this->once())
  183. ->method('read')
  184. ->will($this->returnValue(array('hash' => 'oldmd5')));
  185. $this->assertFalse($locker->isFresh());
  186. }
  187. private function createJsonFileMock()
  188. {
  189. return $this->getMockBuilder('Composer\Json\JsonFile')
  190. ->disableOriginalConstructor()
  191. ->getMock();
  192. }
  193. private function createRepositoryManagerMock()
  194. {
  195. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $mock->expects($this->any())
  199. ->method('getLocalRepository')
  200. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  201. return $mock;
  202. }
  203. private function createInstallationManagerMock()
  204. {
  205. $mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
  206. ->disableOriginalConstructor()
  207. ->getMock();
  208. return $mock;
  209. }
  210. private function createPackageMock()
  211. {
  212. return $this->getMockBuilder('Composer\Package\PackageInterface')
  213. ->getMock();
  214. }
  215. }