LockerTest.php 9.7 KB

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