LockerTest.php 9.9 KB

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