LockerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. 'minimum-stability' => 'dev',
  131. 'stability-flags' => array(),
  132. ));
  133. $locker->setLockData(array($package1, $package2), array(), array(), 'dev', array());
  134. }
  135. public function testLockBadPackages()
  136. {
  137. $json = $this->createJsonFileMock();
  138. $repo = $this->createRepositoryManagerMock();
  139. $locker = new Locker($json, $repo, 'md5');
  140. $package1 = $this->createPackageMock();
  141. $package1
  142. ->expects($this->once())
  143. ->method('getPrettyName')
  144. ->will($this->returnValue('pkg1'));
  145. $this->setExpectedException('LogicException');
  146. $locker->setLockData(array($package1), array(), array(), 'dev', array());
  147. }
  148. public function testIsFresh()
  149. {
  150. $json = $this->createJsonFileMock();
  151. $repo = $this->createRepositoryManagerMock();
  152. $locker = new Locker($json, $repo, 'md5');
  153. $json
  154. ->expects($this->once())
  155. ->method('read')
  156. ->will($this->returnValue(array('hash' => 'md5')));
  157. $this->assertTrue($locker->isFresh());
  158. }
  159. public function testIsFreshFalse()
  160. {
  161. $json = $this->createJsonFileMock();
  162. $repo = $this->createRepositoryManagerMock();
  163. $locker = new Locker($json, $repo, 'md5');
  164. $json
  165. ->expects($this->once())
  166. ->method('read')
  167. ->will($this->returnValue(array('hash' => 'oldmd5')));
  168. $this->assertFalse($locker->isFresh());
  169. }
  170. private function createJsonFileMock()
  171. {
  172. return $this->getMockBuilder('Composer\Json\JsonFile')
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. }
  176. private function createRepositoryManagerMock()
  177. {
  178. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  179. ->disableOriginalConstructor()
  180. ->getMock();
  181. $mock->expects($this->any())
  182. ->method('getLocalRepository')
  183. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  184. return $mock;
  185. }
  186. private function createPackageMock()
  187. {
  188. return $this->getMockBuilder('Composer\Package\PackageInterface')
  189. ->getMock();
  190. }
  191. }