PerforceDriverTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Repository\Vcs;
  12. use Composer\Repository\Vcs\PerforceDriver;
  13. use Composer\Test\TestCase;
  14. use Composer\Util\Filesystem;
  15. use Composer\Config;
  16. use Composer\Util\Perforce;
  17. /**
  18. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  19. */
  20. class PerforceDriverTest extends TestCase
  21. {
  22. protected $config;
  23. protected $io;
  24. protected $process;
  25. protected $httpDownloader;
  26. protected $testPath;
  27. protected $driver;
  28. protected $repoConfig;
  29. protected $perforce;
  30. const TEST_URL = 'TEST_PERFORCE_URL';
  31. const TEST_DEPOT = 'TEST_DEPOT_CONFIG';
  32. const TEST_BRANCH = 'TEST_BRANCH_CONFIG';
  33. protected function setUp()
  34. {
  35. $this->testPath = $this->getUniqueTmpDirectory();
  36. $this->config = $this->getTestConfig($this->testPath);
  37. $this->repoConfig = $this->getTestRepoConfig();
  38. $this->io = $this->getMockIOInterface();
  39. $this->process = $this->getMockProcessExecutor();
  40. $this->httpDownloader = $this->getMockHttpDownloader();
  41. $this->perforce = $this->getMockPerforce();
  42. $this->driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->httpDownloader, $this->process);
  43. $this->overrideDriverInternalPerforce($this->perforce);
  44. }
  45. protected function tearDown()
  46. {
  47. //cleanup directory under test path
  48. $fs = new Filesystem;
  49. $fs->removeDirectory($this->testPath);
  50. $this->driver = null;
  51. $this->perforce = null;
  52. $this->httpDownloader = null;
  53. $this->process = null;
  54. $this->io = null;
  55. $this->repoConfig = null;
  56. $this->config = null;
  57. $this->testPath = null;
  58. }
  59. protected function overrideDriverInternalPerforce(Perforce $perforce)
  60. {
  61. $reflectionClass = new \ReflectionClass($this->driver);
  62. $property = $reflectionClass->getProperty('perforce');
  63. $property->setAccessible(true);
  64. $property->setValue($this->driver, $perforce);
  65. }
  66. protected function getTestConfig($testPath)
  67. {
  68. $config = new Config();
  69. $config->merge(array('config' => array('home' => $testPath)));
  70. return $config;
  71. }
  72. protected function getTestRepoConfig()
  73. {
  74. return array(
  75. 'url' => self::TEST_URL,
  76. 'depot' => self::TEST_DEPOT,
  77. 'branch' => self::TEST_BRANCH,
  78. );
  79. }
  80. protected function getMockIOInterface()
  81. {
  82. return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  83. }
  84. protected function getMockProcessExecutor()
  85. {
  86. return $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  87. }
  88. protected function getMockHttpDownloader()
  89. {
  90. return $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
  91. }
  92. protected function getMockPerforce()
  93. {
  94. $methods = array('p4login', 'checkStream', 'writeP4ClientSpec', 'connectClient', 'getComposerInformation', 'cleanupClientSpec');
  95. return $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
  96. }
  97. public function testInitializeCapturesVariablesFromRepoConfig()
  98. {
  99. $driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->httpDownloader, $this->process);
  100. $driver->initialize();
  101. $this->assertEquals(self::TEST_URL, $driver->getUrl());
  102. $this->assertEquals(self::TEST_DEPOT, $driver->getDepot());
  103. $this->assertEquals(self::TEST_BRANCH, $driver->getBranch());
  104. }
  105. public function testInitializeLogsInAndConnectsClient()
  106. {
  107. $this->perforce->expects($this->at(0))->method('p4Login');
  108. $this->perforce->expects($this->at(1))->method('checkStream');
  109. $this->perforce->expects($this->at(2))->method('writeP4ClientSpec');
  110. $this->perforce->expects($this->at(3))->method('connectClient');
  111. $this->driver->initialize();
  112. }
  113. /**
  114. * @depends testInitializeCapturesVariablesFromRepoConfig
  115. * @depends testInitializeLogsInAndConnectsClient
  116. */
  117. public function testHasComposerFileReturnsFalseOnNoComposerFile()
  118. {
  119. $identifier = 'TEST_IDENTIFIER';
  120. $formatted_depot_path = '//' . self::TEST_DEPOT . '/' . $identifier;
  121. $this->perforce->expects($this->any())->method('getComposerInformation')->with($this->equalTo($formatted_depot_path))->will($this->returnValue(array()));
  122. $this->driver->initialize();
  123. $result = $this->driver->hasComposerFile($identifier);
  124. $this->assertFalse($result);
  125. }
  126. /**
  127. * @depends testInitializeCapturesVariablesFromRepoConfig
  128. * @depends testInitializeLogsInAndConnectsClient
  129. */
  130. public function testHasComposerFileReturnsTrueWithOneOrMoreComposerFiles()
  131. {
  132. $identifier = 'TEST_IDENTIFIER';
  133. $formatted_depot_path = '//' . self::TEST_DEPOT . '/' . $identifier;
  134. $this->perforce->expects($this->any())->method('getComposerInformation')->with($this->equalTo($formatted_depot_path))->will($this->returnValue(array('')));
  135. $this->driver->initialize();
  136. $result = $this->driver->hasComposerFile($identifier);
  137. $this->assertTrue($result);
  138. }
  139. /**
  140. * Test that supports() simply return false.
  141. *
  142. * @covers \Composer\Repository\Vcs\PerforceDriver::supports
  143. *
  144. * @return void
  145. */
  146. public function testSupportsReturnsFalseNoDeepCheck()
  147. {
  148. $this->expectOutputString('');
  149. $this->assertFalse(PerforceDriver::supports($this->io, $this->config, 'existing.url'));
  150. }
  151. public function testCleanup()
  152. {
  153. $this->perforce->expects($this->once())->method('cleanupClientSpec');
  154. $this->driver->cleanup();
  155. }
  156. }