PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/session24/Home Practice/Mobile Management System/vendor/composer/composer/tests/Composer/Test/Package/LockerTest.php

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