PerforceDriverTest.php 6.0 KB

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