LockerTest.php 8.5 KB

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