HgDownloaderTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $expectedGitCommand = $this->getCmd('hg clone \'https://mercurial.dev/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && hg up \'ref\'');
  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('getSourceUrl')
  44. ->will($this->returnValue('https://mercurial.dev/l3l0/composer'));
  45. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  46. $processExecutor->expects($this->once())
  47. ->method('execute')
  48. ->with($this->equalTo($expectedGitCommand))
  49. ->will($this->returnValue(0));
  50. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  51. $downloader->download($packageMock, 'composerPath');
  52. }
  53. /**
  54. * @expectedException \InvalidArgumentException
  55. */
  56. public function testUpdateforPackageWithoutSourceReference()
  57. {
  58. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  59. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  60. $sourcePackageMock->expects($this->once())
  61. ->method('getSourceReference')
  62. ->will($this->returnValue(null));
  63. $downloader = $this->getDownloaderMock();
  64. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  65. }
  66. public function testUpdate()
  67. {
  68. $expectedUpdateCommand = $this->getCmd("cd 'composerPath' && hg pull 'https://github.com/l3l0/composer' && hg up 'ref'");
  69. $expectedResetCommand = $this->getCmd("cd 'composerPath' && hg st");
  70. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  71. $packageMock->expects($this->any())
  72. ->method('getSourceReference')
  73. ->will($this->returnValue('ref'));
  74. $packageMock->expects($this->any())
  75. ->method('getSourceUrl')
  76. ->will($this->returnValue('https://github.com/l3l0/composer'));
  77. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  78. $processExecutor->expects($this->at(0))
  79. ->method('execute')
  80. ->with($this->equalTo($expectedResetCommand));
  81. $processExecutor->expects($this->at(1))
  82. ->method('execute')
  83. ->with($this->equalTo($expectedUpdateCommand))
  84. ->will($this->returnValue(0));
  85. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  86. $downloader->update($packageMock, $packageMock, 'composerPath');
  87. }
  88. public function testRemove()
  89. {
  90. $expectedResetCommand = $this->getCmd('cd \'composerPath\' && hg st');
  91. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  92. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  93. $processExecutor->expects($this->any())
  94. ->method('execute')
  95. ->with($this->equalTo($expectedResetCommand));
  96. $filesystem = $this->getMock('Composer\Util\Filesystem');
  97. $filesystem->expects($this->any())
  98. ->method('removeDirectory')
  99. ->with($this->equalTo('composerPath'))
  100. ->will($this->returnValue(true));
  101. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  102. $downloader->remove($packageMock, 'composerPath');
  103. }
  104. public function testGetInstallationSource()
  105. {
  106. $downloader = $this->getDownloaderMock(null);
  107. $this->assertEquals('source', $downloader->getInstallationSource());
  108. }
  109. private function getCmd($cmd)
  110. {
  111. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  112. return strtr($cmd, "'", '"');
  113. }
  114. return $cmd;
  115. }
  116. }