PerforceDownloaderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\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->getMock('Composer\Util\ProcessExecutor');
  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->getMock('Composer\IO\IOInterface');
  70. }
  71. protected function getMockPackageInterface(VcsRepository $repository)
  72. {
  73. $package = $this->getMock('Composer\Package\PackageInterface');
  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. $class = 'Composer\Repository\VcsRepository';
  84. $methods = array('getRepoConfig');
  85. $args = array($repoConfig, $io, $config);
  86. $repository = $this->getMock($class, $methods, $args);
  87. $repository->expects($this->any())->method('getRepoConfig')->will($this->returnValue($repoConfig));
  88. return $repository;
  89. }
  90. public function testInitPerforceInstantiatesANewPerforceObject()
  91. {
  92. $this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
  93. }
  94. public function testInitPerforceDoesNothingIfPerforceAlreadySet()
  95. {
  96. $perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
  97. $this->downloader->setPerforce($perforce);
  98. $this->repository->expects($this->never())->method('getRepoConfig');
  99. $this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
  100. }
  101. /**
  102. * @depends testInitPerforceInstantiatesANewPerforceObject
  103. * @depends testInitPerforceDoesNothingIfPerforceAlreadySet
  104. */
  105. public function testDoDownloadWithTag()
  106. {
  107. //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.
  108. $ref = 'SOURCE_REF@123';
  109. $label = 123;
  110. $this->package->expects($this->once())->method('getSourceReference')->will($this->returnValue($ref));
  111. $this->io->expects($this->once())->method('writeError')->with($this->stringContains('Cloning '.$ref));
  112. $perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
  113. $perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
  114. $perforce->expects($this->at(0))->method('initializePath')->with($this->equalTo($this->testPath));
  115. $perforce->expects($this->at(1))->method('setStream')->with($this->equalTo($ref));
  116. $perforce->expects($this->at(2))->method('p4Login');
  117. $perforce->expects($this->at(3))->method('writeP4ClientSpec');
  118. $perforce->expects($this->at(4))->method('connectClient');
  119. $perforce->expects($this->at(5))->method('syncCodeBase')->with($label);
  120. $perforce->expects($this->at(6))->method('cleanupClientSpec');
  121. $this->downloader->setPerforce($perforce);
  122. $this->downloader->doDownload($this->package, $this->testPath, 'url');
  123. }
  124. /**
  125. * @depends testInitPerforceInstantiatesANewPerforceObject
  126. * @depends testInitPerforceDoesNothingIfPerforceAlreadySet
  127. */
  128. public function testDoDownloadWithNoTag()
  129. {
  130. $ref = 'SOURCE_REF';
  131. $label = null;
  132. $this->package->expects($this->once())->method('getSourceReference')->will($this->returnValue($ref));
  133. $this->io->expects($this->once())->method('writeError')->with($this->stringContains('Cloning '.$ref));
  134. $perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
  135. $perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
  136. $perforce->expects($this->at(0))->method('initializePath')->with($this->equalTo($this->testPath));
  137. $perforce->expects($this->at(1))->method('setStream')->with($this->equalTo($ref));
  138. $perforce->expects($this->at(2))->method('p4Login');
  139. $perforce->expects($this->at(3))->method('writeP4ClientSpec');
  140. $perforce->expects($this->at(4))->method('connectClient');
  141. $perforce->expects($this->at(5))->method('syncCodeBase')->with($label);
  142. $perforce->expects($this->at(6))->method('cleanupClientSpec');
  143. $this->downloader->setPerforce($perforce);
  144. $this->downloader->doDownload($this->package, $this->testPath, 'url');
  145. }
  146. }