PerforceDownloaderTest.php 6.5 KB

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