PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Composer/Test/Package/LockerTest.php

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