LockerTest.php 10.0 KB

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