LockerTest.php 9.0 KB

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