LockerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. foreach (array($package1, $package2) as $package) {
  101. $package
  102. ->expects($this->atLeastOnce())
  103. ->method('getTransportOptions')
  104. ->will($this->returnValue(array()));
  105. }
  106. $contentHash = md5(trim($jsonContent));
  107. $json
  108. ->expects($this->once())
  109. ->method('write')
  110. ->with(array(
  111. '_readme' => array('This file locks the dependencies of your project to a known state',
  112. 'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file',
  113. 'This file is @gener'.'ated automatically', ),
  114. 'content-hash' => $contentHash,
  115. 'packages' => array(
  116. array('name' => 'pkg1', 'version' => '1.0.0-beta'),
  117. array('name' => 'pkg2', 'version' => '0.1.10'),
  118. ),
  119. 'packages-dev' => array(),
  120. 'aliases' => array(),
  121. 'minimum-stability' => 'dev',
  122. 'stability-flags' => array(),
  123. 'platform' => array(),
  124. 'platform-dev' => array(),
  125. 'platform-overrides' => array('foo/bar' => '1.0'),
  126. 'prefer-stable' => false,
  127. 'prefer-lowest' => false,
  128. ));
  129. $locker->setLockData(array($package1, $package2), array(), array(), array(), array(), 'dev', array(), false, false, array('foo/bar' => '1.0'));
  130. }
  131. public function testLockBadPackages()
  132. {
  133. $json = $this->createJsonFileMock();
  134. $repo = $this->createRepositoryManagerMock();
  135. $inst = $this->createInstallationManagerMock();
  136. $locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
  137. $package1 = $this->createPackageMock();
  138. $package1
  139. ->expects($this->once())
  140. ->method('getPrettyName')
  141. ->will($this->returnValue('pkg1'));
  142. $this->setExpectedException('LogicException');
  143. $locker->setLockData(array($package1), array(), array(), array(), array(), 'dev', array(), false, false, array());
  144. }
  145. public function testIsFresh()
  146. {
  147. $json = $this->createJsonFileMock();
  148. $repo = $this->createRepositoryManagerMock();
  149. $inst = $this->createInstallationManagerMock();
  150. $jsonContent = $this->getJsonContent();
  151. $locker = new Locker(new NullIO, $json, $repo, $inst, $jsonContent);
  152. $json
  153. ->expects($this->once())
  154. ->method('read')
  155. ->will($this->returnValue(array('hash' => md5($jsonContent))));
  156. $this->assertTrue($locker->isFresh());
  157. }
  158. public function testIsFreshFalse()
  159. {
  160. $json = $this->createJsonFileMock();
  161. $repo = $this->createRepositoryManagerMock();
  162. $inst = $this->createInstallationManagerMock();
  163. $locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
  164. $json
  165. ->expects($this->once())
  166. ->method('read')
  167. ->will($this->returnValue(array('hash' => $this->getJsonContent(array('name' => 'test2')))));
  168. $this->assertFalse($locker->isFresh());
  169. }
  170. public function testIsFreshWithContentHash()
  171. {
  172. $json = $this->createJsonFileMock();
  173. $repo = $this->createRepositoryManagerMock();
  174. $inst = $this->createInstallationManagerMock();
  175. $jsonContent = $this->getJsonContent();
  176. $locker = new Locker(new NullIO, $json, $repo, $inst, $jsonContent);
  177. $json
  178. ->expects($this->once())
  179. ->method('read')
  180. ->will($this->returnValue(array('hash' => md5($jsonContent . ' '), 'content-hash' => md5($jsonContent))));
  181. $this->assertTrue($locker->isFresh());
  182. }
  183. public function testIsFreshWithContentHashAndNoHash()
  184. {
  185. $json = $this->createJsonFileMock();
  186. $repo = $this->createRepositoryManagerMock();
  187. $inst = $this->createInstallationManagerMock();
  188. $jsonContent = $this->getJsonContent();
  189. $locker = new Locker(new NullIO, $json, $repo, $inst, $jsonContent);
  190. $json
  191. ->expects($this->once())
  192. ->method('read')
  193. ->will($this->returnValue(array('content-hash' => md5($jsonContent))));
  194. $this->assertTrue($locker->isFresh());
  195. }
  196. public function testIsFreshFalseWithContentHash()
  197. {
  198. $json = $this->createJsonFileMock();
  199. $repo = $this->createRepositoryManagerMock();
  200. $inst = $this->createInstallationManagerMock();
  201. $locker = new Locker(new NullIO, $json, $repo, $inst, $this->getJsonContent());
  202. $differentHash = md5($this->getJsonContent(array('name' => 'test2')));
  203. $json
  204. ->expects($this->once())
  205. ->method('read')
  206. ->will($this->returnValue(array('hash' => $differentHash, 'content-hash' => $differentHash)));
  207. $this->assertFalse($locker->isFresh());
  208. }
  209. private function createJsonFileMock()
  210. {
  211. return $this->getMockBuilder('Composer\Json\JsonFile')
  212. ->disableOriginalConstructor()
  213. ->getMock();
  214. }
  215. private function createRepositoryManagerMock()
  216. {
  217. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  218. ->disableOriginalConstructor()
  219. ->getMock();
  220. $mock->expects($this->any())
  221. ->method('getLocalRepository')
  222. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  223. return $mock;
  224. }
  225. private function createInstallationManagerMock()
  226. {
  227. $mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
  228. ->disableOriginalConstructor()
  229. ->getMock();
  230. return $mock;
  231. }
  232. private function createPackageMock()
  233. {
  234. return $this->getMockBuilder('Composer\Package\PackageInterface')
  235. ->getMock();
  236. }
  237. private function getJsonContent(array $customData = array())
  238. {
  239. $data = array_merge(array(
  240. 'minimum-stability' => 'beta',
  241. 'name' => 'test',
  242. ), $customData);
  243. ksort($data);
  244. return json_encode($data);
  245. }
  246. }