HgDownloaderTest.php 5.8 KB

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