GitDownloaderTest.php 5.0 KB

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