HgDownloaderTest.php 5.1 KB

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