HgDownloaderTest.php 4.9 KB

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