LockerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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->getLockedRepository();
  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('name' => 'pkg1', 'version' => '1.0.0-beta'),
  58. array('name' => 'pkg2', 'version' => '0.1.10')
  59. )
  60. )));
  61. $repo = $locker->getLockedRepository();
  62. $this->assertNotNull($repo->findPackage('pkg1', '1.0.0-beta'));
  63. $this->assertNotNull($repo->findPackage('pkg2', '0.1.10'));
  64. }
  65. public function testSetLockData()
  66. {
  67. $json = $this->createJsonFileMock();
  68. $repo = $this->createRepositoryManagerMock();
  69. $inst = $this->createInstallationManagerMock();
  70. $locker = new Locker($json, $repo, $inst, 'md5');
  71. $package1 = $this->createPackageMock();
  72. $package2 = $this->createPackageMock();
  73. $package1
  74. ->expects($this->atLeastOnce())
  75. ->method('getPrettyName')
  76. ->will($this->returnValue('pkg1'));
  77. $package1
  78. ->expects($this->atLeastOnce())
  79. ->method('getPrettyVersion')
  80. ->will($this->returnValue('1.0.0-beta'));
  81. $package1
  82. ->expects($this->atLeastOnce())
  83. ->method('getVersion')
  84. ->will($this->returnValue('1.0.0.0-beta'));
  85. $package2
  86. ->expects($this->atLeastOnce())
  87. ->method('getPrettyName')
  88. ->will($this->returnValue('pkg2'));
  89. $package2
  90. ->expects($this->atLeastOnce())
  91. ->method('getPrettyVersion')
  92. ->will($this->returnValue('0.1.10'));
  93. $package2
  94. ->expects($this->atLeastOnce())
  95. ->method('getVersion')
  96. ->will($this->returnValue('0.1.10.0'));
  97. $json
  98. ->expects($this->once())
  99. ->method('write')
  100. ->with(array(
  101. '_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'),
  102. 'hash' => 'md5',
  103. 'packages' => array(
  104. array('name' => 'pkg1', 'version' => '1.0.0-beta'),
  105. array('name' => 'pkg2', 'version' => '0.1.10')
  106. ),
  107. 'packages-dev' => array(),
  108. 'aliases' => array(),
  109. 'minimum-stability' => 'dev',
  110. 'stability-flags' => array(),
  111. 'platform' => array(),
  112. 'platform-dev' => array(),
  113. ));
  114. $locker->setLockData(array($package1, $package2), array(), array(), array(), array(), 'dev', array());
  115. }
  116. public function testLockBadPackages()
  117. {
  118. $json = $this->createJsonFileMock();
  119. $repo = $this->createRepositoryManagerMock();
  120. $inst = $this->createInstallationManagerMock();
  121. $locker = new Locker($json, $repo, $inst, 'md5');
  122. $package1 = $this->createPackageMock();
  123. $package1
  124. ->expects($this->once())
  125. ->method('getPrettyName')
  126. ->will($this->returnValue('pkg1'));
  127. $this->setExpectedException('LogicException');
  128. $locker->setLockData(array($package1), array(), array(), array(), array(), 'dev', array());
  129. }
  130. public function testIsFresh()
  131. {
  132. $json = $this->createJsonFileMock();
  133. $repo = $this->createRepositoryManagerMock();
  134. $inst = $this->createInstallationManagerMock();
  135. $locker = new Locker($json, $repo, $inst, 'md5');
  136. $json
  137. ->expects($this->once())
  138. ->method('read')
  139. ->will($this->returnValue(array('hash' => 'md5')));
  140. $this->assertTrue($locker->isFresh());
  141. }
  142. public function testIsFreshFalse()
  143. {
  144. $json = $this->createJsonFileMock();
  145. $repo = $this->createRepositoryManagerMock();
  146. $inst = $this->createInstallationManagerMock();
  147. $locker = new Locker($json, $repo, $inst, 'md5');
  148. $json
  149. ->expects($this->once())
  150. ->method('read')
  151. ->will($this->returnValue(array('hash' => 'oldmd5')));
  152. $this->assertFalse($locker->isFresh());
  153. }
  154. private function createJsonFileMock()
  155. {
  156. return $this->getMockBuilder('Composer\Json\JsonFile')
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. }
  160. private function createRepositoryManagerMock()
  161. {
  162. $mock = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  163. ->disableOriginalConstructor()
  164. ->getMock();
  165. $mock->expects($this->any())
  166. ->method('getLocalRepository')
  167. ->will($this->returnValue($this->getMockBuilder('Composer\Repository\ArrayRepository')->getMock()));
  168. return $mock;
  169. }
  170. private function createInstallationManagerMock()
  171. {
  172. $mock = $this->getMockBuilder('Composer\Installer\InstallationManager')
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. return $mock;
  176. }
  177. private function createPackageMock()
  178. {
  179. return $this->getMockBuilder('Composer\Package\PackageInterface')
  180. ->getMock();
  181. }
  182. }