LockerTest.php 6.7 KB

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