FossilDownloaderTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\FossilDownloader;
  13. use Composer\TestCase;
  14. use Composer\Util\Filesystem;
  15. use Composer\Util\Platform;
  16. class FossilDownloaderTest 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->getMock('Composer\IO\IOInterface');
  34. $config = $config ?: $this->getMock('Composer\Config');
  35. $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
  36. $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
  37. return new FossilDownloader($io, $config, $executor, $filesystem);
  38. }
  39. /**
  40. * @expectedException \InvalidArgumentException
  41. */
  42. public function testDownloadForPackageWithoutSourceReference()
  43. {
  44. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  45. $packageMock->expects($this->once())
  46. ->method('getSourceReference')
  47. ->will($this->returnValue(null));
  48. $downloader = $this->getDownloaderMock();
  49. $downloader->download($packageMock, '/path');
  50. }
  51. public function testDownload()
  52. {
  53. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  54. $packageMock->expects($this->any())
  55. ->method('getSourceReference')
  56. ->will($this->returnValue('trunk'));
  57. $packageMock->expects($this->once())
  58. ->method('getSourceUrls')
  59. ->will($this->returnValue(array('http://fossil.kd2.org/kd2fw/')));
  60. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  61. $expectedFossilCommand = $this->getCmd('fossil clone \'http://fossil.kd2.org/kd2fw/\' \'repo.fossil\'');
  62. $processExecutor->expects($this->at(0))
  63. ->method('execute')
  64. ->with($this->equalTo($expectedFossilCommand))
  65. ->will($this->returnValue(0));
  66. $expectedFossilCommand = $this->getCmd('fossil open \'repo.fossil\'');
  67. $processExecutor->expects($this->at(1))
  68. ->method('execute')
  69. ->with($this->equalTo($expectedFossilCommand))
  70. ->will($this->returnValue(0));
  71. $expectedFossilCommand = $this->getCmd('fossil update \'trunk\'');
  72. $processExecutor->expects($this->at(2))
  73. ->method('execute')
  74. ->with($this->equalTo($expectedFossilCommand))
  75. ->will($this->returnValue(0));
  76. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  77. $downloader->download($packageMock, 'repo');
  78. }
  79. /**
  80. * @expectedException \InvalidArgumentException
  81. */
  82. public function testUpdateforPackageWithoutSourceReference()
  83. {
  84. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  85. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  86. $sourcePackageMock->expects($this->once())
  87. ->method('getSourceReference')
  88. ->will($this->returnValue(null));
  89. $downloader = $this->getDownloaderMock();
  90. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  91. }
  92. public function testUpdate()
  93. {
  94. // Ensure file exists
  95. $file = $this->workingDir . '/.fslckout';
  96. if (!file_exists($file)) {
  97. touch($file);
  98. }
  99. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  100. $packageMock->expects($this->any())
  101. ->method('getSourceReference')
  102. ->will($this->returnValue('trunk'));
  103. $packageMock->expects($this->any())
  104. ->method('getSourceUrls')
  105. ->will($this->returnValue(array('http://fossil.kd2.org/kd2fw/')));
  106. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  107. $expectedFossilCommand = $this->getCmd("fossil changes");
  108. $processExecutor->expects($this->at(0))
  109. ->method('execute')
  110. ->with($this->equalTo($expectedFossilCommand))
  111. ->will($this->returnValue(0));
  112. $expectedFossilCommand = $this->getCmd("fossil pull && fossil up 'trunk'");
  113. $processExecutor->expects($this->at(1))
  114. ->method('execute')
  115. ->with($this->equalTo($expectedFossilCommand))
  116. ->will($this->returnValue(0));
  117. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  118. $downloader->update($packageMock, $packageMock, $this->workingDir);
  119. }
  120. public function testRemove()
  121. {
  122. $expectedResetCommand = $this->getCmd('cd \'composerPath\' && fossil status');
  123. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  124. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  125. $processExecutor->expects($this->any())
  126. ->method('execute')
  127. ->with($this->equalTo($expectedResetCommand));
  128. $filesystem = $this->getMock('Composer\Util\Filesystem');
  129. $filesystem->expects($this->any())
  130. ->method('removeDirectory')
  131. ->with($this->equalTo('composerPath'))
  132. ->will($this->returnValue(true));
  133. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  134. $downloader->remove($packageMock, 'composerPath');
  135. }
  136. public function testGetInstallationSource()
  137. {
  138. $downloader = $this->getDownloaderMock(null);
  139. $this->assertEquals('source', $downloader->getInstallationSource());
  140. }
  141. private function getCmd($cmd)
  142. {
  143. return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd;
  144. }
  145. }