LockerTest.php 8.9 KB

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