PerforceDownloaderTest.php 6.7 KB

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