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

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

https://github.com/xuguangtech/composer
PHP | 225 lines | 174 code | 42 blank | 9 comment | 0 complexity | a71daff922fd409c726323e9517ede75 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(), 'md5');
  20. $json
  21. ->expects($this->any())
  22. ->method('exists')
  23. ->will($this->returnValue(true));
  24. $json
  25. ->expects($this->any())
  26. ->method('read')
  27. ->will($this->returnValue(array('packages' => array())));
  28. $this->assertTrue($locker->isLocked());
  29. }
  30. public function testGetNotLockedPackages()
  31. {
  32. $json = $this->createJsonFileMock();
  33. $repo = $this->createRepositoryManagerMock();
  34. $inst = $this->createInstallationManagerMock();
  35. $locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
  36. $json
  37. ->expects($this->once())
  38. ->method('exists')
  39. ->will($this->returnValue(false));
  40. $this->setExpectedException('LogicException');
  41. $locker->getLockedRepository();
  42. }
  43. public function testGetLockedPackages()
  44. {
  45. $json = $this->createJsonFileMock();
  46. $repo = $this->createRepositoryManagerMock();
  47. $inst = $this->createInstallationManagerMock();
  48. $locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
  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. $repo = $this->createRepositoryManagerMock();
  70. $inst = $this->createInstallationManagerMock();
  71. $locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
  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. $json
  99. ->expects($this->once())
  100. ->method('write')
  101. ->with(array(
  102. '_readme' => array('This file locks the dependencies of your project to a known state', 'Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file'),
  103. 'hash' => 'md5',
  104. 'packages' => array(
  105. array('name' => 'pkg1', 'version' => '1.0.0-beta'),
  106. array('name' => 'pkg2', 'version' => '0.1.10')
  107. ),
  108. 'packages-dev' => array(),
  109. 'aliases' => array(),
  110. 'minimum-stability' => 'dev',
  111. 'stability-flags' => array(),
  112. 'platform' => array(),
  113. 'platform-dev' => array(),
  114. ));
  115. $locker->setLockData(array($package1, $package2), array(), array(), array(), array(), 'dev', array());
  116. }
  117. public function testLockBadPackages()
  118. {
  119. $json = $this->createJsonFileMock();
  120. $repo = $this->createRepositoryManagerMock();
  121. $inst = $this->createInstallationManagerMock();
  122. $locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
  123. $package1 = $this->createPackageMock();
  124. $package1
  125. ->expects($this->once())
  126. ->method('getPrettyName')
  127. ->will($this->returnValue('pkg1'));
  128. $this->setExpectedException('LogicException');
  129. $locker->setLockData(array($package1), array(), array(), array(), array(), 'dev', array());
  130. }
  131. public function testIsFresh()
  132. {
  133. $json = $this->createJsonFileMock();
  134. $repo = $this->createRepositoryManagerMock();
  135. $inst = $this->createInstallationManagerMock();
  136. $locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
  137. $json
  138. ->expects($this->once())
  139. ->method('read')
  140. ->will($this->returnValue(array('hash' => 'md5')));
  141. $this->assertTrue($locker->isFresh());
  142. }
  143. public function testIsFreshFalse()
  144. {
  145. $json = $this->createJsonFileMock();
  146. $repo = $this->createRepositoryManagerMock();
  147. $inst = $this->createInstallationManagerMock();
  148. $locker = new Locker(new NullIO, $json, $repo, $inst, 'md5');
  149. $json
  150. ->expects($this->once())
  151. ->method('read')
  152. ->will($this->returnValue(array('hash' => 'oldmd5')));
  153. $this->assertFalse($locker->isFresh());
  154. }
  155. private function createJsonFileMock()
  156. {
  157. return $this->getMockBuilder('Composer\Json\JsonFile')
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. }
  161. private function createRepositoryManagerMock()
  162. {
  163. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $mock->expects($this->any())
  167. ->method('getLocalRepository')
  168. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  169. return $mock;
  170. }
  171. private function createInstallationManagerMock()
  172. {
  173. $mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. return $mock;
  177. }
  178. private function createPackageMock()
  179. {
  180. return $this->getMockBuilder('Composer\Package\PackageInterface')
  181. ->getMock();
  182. }
  183. }