HgDownloaderTest.php 5.9 KB

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