PerforceDriverTest.php 6.1 KB

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