HgDownloaderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Test\TestCase;
  14. use Composer\Util\Filesystem;
  15. use Composer\Util\Platform;
  16. class HgDownloaderTest extends TestCase
  17. {
  18. /** @var string */
  19. private $workingDir;
  20. protected function setUp()
  21. {
  22. $this->workingDir = $this->getUniqueTmpDirectory();
  23. }
  24. protected function tearDown()
  25. {
  26. if (is_dir($this->workingDir)) {
  27. $fs = new Filesystem;
  28. $fs->removeDirectory($this->workingDir);
  29. }
  30. }
  31. protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
  32. {
  33. $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  34. $config = $config ?: $this->getMockBuilder('Composer\Config')->getMock();
  35. $executor = $executor ?: $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  36. $filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  37. return new HgDownloader($io, $config, $executor, $filesystem);
  38. }
  39. /**
  40. * @expectedException \InvalidArgumentException
  41. */
  42. public function testDownloadForPackageWithoutSourceReference()
  43. {
  44. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  45. $packageMock->expects($this->once())
  46. ->method('getSourceReference')
  47. ->will($this->returnValue(null));
  48. $downloader = $this->getDownloaderMock();
  49. $downloader->install($packageMock, '/path');
  50. }
  51. public function testDownload()
  52. {
  53. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  54. $packageMock->expects($this->any())
  55. ->method('getSourceReference')
  56. ->will($this->returnValue('ref'));
  57. $packageMock->expects($this->once())
  58. ->method('getSourceUrls')
  59. ->will($this->returnValue(array('https://mercurial.dev/l3l0/composer')));
  60. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  61. $expectedGitCommand = $this->getCmd('hg clone \'https://mercurial.dev/l3l0/composer\' \'composerPath\'');
  62. $processExecutor->expects($this->at(0))
  63. ->method('execute')
  64. ->with($this->equalTo($expectedGitCommand))
  65. ->will($this->returnValue(0));
  66. $expectedGitCommand = $this->getCmd('hg up \'ref\'');
  67. $processExecutor->expects($this->at(1))
  68. ->method('execute')
  69. ->with($this->equalTo($expectedGitCommand))
  70. ->will($this->returnValue(0));
  71. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  72. $downloader->install($packageMock, 'composerPath');
  73. }
  74. /**
  75. * @expectedException \InvalidArgumentException
  76. */
  77. public function testUpdateforPackageWithoutSourceReference()
  78. {
  79. $initialPackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  80. $sourcePackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  81. $sourcePackageMock->expects($this->once())
  82. ->method('getSourceReference')
  83. ->will($this->returnValue(null));
  84. $downloader = $this->getDownloaderMock();
  85. $downloader->prepare('update', $sourcePackageMock, '/path', $initialPackageMock);
  86. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  87. $downloader->cleanup('update', $sourcePackageMock, '/path', $initialPackageMock);
  88. }
  89. public function testUpdate()
  90. {
  91. $fs = new Filesystem;
  92. $fs->ensureDirectoryExists($this->workingDir.'/.hg');
  93. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  94. $packageMock->expects($this->any())
  95. ->method('getSourceReference')
  96. ->will($this->returnValue('ref'));
  97. $packageMock->expects($this->any())
  98. ->method('getVersion')
  99. ->will($this->returnValue('1.0.0.0'));
  100. $packageMock->expects($this->any())
  101. ->method('getSourceUrls')
  102. ->will($this->returnValue(array('https://github.com/l3l0/composer')));
  103. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  104. $expectedHgCommand = $this->getCmd("hg st");
  105. $processExecutor->expects($this->at(0))
  106. ->method('execute')
  107. ->with($this->equalTo($expectedHgCommand))
  108. ->will($this->returnValue(0));
  109. $expectedHgCommand = $this->getCmd("hg pull 'https://github.com/l3l0/composer' && hg up 'ref'");
  110. $processExecutor->expects($this->at(1))
  111. ->method('execute')
  112. ->with($this->equalTo($expectedHgCommand))
  113. ->will($this->returnValue(0));
  114. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  115. $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
  116. $downloader->update($packageMock, $packageMock, $this->workingDir);
  117. $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
  118. }
  119. public function testRemove()
  120. {
  121. $expectedResetCommand = $this->getCmd('cd \'composerPath\' && hg st');
  122. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  123. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  124. $processExecutor->expects($this->any())
  125. ->method('execute')
  126. ->with($this->equalTo($expectedResetCommand));
  127. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  128. $filesystem->expects($this->once())
  129. ->method('removeDirectory')
  130. ->with($this->equalTo('composerPath'))
  131. ->will($this->returnValue(true));
  132. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  133. $downloader->prepare('uninstall', $packageMock, 'composerPath');
  134. $downloader->remove($packageMock, 'composerPath');
  135. $downloader->cleanup('uninstall', $packageMock, 'composerPath');
  136. }
  137. public function testGetInstallationSource()
  138. {
  139. $downloader = $this->getDownloaderMock(null);
  140. $this->assertEquals('source', $downloader->getInstallationSource());
  141. }
  142. private function getCmd($cmd)
  143. {
  144. return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd;
  145. }
  146. }