LockerTest.php 6.8 KB

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