GitDownloaderTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
  42. $downloader->download($packageMock, 'composerPath');
  43. }
  44. /**
  45. * @expectedException \InvalidArgumentException
  46. */
  47. public function testUpdateforPackageWithoutSourceReference()
  48. {
  49. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  50. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  51. $sourcePackageMock->expects($this->once())
  52. ->method('getSourceReference')
  53. ->will($this->returnValue(null));
  54. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'));
  55. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  56. }
  57. public function testUpdate()
  58. {
  59. $expectedGitUpdateCommand = $this->getCmd('cd \'composerPath\' && git fetch && git checkout \'ref\' && git reset --hard \'ref\'');
  60. $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain');
  61. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  62. $packageMock->expects($this->any())
  63. ->method('getSourceReference')
  64. ->will($this->returnValue('ref'));
  65. $packageMock->expects($this->any())
  66. ->method('getSourceUrl')
  67. ->will($this->returnValue('https://github.com/l3l0/composer'));
  68. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  69. $processExecutor->expects($this->at(0))
  70. ->method('execute')
  71. ->with($this->equalTo($expectedGitResetCommand));
  72. $processExecutor->expects($this->at(1))
  73. ->method('execute')
  74. ->with($this->equalTo($expectedGitUpdateCommand));
  75. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
  76. $downloader->update($packageMock, $packageMock, 'composerPath');
  77. }
  78. public function testRemove()
  79. {
  80. $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain');
  81. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  82. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  83. $processExecutor->expects($this->any())
  84. ->method('execute')
  85. ->with($this->equalTo($expectedGitResetCommand));
  86. $filesystem = $this->getMock('Composer\Util\Filesystem');
  87. $filesystem->expects($this->any())
  88. ->method('removeDirectory')
  89. ->with($this->equalTo('composerPath'));
  90. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor, $filesystem);
  91. $downloader->remove($packageMock, 'composerPath');
  92. }
  93. public function testGetInstallationSource()
  94. {
  95. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'));
  96. $this->assertEquals('source', $downloader->getInstallationSource());
  97. }
  98. private function getCmd($cmd)
  99. {
  100. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  101. return strtr($cmd, "'", '"');
  102. }
  103. return $cmd;
  104. }
  105. }