HgDownloaderTest.php 5.0 KB

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