HgDownloaderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Downloader;
  12. use Composer\Downloader\HgDownloader;
  13. use Composer\TestCase;
  14. use Composer\Util\Filesystem;
  15. class HgDownloaderTest extends TestCase
  16. {
  17. /** @var string */
  18. private $workingDir;
  19. protected function setUp()
  20. {
  21. $this->workingDir = $this->getUniqueTmpDirectory();
  22. }
  23. protected function tearDown()
  24. {
  25. if (is_dir($this->workingDir)) {
  26. $fs = new Filesystem;
  27. $fs->removeDirectory($this->workingDir);
  28. }
  29. }
  30. protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
  31. {
  32. $io = $io ?: $this->getMock('Composer\IO\IOInterface');
  33. $config = $config ?: $this->getMock('Composer\Config');
  34. $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
  35. $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
  36. return new HgDownloader($io, $config, $executor, $filesystem);
  37. }
  38. /**
  39. * @expectedException \InvalidArgumentException
  40. */
  41. public function testDownloadForPackageWithoutSourceReference()
  42. {
  43. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  44. $packageMock->expects($this->once())
  45. ->method('getSourceReference')
  46. ->will($this->returnValue(null));
  47. $downloader = $this->getDownloaderMock();
  48. $downloader->download($packageMock, '/path');
  49. }
  50. public function testDownload()
  51. {
  52. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  53. $packageMock->expects($this->any())
  54. ->method('getSourceReference')
  55. ->will($this->returnValue('ref'));
  56. $packageMock->expects($this->once())
  57. ->method('getSourceUrls')
  58. ->will($this->returnValue(array('https://mercurial.dev/l3l0/composer')));
  59. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  60. $expectedGitCommand = $this->getCmd('hg clone \'https://mercurial.dev/l3l0/composer\' \'composerPath\'');
  61. $processExecutor->expects($this->at(0))
  62. ->method('execute')
  63. ->with($this->equalTo($expectedGitCommand))
  64. ->will($this->returnValue(0));
  65. $expectedGitCommand = $this->getCmd('hg up \'ref\'');
  66. $processExecutor->expects($this->at(1))
  67. ->method('execute')
  68. ->with($this->equalTo($expectedGitCommand))
  69. ->will($this->returnValue(0));
  70. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  71. $downloader->download($packageMock, 'composerPath');
  72. }
  73. /**
  74. * @expectedException \InvalidArgumentException
  75. */
  76. public function testUpdateforPackageWithoutSourceReference()
  77. {
  78. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  79. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  80. $sourcePackageMock->expects($this->once())
  81. ->method('getSourceReference')
  82. ->will($this->returnValue(null));
  83. $downloader = $this->getDownloaderMock();
  84. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  85. }
  86. public function testUpdate()
  87. {
  88. $fs = new Filesystem;
  89. $fs->ensureDirectoryExists($this->workingDir.'/.hg');
  90. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  91. $packageMock->expects($this->any())
  92. ->method('getSourceReference')
  93. ->will($this->returnValue('ref'));
  94. $packageMock->expects($this->any())
  95. ->method('getSourceUrls')
  96. ->will($this->returnValue(array('https://github.com/l3l0/composer')));
  97. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  98. $expectedHgCommand = $this->getCmd("hg st");
  99. $processExecutor->expects($this->at(0))
  100. ->method('execute')
  101. ->with($this->equalTo($expectedHgCommand))
  102. ->will($this->returnValue(0));
  103. $expectedHgCommand = $this->getCmd("hg pull 'https://github.com/l3l0/composer' && hg up 'ref'");
  104. $processExecutor->expects($this->at(1))
  105. ->method('execute')
  106. ->with($this->equalTo($expectedHgCommand))
  107. ->will($this->returnValue(0));
  108. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  109. $downloader->update($packageMock, $packageMock, $this->workingDir);
  110. }
  111. public function testRemove()
  112. {
  113. $expectedResetCommand = $this->getCmd('cd \'composerPath\' && hg st');
  114. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  115. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  116. $processExecutor->expects($this->any())
  117. ->method('execute')
  118. ->with($this->equalTo($expectedResetCommand));
  119. $filesystem = $this->getMock('Composer\Util\Filesystem');
  120. $filesystem->expects($this->any())
  121. ->method('removeDirectory')
  122. ->with($this->equalTo('composerPath'))
  123. ->will($this->returnValue(true));
  124. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  125. $downloader->remove($packageMock, 'composerPath');
  126. }
  127. public function testGetInstallationSource()
  128. {
  129. $downloader = $this->getDownloaderMock(null);
  130. $this->assertEquals('source', $downloader->getInstallationSource());
  131. }
  132. private function getCmd($cmd)
  133. {
  134. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  135. return strtr($cmd, "'", '"');
  136. }
  137. return $cmd;
  138. }
  139. }