LockerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(), '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. $locker = new Locker($json, $repo, 'md5');
  34. $json
  35. ->expects($this->once())
  36. ->method('exists')
  37. ->will($this->returnValue(false));
  38. $this->setExpectedException('LogicException');
  39. $locker->getLockedPackages();
  40. }
  41. public function testGetLockedPackages()
  42. {
  43. $json = $this->createJsonFileMock();
  44. $repo = $this->createRepositoryManagerMock();
  45. $locker = new Locker($json, $repo, 'md5');
  46. $json
  47. ->expects($this->once())
  48. ->method('exists')
  49. ->will($this->returnValue(true));
  50. $json
  51. ->expects($this->once())
  52. ->method('read')
  53. ->will($this->returnValue(array(
  54. 'packages' => array(
  55. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  56. array('package' => 'pkg2', 'version' => '0.1.10')
  57. )
  58. )));
  59. $package1 = $this->createPackageMock();
  60. $package2 = $this->createPackageMock();
  61. $repo->getLocalRepository()
  62. ->expects($this->exactly(2))
  63. ->method('findPackage')
  64. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  65. ->will($this->onConsecutiveCalls($package1, $package2));
  66. $this->assertEquals(array($package1, $package2), $locker->getLockedPackages());
  67. }
  68. public function testGetPackagesWithoutRepo()
  69. {
  70. $json = $this->createJsonFileMock();
  71. $repo = $this->createRepositoryManagerMock();
  72. $locker = new Locker($json, $repo, 'md5');
  73. $json
  74. ->expects($this->once())
  75. ->method('exists')
  76. ->will($this->returnValue(true));
  77. $json
  78. ->expects($this->once())
  79. ->method('read')
  80. ->will($this->returnValue(array(
  81. 'packages' => array(
  82. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  83. array('package' => 'pkg2', 'version' => '0.1.10')
  84. )
  85. )));
  86. $package1 = $this->createPackageMock();
  87. $package2 = $this->createPackageMock();
  88. $repo->getLocalRepository()
  89. ->expects($this->exactly(2))
  90. ->method('findPackage')
  91. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  92. ->will($this->onConsecutiveCalls($package1, null));
  93. $this->setExpectedException('LogicException');
  94. $locker->getLockedPackages();
  95. }
  96. public function testSetLockData()
  97. {
  98. $json = $this->createJsonFileMock();
  99. $repo = $this->createRepositoryManagerMock();
  100. $locker = new Locker($json, $repo, 'md5');
  101. $package1 = $this->createPackageMock();
  102. $package2 = $this->createPackageMock();
  103. $package1
  104. ->expects($this->once())
  105. ->method('getPrettyName')
  106. ->will($this->returnValue('pkg1'));
  107. $package1
  108. ->expects($this->once())
  109. ->method('getPrettyVersion')
  110. ->will($this->returnValue('1.0.0-beta'));
  111. $package2
  112. ->expects($this->once())
  113. ->method('getPrettyName')
  114. ->will($this->returnValue('pkg2'));
  115. $package2
  116. ->expects($this->once())
  117. ->method('getPrettyVersion')
  118. ->will($this->returnValue('0.1.10'));
  119. $json
  120. ->expects($this->once())
  121. ->method('write')
  122. ->with(array(
  123. 'hash' => 'md5',
  124. 'packages' => array(
  125. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  126. array('package' => 'pkg2', 'version' => '0.1.10')
  127. ),
  128. 'packages-dev' => array(),
  129. 'aliases' => array(),
  130. ));
  131. $locker->setLockData(array($package1, $package2), array(), array());
  132. }
  133. public function testLockBadPackages()
  134. {
  135. $json = $this->createJsonFileMock();
  136. $repo = $this->createRepositoryManagerMock();
  137. $locker = new Locker($json, $repo, 'md5');
  138. $package1 = $this->createPackageMock();
  139. $package1
  140. ->expects($this->once())
  141. ->method('getPrettyName')
  142. ->will($this->returnValue('pkg1'));
  143. $this->setExpectedException('LogicException');
  144. $locker->setLockData(array($package1), array(), array());
  145. }
  146. public function testIsFresh()
  147. {
  148. $json = $this->createJsonFileMock();
  149. $repo = $this->createRepositoryManagerMock();
  150. $locker = new Locker($json, $repo, 'md5');
  151. $json
  152. ->expects($this->once())
  153. ->method('read')
  154. ->will($this->returnValue(array('hash' => 'md5')));
  155. $this->assertTrue($locker->isFresh());
  156. }
  157. public function testIsFreshFalse()
  158. {
  159. $json = $this->createJsonFileMock();
  160. $repo = $this->createRepositoryManagerMock();
  161. $locker = new Locker($json, $repo, 'md5');
  162. $json
  163. ->expects($this->once())
  164. ->method('read')
  165. ->will($this->returnValue(array('hash' => 'oldmd5')));
  166. $this->assertFalse($locker->isFresh());
  167. }
  168. private function createJsonFileMock()
  169. {
  170. return $this->getMockBuilder('Composer\Json\JsonFile')
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. }
  174. private function createRepositoryManagerMock()
  175. {
  176. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $mock->expects($this->any())
  180. ->method('getLocalRepository')
  181. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  182. return $mock;
  183. }
  184. private function createPackageMock()
  185. {
  186. return $this->getMockBuilder('Composer\Package\PackageInterface')
  187. ->getMock();
  188. }
  189. }