LockerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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());
  19. $json
  20. ->expects($this->once())
  21. ->method('exists')
  22. ->will($this->returnValue(true));
  23. $this->assertTrue($locker->isLocked());
  24. }
  25. public function testGetNotLockedPackages()
  26. {
  27. $json = $this->createJsonFileMock();
  28. $repo = $this->createRepositoryManagerMock();
  29. $locker = new Locker($json, $repo);
  30. $json
  31. ->expects($this->once())
  32. ->method('exists')
  33. ->will($this->returnValue(false));
  34. $this->setExpectedException('LogicException');
  35. $locker->getLockedPackages();
  36. }
  37. public function testGetLockedPackages()
  38. {
  39. $json = $this->createJsonFileMock();
  40. $repo = $this->createRepositoryManagerMock();
  41. $locker = new Locker($json, $repo);
  42. $json
  43. ->expects($this->once())
  44. ->method('exists')
  45. ->will($this->returnValue(true));
  46. $json
  47. ->expects($this->once())
  48. ->method('read')
  49. ->will($this->returnValue(array(
  50. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  51. array('package' => 'pkg2', 'version' => '0.1.10')
  52. )));
  53. $package1 = $this->createPackageMock();
  54. $package2 = $this->createPackageMock();
  55. $repo
  56. ->expects($this->exactly(2))
  57. ->method('findPackage')
  58. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  59. ->will($this->onConsecutiveCalls($package1, $package2));
  60. $this->assertEquals(array($package1, $package2), $locker->getLockedPackages());
  61. }
  62. public function testGetPackagesWithoutRepo()
  63. {
  64. $json = $this->createJsonFileMock();
  65. $repo = $this->createRepositoryManagerMock();
  66. $locker = new Locker($json, $repo);
  67. $json
  68. ->expects($this->once())
  69. ->method('exists')
  70. ->will($this->returnValue(true));
  71. $json
  72. ->expects($this->once())
  73. ->method('read')
  74. ->will($this->returnValue(array(
  75. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  76. array('package' => 'pkg2', 'version' => '0.1.10')
  77. )));
  78. $package1 = $this->createPackageMock();
  79. $package2 = $this->createPackageMock();
  80. $repo
  81. ->expects($this->exactly(2))
  82. ->method('findPackage')
  83. ->with($this->logicalOr('pkg1', 'pkg2'), $this->logicalOr('1.0.0-beta', '0.1.10'))
  84. ->will($this->onConsecutiveCalls($package1, null));
  85. $this->setExpectedException('LogicException');
  86. $locker->getLockedPackages();
  87. }
  88. public function testLockPackages()
  89. {
  90. $json = $this->createJsonFileMock();
  91. $repo = $this->createRepositoryManagerMock();
  92. $locker = new Locker($json, $repo);
  93. $package1 = $this->createPackageMock();
  94. $package2 = $this->createPackageMock();
  95. $package1
  96. ->expects($this->once())
  97. ->method('getName')
  98. ->will($this->returnValue('pkg1'));
  99. $package1
  100. ->expects($this->once())
  101. ->method('getVersion')
  102. ->will($this->returnValue('1.0.0-beta'));
  103. $package2
  104. ->expects($this->once())
  105. ->method('getName')
  106. ->will($this->returnValue('pkg2'));
  107. $package2
  108. ->expects($this->once())
  109. ->method('getVersion')
  110. ->will($this->returnValue('0.1.10'));
  111. $json
  112. ->expects($this->once())
  113. ->method('write')
  114. ->with(array(
  115. array('package' => 'pkg1', 'version' => '1.0.0-beta'),
  116. array('package' => 'pkg2', 'version' => '0.1.10')
  117. ));
  118. $locker->lockPackages(array($package1, $package2));
  119. }
  120. public function testLockBadPackages()
  121. {
  122. $json = $this->createJsonFileMock();
  123. $repo = $this->createRepositoryManagerMock();
  124. $locker = new Locker($json, $repo);
  125. $package1 = $this->createPackageMock();
  126. $package1
  127. ->expects($this->once())
  128. ->method('getName')
  129. ->will($this->returnValue('pkg1'));
  130. $this->setExpectedException('LogicException');
  131. $locker->lockPackages(array($package1));
  132. }
  133. private function createJsonFileMock()
  134. {
  135. return $this->getMockBuilder('Composer\Json\JsonFile')
  136. ->disableOriginalConstructor()
  137. ->getMock();
  138. }
  139. private function createRepositoryManagerMock()
  140. {
  141. return $this->getMockBuilder('Composer\Repository\RepositoryManager')
  142. ->disableOriginalConstructor()
  143. ->getMock();
  144. }
  145. private function createPackageMock()
  146. {
  147. return $this->getMockBuilder('Composer\Package\PackageInterface')
  148. ->getMock();
  149. }
  150. }