HgDownloaderTest.php 5.5 KB

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