PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Composer/Test/Downloader/FossilDownloaderTest.php

http://github.com/composer/composer
PHP | 180 lines | 135 code | 28 blank | 17 comment | 3 complexity | 17eff285b21caaf8be2976066a9d71c7 MD5 | raw file
  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\Test\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->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 FossilDownloader($io, $config, $executor, $filesystem);
  38. }
  39. /**
  40. * @expectedException \InvalidArgumentException
  41. */
  42. public function testInstallForPackageWithoutSourceReference()
  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 testInstall()
  52. {
  53. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  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->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  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\' --nested');
  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->install($packageMock, 'repo');
  78. }
  79. /**
  80. * @expectedException \InvalidArgumentException
  81. */
  82. public function testUpdateforPackageWithoutSourceReference()
  83. {
  84. $initialPackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  85. $sourcePackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  86. $sourcePackageMock->expects($this->once())
  87. ->method('getSourceReference')
  88. ->will($this->returnValue(null));
  89. $downloader = $this->getDownloaderMock();
  90. $downloader->prepare('update', $sourcePackageMock, '/path', $initialPackageMock);
  91. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  92. $downloader->cleanup('update', $sourcePackageMock, '/path', $initialPackageMock);
  93. }
  94. public function testUpdate()
  95. {
  96. // Ensure file exists
  97. $file = $this->workingDir . '/.fslckout';
  98. if (!file_exists($file)) {
  99. touch($file);
  100. }
  101. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  102. $packageMock->expects($this->any())
  103. ->method('getSourceReference')
  104. ->will($this->returnValue('trunk'));
  105. $packageMock->expects($this->any())
  106. ->method('getSourceUrls')
  107. ->will($this->returnValue(array('http://fossil.kd2.org/kd2fw/')));
  108. $packageMock->expects($this->any())
  109. ->method('getVersion')
  110. ->will($this->returnValue('1.0.0.0'));
  111. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  112. $expectedFossilCommand = $this->getCmd("fossil changes");
  113. $processExecutor->expects($this->at(0))
  114. ->method('execute')
  115. ->with($this->equalTo($expectedFossilCommand))
  116. ->will($this->returnValue(0));
  117. $expectedFossilCommand = $this->getCmd("fossil pull && fossil up 'trunk'");
  118. $processExecutor->expects($this->at(1))
  119. ->method('execute')
  120. ->with($this->equalTo($expectedFossilCommand))
  121. ->will($this->returnValue(0));
  122. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  123. $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
  124. $downloader->update($packageMock, $packageMock, $this->workingDir);
  125. $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
  126. }
  127. public function testRemove()
  128. {
  129. $expectedResetCommand = $this->getCmd('cd \'composerPath\' && fossil status');
  130. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  131. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  132. $processExecutor->expects($this->any())
  133. ->method('execute')
  134. ->with($this->equalTo($expectedResetCommand));
  135. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  136. $filesystem->expects($this->once())
  137. ->method('removeDirectory')
  138. ->with($this->equalTo('composerPath'))
  139. ->will($this->returnValue(true));
  140. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  141. $downloader->remove($packageMock, 'composerPath');
  142. }
  143. public function testGetInstallationSource()
  144. {
  145. $downloader = $this->getDownloaderMock(null);
  146. $this->assertEquals('source', $downloader->getInstallationSource());
  147. }
  148. private function getCmd($cmd)
  149. {
  150. return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd;
  151. }
  152. }