PerforceTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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\Util;
  12. use Composer\Util\Perforce;
  13. use Composer\Test\TestCase;
  14. use Composer\Util\ProcessExecutor;
  15. /**
  16. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  17. */
  18. class PerforceTest extends TestCase
  19. {
  20. protected $perforce;
  21. protected $processExecutor;
  22. protected $repoConfig;
  23. protected $io;
  24. const TEST_DEPOT = 'depot';
  25. const TEST_BRANCH = 'branch';
  26. const TEST_P4USER = 'user';
  27. const TEST_CLIENT_NAME = 'TEST';
  28. const TEST_PORT = 'port';
  29. const TEST_PATH = 'path';
  30. protected function setUp()
  31. {
  32. $this->processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  33. $this->repoConfig = $this->getTestRepoConfig();
  34. $this->io = $this->getMockIOInterface();
  35. $this->createNewPerforceWithWindowsFlag(true);
  36. }
  37. protected function tearDown()
  38. {
  39. $this->perforce = null;
  40. $this->io = null;
  41. $this->repoConfig = null;
  42. $this->processExecutor = null;
  43. }
  44. public function getTestRepoConfig()
  45. {
  46. return array(
  47. 'depot' => self::TEST_DEPOT,
  48. 'branch' => self::TEST_BRANCH,
  49. 'p4user' => self::TEST_P4USER,
  50. 'unique_perforce_client_name' => self::TEST_CLIENT_NAME,
  51. );
  52. }
  53. public function getMockIOInterface()
  54. {
  55. return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  56. }
  57. protected function createNewPerforceWithWindowsFlag($flag)
  58. {
  59. $this->perforce = new Perforce($this->repoConfig, self::TEST_PORT, self::TEST_PATH, $this->processExecutor, $flag, $this->io);
  60. }
  61. public function testGetClientWithoutStream()
  62. {
  63. $client = $this->perforce->getClient();
  64. $hostname = gethostname();
  65. $timestamp = time();
  66. $expected = 'composer_perforce_TEST_depot';
  67. $this->assertEquals($expected, $client);
  68. }
  69. public function testGetClientFromStream()
  70. {
  71. $this->setPerforceToStream();
  72. $client = $this->perforce->getClient();
  73. $expected = 'composer_perforce_TEST_depot_branch';
  74. $this->assertEquals($expected, $client);
  75. }
  76. public function testGetStreamWithoutStream()
  77. {
  78. $stream = $this->perforce->getStream();
  79. $this->assertEquals("//depot", $stream);
  80. }
  81. public function testGetStreamWithStream()
  82. {
  83. $this->setPerforceToStream();
  84. $stream = $this->perforce->getStream();
  85. $this->assertEquals('//depot/branch', $stream);
  86. }
  87. public function testGetStreamWithoutLabelWithStreamWithoutLabel()
  88. {
  89. $stream = $this->perforce->getStreamWithoutLabel('//depot/branch');
  90. $this->assertEquals('//depot/branch', $stream);
  91. }
  92. public function testGetStreamWithoutLabelWithStreamWithLabel()
  93. {
  94. $stream = $this->perforce->getStreamWithoutLabel('//depot/branching@label');
  95. $this->assertEquals('//depot/branching', $stream);
  96. }
  97. public function testGetClientSpec()
  98. {
  99. $clientSpec = $this->perforce->getP4ClientSpec();
  100. $expected = 'path/composer_perforce_TEST_depot.p4.spec';
  101. $this->assertEquals($expected, $clientSpec);
  102. }
  103. public function testGenerateP4Command()
  104. {
  105. $command = 'do something';
  106. $p4Command = $this->perforce->generateP4Command($command);
  107. $expected = 'p4 -u user -c composer_perforce_TEST_depot -p port do something';
  108. $this->assertEquals($expected, $p4Command);
  109. }
  110. public function testQueryP4UserWithUserAlreadySet()
  111. {
  112. $this->perforce->queryP4user();
  113. $this->assertEquals(self::TEST_P4USER, $this->perforce->getUser());
  114. }
  115. public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS()
  116. {
  117. $this->createNewPerforceWithWindowsFlag(true);
  118. $this->perforce->setUser(null);
  119. $expectedCommand = 'p4 set';
  120. $callback = function ($command, &$output) {
  121. $output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL;
  122. return true;
  123. };
  124. $this->processExecutor->expects($this->at(0))
  125. ->method('execute')
  126. ->with($this->equalTo($expectedCommand))
  127. ->will($this->returnCallback($callback));
  128. $this->perforce->queryP4user();
  129. $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
  130. }
  131. public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS()
  132. {
  133. $this->createNewPerforceWithWindowsFlag(false);
  134. $this->perforce->setUser(null);
  135. $expectedCommand = 'echo $P4USER';
  136. $callback = function ($command, &$output) {
  137. $output = 'TEST_P4VARIABLE_USER' . PHP_EOL;
  138. return true;
  139. };
  140. $this->processExecutor->expects($this->at(0))
  141. ->method('execute')
  142. ->with($this->equalTo($expectedCommand))
  143. ->will($this->returnCallback($callback));
  144. $this->perforce->queryP4user();
  145. $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
  146. }
  147. public function testQueryP4UserQueriesForUser()
  148. {
  149. $this->perforce->setUser(null);
  150. $expectedQuestion = 'Enter P4 User:';
  151. $this->io->expects($this->at(0))
  152. ->method('ask')
  153. ->with($this->equalTo($expectedQuestion))
  154. ->will($this->returnValue('TEST_QUERY_USER'));
  155. $this->perforce->queryP4user();
  156. $this->assertEquals('TEST_QUERY_USER', $this->perforce->getUser());
  157. }
  158. public function testQueryP4UserStoresResponseToQueryForUserWithWindows()
  159. {
  160. $this->createNewPerforceWithWindowsFlag(true);
  161. $this->perforce->setUser(null);
  162. $expectedQuestion = 'Enter P4 User:';
  163. $expectedCommand = 'p4 set P4USER=TEST_QUERY_USER';
  164. $this->io->expects($this->at(0))
  165. ->method('ask')
  166. ->with($this->equalTo($expectedQuestion))
  167. ->will($this->returnValue('TEST_QUERY_USER'));
  168. $this->processExecutor->expects($this->at(1))
  169. ->method('execute')
  170. ->with($this->equalTo($expectedCommand))
  171. ->will($this->returnValue(0));
  172. $this->perforce->queryP4user();
  173. }
  174. public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows()
  175. {
  176. $this->createNewPerforceWithWindowsFlag(false);
  177. $this->perforce->setUser(null);
  178. $expectedQuestion = 'Enter P4 User:';
  179. $expectedCommand = 'export P4USER=TEST_QUERY_USER';
  180. $this->io->expects($this->at(0))
  181. ->method('ask')
  182. ->with($this->equalTo($expectedQuestion))
  183. ->will($this->returnValue('TEST_QUERY_USER'));
  184. $this->processExecutor->expects($this->at(1))
  185. ->method('execute')
  186. ->with($this->equalTo($expectedCommand))
  187. ->will($this->returnValue(0));
  188. $this->perforce->queryP4user();
  189. }
  190. public function testQueryP4PasswordWithPasswordAlreadySet()
  191. {
  192. $repoConfig = array(
  193. 'depot' => 'depot',
  194. 'branch' => 'branch',
  195. 'p4user' => 'user',
  196. 'p4password' => 'TEST_PASSWORD',
  197. );
  198. $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, $this->getMockIOInterface());
  199. $password = $this->perforce->queryP4Password();
  200. $this->assertEquals('TEST_PASSWORD', $password);
  201. }
  202. public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS()
  203. {
  204. $this->createNewPerforceWithWindowsFlag(true);
  205. $expectedCommand = 'p4 set';
  206. $callback = function ($command, &$output) {
  207. $output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
  208. return true;
  209. };
  210. $this->processExecutor->expects($this->at(0))
  211. ->method('execute')
  212. ->with($this->equalTo($expectedCommand))
  213. ->will($this->returnCallback($callback));
  214. $password = $this->perforce->queryP4Password();
  215. $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
  216. }
  217. public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS()
  218. {
  219. $this->createNewPerforceWithWindowsFlag(false);
  220. $expectedCommand = 'echo $P4PASSWD';
  221. $callback = function ($command, &$output) {
  222. $output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
  223. return true;
  224. };
  225. $this->processExecutor->expects($this->at(0))
  226. ->method('execute')
  227. ->with($this->equalTo($expectedCommand))
  228. ->will($this->returnCallback($callback));
  229. $password = $this->perforce->queryP4Password();
  230. $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
  231. }
  232. public function testQueryP4PasswordQueriesForPassword()
  233. {
  234. $expectedQuestion = 'Enter password for Perforce user user: ';
  235. $this->io->expects($this->at(0))
  236. ->method('askAndHideAnswer')
  237. ->with($this->equalTo($expectedQuestion))
  238. ->will($this->returnValue('TEST_QUERY_PASSWORD'));
  239. $password = $this->perforce->queryP4Password();
  240. $this->assertEquals('TEST_QUERY_PASSWORD', $password);
  241. }
  242. public function testWriteP4ClientSpecWithoutStream()
  243. {
  244. $stream = fopen('php://memory', 'w+');
  245. $this->perforce->writeClientSpecToFile($stream);
  246. rewind($stream);
  247. $expectedArray = $this->getExpectedClientSpec(false);
  248. try {
  249. foreach ($expectedArray as $expected) {
  250. $this->assertStringStartsWith($expected, fgets($stream));
  251. }
  252. $this->assertFalse(fgets($stream));
  253. } catch (\Exception $e) {
  254. fclose($stream);
  255. throw $e;
  256. }
  257. fclose($stream);
  258. }
  259. public function testWriteP4ClientSpecWithStream()
  260. {
  261. $this->setPerforceToStream();
  262. $stream = fopen('php://memory', 'w+');
  263. $this->perforce->writeClientSpecToFile($stream);
  264. rewind($stream);
  265. $expectedArray = $this->getExpectedClientSpec(true);
  266. try {
  267. foreach ($expectedArray as $expected) {
  268. $this->assertStringStartsWith($expected, fgets($stream));
  269. }
  270. $this->assertFalse(fgets($stream));
  271. } catch (\Exception $e) {
  272. fclose($stream);
  273. throw $e;
  274. }
  275. fclose($stream);
  276. }
  277. public function testIsLoggedIn()
  278. {
  279. $expectedCommand = 'p4 -u user -p port login -s';
  280. $this->processExecutor->expects($this->at(0))
  281. ->method('execute')
  282. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  283. ->will($this->returnValue(0));
  284. $this->perforce->isLoggedIn();
  285. }
  286. public function testConnectClient()
  287. {
  288. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec';
  289. $this->processExecutor->expects($this->at(0))
  290. ->method('execute')
  291. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  292. ->will($this->returnValue(0));
  293. $this->perforce->connectClient();
  294. }
  295. public function testGetBranchesWithStream()
  296. {
  297. $this->setPerforceToStream();
  298. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port streams '.ProcessExecutor::escape('//depot/...');
  299. $this->processExecutor->expects($this->at(0))
  300. ->method('execute')
  301. ->with($this->equalTo($expectedCommand))
  302. ->will(
  303. $this->returnCallback(
  304. function ($command, &$output) {
  305. $output = 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL;
  306. return true;
  307. }
  308. )
  309. );
  310. $expectedCommand2 = 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/branch/...');
  311. $expectedCallback = function ($command, &$output) {
  312. $output = 'Change 1234 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
  313. return true;
  314. };
  315. $this->processExecutor->expects($this->at(1))
  316. ->method('execute')
  317. ->with($this->equalTo($expectedCommand2))
  318. ->will($this->returnCallback($expectedCallback));
  319. $branches = $this->perforce->getBranches();
  320. $this->assertEquals('//depot/branch@1234', $branches['master']);
  321. }
  322. public function testGetBranchesWithoutStream()
  323. {
  324. $expectedCommand = 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/...');
  325. $expectedCallback = function ($command, &$output) {
  326. $output = 'Change 5678 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
  327. return true;
  328. };
  329. $this->processExecutor->expects($this->once())
  330. ->method('execute')
  331. ->with($this->equalTo($expectedCommand))
  332. ->will($this->returnCallback($expectedCallback));
  333. $branches = $this->perforce->getBranches();
  334. $this->assertEquals('//depot@5678', $branches['master']);
  335. }
  336. public function testGetTagsWithoutStream()
  337. {
  338. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port labels';
  339. $this->processExecutor->expects($this->at(0))
  340. ->method('execute')
  341. ->with($this->equalTo($expectedCommand))
  342. ->will(
  343. $this->returnCallback(
  344. function ($command, &$output) {
  345. $output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL;
  346. return true;
  347. }
  348. )
  349. );
  350. $tags = $this->perforce->getTags();
  351. $this->assertEquals('//depot@0.0.1', $tags['0.0.1']);
  352. $this->assertEquals('//depot@0.0.2', $tags['0.0.2']);
  353. }
  354. public function testGetTagsWithStream()
  355. {
  356. $this->setPerforceToStream();
  357. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port labels';
  358. $this->processExecutor->expects($this->at(0))
  359. ->method('execute')
  360. ->with($this->equalTo($expectedCommand))
  361. ->will(
  362. $this->returnCallback(
  363. function ($command, &$output) {
  364. $output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL;
  365. return true;
  366. }
  367. )
  368. );
  369. $tags = $this->perforce->getTags();
  370. $this->assertEquals('//depot/branch@0.0.1', $tags['0.0.1']);
  371. $this->assertEquals('//depot/branch@0.0.2', $tags['0.0.2']);
  372. }
  373. public function testCheckStreamWithoutStream()
  374. {
  375. $result = $this->perforce->checkStream('depot');
  376. $this->assertFalse($result);
  377. $this->assertFalse($this->perforce->isStream());
  378. }
  379. public function testCheckStreamWithStream()
  380. {
  381. $this->processExecutor->expects($this->any())->method('execute')
  382. ->will(
  383. $this->returnCallback(
  384. function ($command, &$output) {
  385. $output = 'Depot depot 2013/06/25 stream /p4/1/depots/depot/... \'Created by Me\'';
  386. return true;
  387. }
  388. )
  389. );
  390. $result = $this->perforce->checkStream('depot');
  391. $this->assertTrue($result);
  392. $this->assertTrue($this->perforce->isStream());
  393. }
  394. public function testGetComposerInformationWithoutLabelWithoutStream()
  395. {
  396. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json');
  397. $this->processExecutor->expects($this->at(0))
  398. ->method('execute')
  399. ->with($this->equalTo($expectedCommand))
  400. ->will(
  401. $this->returnCallback(
  402. function ($command, &$output) {
  403. $output = PerforceTest::getComposerJson();
  404. return true;
  405. }
  406. )
  407. );
  408. $result = $this->perforce->getComposerInformation('//depot');
  409. $expected = array(
  410. 'name' => 'test/perforce',
  411. 'description' => 'Basic project for testing',
  412. 'minimum-stability' => 'dev',
  413. 'autoload' => array('psr-0' => array()),
  414. );
  415. $this->assertEquals($expected, $result);
  416. }
  417. public function testGetComposerInformationWithLabelWithoutStream()
  418. {
  419. $expectedCommand = 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/composer.json@0.0.1');
  420. $this->processExecutor->expects($this->at(0))
  421. ->method('execute')
  422. ->with($this->equalTo($expectedCommand))
  423. ->will(
  424. $this->returnCallback(
  425. function ($command, &$output) {
  426. $output = '//depot/composer.json#1 - branch change 10001 (text)';
  427. return true;
  428. }
  429. )
  430. );
  431. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json@10001');
  432. $this->processExecutor->expects($this->at(1))
  433. ->method('execute')
  434. ->with($this->equalTo($expectedCommand))
  435. ->will(
  436. $this->returnCallback(
  437. function ($command, &$output) {
  438. $output = PerforceTest::getComposerJson();
  439. return true;
  440. }
  441. )
  442. );
  443. $result = $this->perforce->getComposerInformation('//depot@0.0.1');
  444. $expected = array(
  445. 'name' => 'test/perforce',
  446. 'description' => 'Basic project for testing',
  447. 'minimum-stability' => 'dev',
  448. 'autoload' => array('psr-0' => array()),
  449. );
  450. $this->assertEquals($expected, $result);
  451. }
  452. public function testGetComposerInformationWithoutLabelWithStream()
  453. {
  454. $this->setPerforceToStream();
  455. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json');
  456. $this->processExecutor->expects($this->at(0))
  457. ->method('execute')
  458. ->with($this->equalTo($expectedCommand))
  459. ->will(
  460. $this->returnCallback(
  461. function ($command, &$output) {
  462. $output = PerforceTest::getComposerJson();
  463. return true;
  464. }
  465. )
  466. );
  467. $result = $this->perforce->getComposerInformation('//depot/branch');
  468. $expected = array(
  469. 'name' => 'test/perforce',
  470. 'description' => 'Basic project for testing',
  471. 'minimum-stability' => 'dev',
  472. 'autoload' => array('psr-0' => array()),
  473. );
  474. $this->assertEquals($expected, $result);
  475. }
  476. public function testGetComposerInformationWithLabelWithStream()
  477. {
  478. $this->setPerforceToStream();
  479. $expectedCommand = 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/branch/composer.json@0.0.1');
  480. $this->processExecutor->expects($this->at(0))
  481. ->method('execute')
  482. ->with($this->equalTo($expectedCommand))
  483. ->will(
  484. $this->returnCallback(
  485. function ($command, &$output) {
  486. $output = '//depot/composer.json#1 - branch change 10001 (text)';
  487. return true;
  488. }
  489. )
  490. );
  491. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json@10001');
  492. $this->processExecutor->expects($this->at(1))
  493. ->method('execute')
  494. ->with($this->equalTo($expectedCommand))
  495. ->will(
  496. $this->returnCallback(
  497. function ($command, &$output) {
  498. $output = PerforceTest::getComposerJson();
  499. return true;
  500. }
  501. )
  502. );
  503. $result = $this->perforce->getComposerInformation('//depot/branch@0.0.1');
  504. $expected = array(
  505. 'name' => 'test/perforce',
  506. 'description' => 'Basic project for testing',
  507. 'minimum-stability' => 'dev',
  508. 'autoload' => array('psr-0' => array()),
  509. );
  510. $this->assertEquals($expected, $result);
  511. }
  512. public function testSyncCodeBaseWithoutStream()
  513. {
  514. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label';
  515. $this->processExecutor->expects($this->at(0))
  516. ->method('execute')
  517. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  518. ->will($this->returnValue(0));
  519. $this->perforce->syncCodeBase('label');
  520. }
  521. public function testSyncCodeBaseWithStream()
  522. {
  523. $this->setPerforceToStream();
  524. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label';
  525. $this->processExecutor->expects($this->at(0))
  526. ->method('execute')
  527. ->with($this->equalTo($expectedCommand))
  528. ->will($this->returnValue(0));
  529. $this->perforce->syncCodeBase('label');
  530. }
  531. public function testCheckServerExists()
  532. {
  533. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  534. $expectedCommand = 'p4 -p '.ProcessExecutor::escape('perforce.does.exist:port').' info -s';
  535. $processExecutor->expects($this->at(0))
  536. ->method('execute')
  537. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  538. ->will($this->returnValue(0));
  539. $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
  540. $this->assertTrue($result);
  541. }
  542. /**
  543. * Test if "p4" command is missing.
  544. *
  545. * @covers \Composer\Util\Perforce::checkServerExists
  546. *
  547. * @return void
  548. */
  549. public function testCheckServerClientError()
  550. {
  551. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  552. $expectedCommand = 'p4 -p '.ProcessExecutor::escape('perforce.does.exist:port').' info -s';
  553. $processExecutor->expects($this->at(0))
  554. ->method('execute')
  555. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  556. ->will($this->returnValue(127));
  557. $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
  558. $this->assertFalse($result);
  559. }
  560. public static function getComposerJson()
  561. {
  562. $composer_json = array(
  563. '{',
  564. '"name": "test/perforce",',
  565. '"description": "Basic project for testing",',
  566. '"minimum-stability": "dev",',
  567. '"autoload": {',
  568. '"psr-0" : {',
  569. '}',
  570. '}',
  571. '}',
  572. );
  573. return implode($composer_json);
  574. }
  575. private function getExpectedClientSpec($withStream)
  576. {
  577. $expectedArray = array(
  578. 'Client: composer_perforce_TEST_depot',
  579. PHP_EOL,
  580. 'Update:',
  581. PHP_EOL,
  582. 'Access:',
  583. 'Owner: user',
  584. PHP_EOL,
  585. 'Description:',
  586. ' Created by user from composer.',
  587. PHP_EOL,
  588. 'Root: path',
  589. PHP_EOL,
  590. 'Options: noallwrite noclobber nocompress unlocked modtime rmdir',
  591. PHP_EOL,
  592. 'SubmitOptions: revertunchanged',
  593. PHP_EOL,
  594. 'LineEnd: local',
  595. PHP_EOL,
  596. );
  597. if ($withStream) {
  598. $expectedArray[] = 'Stream:';
  599. $expectedArray[] = ' //depot/branch';
  600. } else {
  601. $expectedArray[] = 'View: //depot/... //composer_perforce_TEST_depot/...';
  602. }
  603. return $expectedArray;
  604. }
  605. private function setPerforceToStream()
  606. {
  607. $this->perforce->setStream('//depot/branch');
  608. }
  609. public function testCleanupClientSpecShouldDeleteClient()
  610. {
  611. $fs = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  612. $this->perforce->setFilesystem($fs);
  613. $testClient = $this->perforce->getClient();
  614. $expectedCommand = 'p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . ProcessExecutor::escape($testClient);
  615. $this->processExecutor->expects($this->once())->method('execute')->with($this->equalTo($expectedCommand));
  616. $fs->expects($this->once())->method('remove')->with($this->perforce->getP4ClientSpec());
  617. $this->perforce->cleanupClientSpec();
  618. }
  619. }