LockerTest.php 10 KB

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