PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/skug/composer
PHP | 256 lines | 196 code | 51 blank | 9 comment | 0 complexity | deca9d59c295fb9b5e12dcf10513311e 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. class LockerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testIsLocked()
  16. {
  17. $json = $this->createJsonFileMock();
  18. $locker = new Locker($json, $this->createRepositoryManagerMock(), $this->createInstallationManagerMock(), 'md5');
  19. $json
  20. ->expects($this->any())
  21. ->method('exists')
  22. ->will($this->returnValue(true));
  23. $json
  24. ->expects($this->any())
  25. ->method('read')
  26. ->will($this->returnValue(array('packages' => array())));
  27. $this->assertTrue($locker->isLocked());
  28. }
  29. public function testGetNotLockedPackages()
  30. {
  31. $json = $this->createJsonFileMock();
  32. $repo = $this->createRepositoryManagerMock();
  33. $inst = $this->createInstallationManagerMock();
  34. $locker = new Locker($json, $repo, $inst, 'md5');
  35. $json
  36. ->expects($this->once())
  37. ->method('exists')
  38. ->will($this->returnValue(false));
  39. $this->setExpectedException('LogicException');
  40. $locker->getLockedPackages();
  41. }
  42. public function testGetLockedPackages()
  43. {
  44. $json = $this->createJsonFileMock();
  45. $repo = $this->createRepositoryManagerMock();
  46. $inst = $this->createInstallationManagerMock();
  47. $locker = new Locker($json, $repo, $inst, 'md5');
  48. $json
  49. ->expects($this->once())
  50. ->method('exists')
  51. ->will($this->returnValue(true));
  52. $json
  53. ->expects($this->once())
  54. ->method('read')
  55. ->will($this->returnValue(array(
  56. 'packages' => array(
  57. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  58. array('package' => 'pkg2', 'version' => '0.1.10')
  59. )
  60. )));
  61. $package1 = $this->createPackageMock();
  62. $package2 = $this->createPackageMock();
  63. $repo->getLocalRepository()
  64. ->expects($this->exactly(2))
  65. ->method('findPackage')
  66. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  67. ->will($this->onConsecutiveCalls($package1, $package2));
  68. $this->assertEquals(array($package1, $package2), $locker->getLockedPackages());
  69. }
  70. public function testGetPackagesWithoutRepo()
  71. {
  72. $json = $this->createJsonFileMock();
  73. $repo = $this->createRepositoryManagerMock();
  74. $inst = $this->createInstallationManagerMock();
  75. $locker = new Locker($json, $repo, $inst, 'md5');
  76. $json
  77. ->expects($this->once())
  78. ->method('exists')
  79. ->will($this->returnValue(true));
  80. $json
  81. ->expects($this->once())
  82. ->method('read')
  83. ->will($this->returnValue(array(
  84. 'packages' => array(
  85. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  86. array('package' => 'pkg2', 'version' => '0.1.10')
  87. )
  88. )));
  89. $package1 = $this->createPackageMock();
  90. $package2 = $this->createPackageMock();
  91. $repo->getLocalRepository()
  92. ->expects($this->exactly(2))
  93. ->method('findPackage')
  94. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  95. ->will($this->onConsecutiveCalls($package1, null));
  96. $this->setExpectedException('LogicException');
  97. $locker->getLockedPackages();
  98. }
  99. public function testSetLockData()
  100. {
  101. $json = $this->createJsonFileMock();
  102. $repo = $this->createRepositoryManagerMock();
  103. $inst = $this->createInstallationManagerMock();
  104. $locker = new Locker($json, $repo, $inst, 'md5');
  105. $package1 = $this->createPackageMock();
  106. $package2 = $this->createPackageMock();
  107. $package1
  108. ->expects($this->once())
  109. ->method('getPrettyName')
  110. ->will($this->returnValue('pkg1'));
  111. $package1
  112. ->expects($this->once())
  113. ->method('getPrettyVersion')
  114. ->will($this->returnValue('1.0.0-beta'));
  115. $package2
  116. ->expects($this->once())
  117. ->method('getPrettyName')
  118. ->will($this->returnValue('pkg2'));
  119. $package2
  120. ->expects($this->once())
  121. ->method('getPrettyVersion')
  122. ->will($this->returnValue('0.1.10'));
  123. $json
  124. ->expects($this->once())
  125. ->method('write')
  126. ->with(array(
  127. 'hash' => 'md5',
  128. 'packages' => array(
  129. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  130. array('package' => 'pkg2', 'version' => '0.1.10')
  131. ),
  132. 'packages-dev' => array(),
  133. 'aliases' => array(),
  134. 'minimum-stability' => 'dev',
  135. 'stability-flags' => array(),
  136. ));
  137. $locker->setLockData(array($package1, $package2), array(), array(), 'dev', array());
  138. }
  139. public function testLockBadPackages()
  140. {
  141. $json = $this->createJsonFileMock();
  142. $repo = $this->createRepositoryManagerMock();
  143. $inst = $this->createInstallationManagerMock();
  144. $locker = new Locker($json, $repo, $inst, 'md5');
  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(), 'dev', array());
  152. }
  153. public function testIsFresh()
  154. {
  155. $json = $this->createJsonFileMock();
  156. $repo = $this->createRepositoryManagerMock();
  157. $inst = $this->createInstallationManagerMock();
  158. $locker = new Locker($json, $repo, $inst, 'md5');
  159. $json
  160. ->expects($this->once())
  161. ->method('read')
  162. ->will($this->returnValue(array('hash' => 'md5')));
  163. $this->assertTrue($locker->isFresh());
  164. }
  165. public function testIsFreshFalse()
  166. {
  167. $json = $this->createJsonFileMock();
  168. $repo = $this->createRepositoryManagerMock();
  169. $inst = $this->createInstallationManagerMock();
  170. $locker = new Locker($json, $repo, $inst, 'md5');
  171. $json
  172. ->expects($this->once())
  173. ->method('read')
  174. ->will($this->returnValue(array('hash' => 'oldmd5')));
  175. $this->assertFalse($locker->isFresh());
  176. }
  177. private function createJsonFileMock()
  178. {
  179. return $this->getMockBuilder('Composer\Json\JsonFile')
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. }
  183. private function createRepositoryManagerMock()
  184. {
  185. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $mock->expects($this->any())
  189. ->method('getLocalRepository')
  190. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  191. return $mock;
  192. }
  193. private function createInstallationManagerMock()
  194. {
  195. $mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. return $mock;
  199. }
  200. private function createPackageMock()
  201. {
  202. return $this->getMockBuilder('Composer\Package\PackageInterface')
  203. ->getMock();
  204. }
  205. }