PerforceDownloaderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\PerforceDownloader;
  13. use Composer\Config;
  14. use Composer\Repository\VcsRepository;
  15. use Composer\IO\IOInterface;
  16. use Composer\Test\TestCase;
  17. use Composer\Util\Filesystem;
  18. /**
  19. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  20. */
  21. class PerforceDownloaderTest extends TestCase
  22. {
  23. protected $config;
  24. /** @var PerforceDownloader */
  25. protected $downloader;
  26. protected $io;
  27. protected $package;
  28. protected $processExecutor;
  29. protected $repoConfig;
  30. protected $repository;
  31. protected $testPath;
  32. protected function setUp()
  33. {
  34. $this->testPath = $this->getUniqueTmpDirectory();
  35. $this->repoConfig = $this->getRepoConfig();
  36. $this->config = $this->getConfig();
  37. $this->io = $this->getMockIoInterface();
  38. $this->processExecutor = $this->getMockProcessExecutor();
  39. $this->repository = $this->getMockRepository($this->repoConfig, $this->io, $this->config);
  40. $this->package = $this->getMockPackageInterface($this->repository);
  41. $this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor);
  42. }
  43. protected function tearDown()
  44. {
  45. $this->downloader = null;
  46. $this->package = null;
  47. $this->repository = null;
  48. $this->io = null;
  49. $this->config = null;
  50. $this->repoConfig = null;
  51. if (is_dir($this->testPath)) {
  52. $fs = new Filesystem;
  53. $fs->removeDirectory($this->testPath);
  54. }
  55. }
  56. protected function getMockProcessExecutor()
  57. {
  58. return $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  59. }
  60. protected function getConfig()
  61. {
  62. $config = new Config();
  63. $settings = array('config' => array('home' => $this->testPath));
  64. $config->merge($settings);
  65. return $config;
  66. }
  67. protected function getMockIoInterface()
  68. {
  69. return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  70. }
  71. protected function getMockPackageInterface(VcsRepository $repository)
  72. {
  73. $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  74. $package->expects($this->any())->method('getRepository')->will($this->returnValue($repository));
  75. return $package;
  76. }
  77. protected function getRepoConfig()
  78. {
  79. return array('url' => 'TEST_URL', 'p4user' => 'TEST_USER');
  80. }
  81. protected function getMockRepository(array $repoConfig, IOInterface $io, Config $config)
  82. {
  83. $repository = $this->getMockBuilder('Composer\Repository\VcsRepository')
  84. ->setMethods(array('getRepoConfig'))
  85. ->setConstructorArgs(array($repoConfig, $io, $config))
  86. ->getMock();
  87. $repository->expects($this->any())->method('getRepoConfig')->will($this->returnValue($repoConfig));
  88. return $repository;
  89. }
  90. /**
  91. * @doesNotPerformAssertions
  92. */
  93. public function testInitPerforceInstantiatesANewPerforceObject()
  94. {
  95. $this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
  96. }
  97. public function testInitPerforceDoesNothingIfPerforceAlreadySet()
  98. {
  99. $perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
  100. $this->downloader->setPerforce($perforce);
  101. $this->repository->expects($this->never())->method('getRepoConfig');
  102. $this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
  103. }
  104. /**
  105. * @depends testInitPerforceInstantiatesANewPerforceObject
  106. * @depends testInitPerforceDoesNothingIfPerforceAlreadySet
  107. */
  108. public function testDoDownloadWithTag()
  109. {
  110. //I really don't like this test but the logic of each Perforce method is tested in the Perforce class. Really I am just enforcing workflow.
  111. $ref = 'SOURCE_REF@123';
  112. $label = 123;
  113. $this->package->expects($this->once())->method('getSourceReference')->will($this->returnValue($ref));
  114. $this->io->expects($this->once())->method('writeError')->with($this->stringContains('Cloning '.$ref));
  115. $perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
  116. $perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
  117. $perforce->expects($this->at(0))->method('initializePath')->with($this->equalTo($this->testPath));
  118. $perforce->expects($this->at(1))->method('setStream')->with($this->equalTo($ref));
  119. $perforce->expects($this->at(2))->method('p4Login');
  120. $perforce->expects($this->at(3))->method('writeP4ClientSpec');
  121. $perforce->expects($this->at(4))->method('connectClient');
  122. $perforce->expects($this->at(5))->method('syncCodeBase')->with($label);
  123. $perforce->expects($this->at(6))->method('cleanupClientSpec');
  124. $this->downloader->setPerforce($perforce);
  125. $this->downloader->doDownload($this->package, $this->testPath, 'url');
  126. }
  127. /**
  128. * @depends testInitPerforceInstantiatesANewPerforceObject
  129. * @depends testInitPerforceDoesNothingIfPerforceAlreadySet
  130. */
  131. public function testDoDownloadWithNoTag()
  132. {
  133. $ref = 'SOURCE_REF';
  134. $label = null;
  135. $this->package->expects($this->once())->method('getSourceReference')->will($this->returnValue($ref));
  136. $this->io->expects($this->once())->method('writeError')->with($this->stringContains('Cloning '.$ref));
  137. $perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
  138. $perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
  139. $perforce->expects($this->at(0))->method('initializePath')->with($this->equalTo($this->testPath));
  140. $perforce->expects($this->at(1))->method('setStream')->with($this->equalTo($ref));
  141. $perforce->expects($this->at(2))->method('p4Login');
  142. $perforce->expects($this->at(3))->method('writeP4ClientSpec');
  143. $perforce->expects($this->at(4))->method('connectClient');
  144. $perforce->expects($this->at(5))->method('syncCodeBase')->with($label);
  145. $perforce->expects($this->at(6))->method('cleanupClientSpec');
  146. $this->downloader->setPerforce($perforce);
  147. $this->downloader->doDownload($this->package, $this->testPath, 'url');
  148. }
  149. }