PerforceTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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\Util\ProcessExecutor;
  14. /**
  15. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  16. */
  17. class PerforceTest extends \PHPUnit_Framework_TestCase
  18. {
  19. protected $perforce;
  20. protected $processExecutor;
  21. protected $io;
  22. const TEST_DEPOT = 'depot';
  23. const TEST_BRANCH = 'branch';
  24. const TEST_P4USER = 'user';
  25. const TEST_CLIENT_NAME = 'TEST';
  26. const TEST_PORT = 'port';
  27. const TEST_PATH = 'path';
  28. public function setUp()
  29. {
  30. $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  31. $this->repoConfig = $this->getTestRepoConfig();
  32. $this->io = $this->getMockIOInterface();
  33. $this->perforce = new Perforce($this->repoConfig, self::TEST_PORT, self::TEST_PATH, $this->processExecutor, true, $this->io);
  34. }
  35. public function tearDown()
  36. {
  37. $this->perforce = null;
  38. $this->io = null;
  39. $this->repoConfig = null;
  40. $this->processExecutor = null;
  41. }
  42. public function getTestRepoConfig()
  43. {
  44. return array(
  45. 'depot' => self::TEST_DEPOT,
  46. 'branch' => self::TEST_BRANCH,
  47. 'p4user' => self::TEST_P4USER,
  48. 'unique_perforce_client_name' => self::TEST_CLIENT_NAME
  49. );
  50. }
  51. public function getMockIOInterface()
  52. {
  53. return $this->getMock('Composer\IO\IOInterface');
  54. }
  55. public function testGetClientWithoutStream()
  56. {
  57. $client = $this->perforce->getClient();
  58. $hostname = gethostname();
  59. $timestamp = time();
  60. $expected = 'composer_perforce_TEST_depot';
  61. $this->assertEquals($expected, $client);
  62. }
  63. public function testGetClientFromStream()
  64. {
  65. $this->setPerforceToStream();
  66. $client = $this->perforce->getClient();
  67. $expected = 'composer_perforce_TEST_depot_branch';
  68. $this->assertEquals($expected, $client);
  69. }
  70. public function testGetStreamWithoutStream()
  71. {
  72. $stream = $this->perforce->getStream();
  73. $this->assertEquals("//depot", $stream);
  74. }
  75. public function testGetStreamWithStream()
  76. {
  77. $this->setPerforceToStream();
  78. $stream = $this->perforce->getStream();
  79. $this->assertEquals('//depot/branch', $stream);
  80. }
  81. public function testGetStreamWithoutLabelWithStreamWithoutLabel()
  82. {
  83. $stream = $this->perforce->getStreamWithoutLabel('//depot/branch');
  84. $this->assertEquals('//depot/branch', $stream);
  85. }
  86. public function testGetStreamWithoutLabelWithStreamWithLabel()
  87. {
  88. $stream = $this->perforce->getStreamWithoutLabel('//depot/branching@label');
  89. $this->assertEquals('//depot/branching', $stream);
  90. }
  91. public function testGetClientSpec()
  92. {
  93. $clientSpec = $this->perforce->getP4ClientSpec();
  94. $expected = 'path/composer_perforce_TEST_depot.p4.spec';
  95. $this->assertEquals($expected, $clientSpec);
  96. }
  97. public function testGenerateP4Command()
  98. {
  99. $command = 'do something';
  100. $p4Command = $this->perforce->generateP4Command($command);
  101. $expected = 'p4 -u user -c composer_perforce_TEST_depot -p port do something';
  102. $this->assertEquals($expected, $p4Command);
  103. }
  104. public function testQueryP4UserWithUserAlreadySet()
  105. {
  106. $this->perforce->queryP4user();
  107. $this->assertEquals(self::TEST_P4USER, $this->perforce->getUser());
  108. }
  109. public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS()
  110. {
  111. $this->perforce->setUser(null);
  112. $this->perforce->setWindowsFlag(true);
  113. $expectedCommand = 'p4 set';
  114. $callback = function($command, &$output)
  115. {
  116. $output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL;
  117. return true;
  118. };
  119. $this->processExecutor->expects($this->at(0))
  120. ->method('execute')
  121. ->with($this->equalTo($expectedCommand))
  122. ->will($this->returnCallback($callback));
  123. $this->perforce->queryP4user();
  124. $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
  125. }
  126. public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS()
  127. {
  128. $this->perforce->setUser(null);
  129. $this->perforce->setWindowsFlag(false);
  130. $expectedCommand = 'echo $P4USER';
  131. $callback = function($command, &$output)
  132. {
  133. $output = 'TEST_P4VARIABLE_USER' . PHP_EOL;
  134. return true;
  135. };
  136. $this->processExecutor->expects($this->at(0))
  137. ->method('execute')
  138. ->with($this->equalTo($expectedCommand))
  139. ->will($this->returnCallback($callback));
  140. $this->perforce->queryP4user();
  141. $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
  142. }
  143. public function testQueryP4UserQueriesForUser()
  144. {
  145. $this->perforce->setUser(null);
  146. $expectedQuestion = 'Enter P4 User:';
  147. $this->io->expects($this->at(0))
  148. ->method('ask')
  149. ->with($this->equalTo($expectedQuestion))
  150. ->will($this->returnValue('TEST_QUERY_USER'));
  151. $this->perforce->queryP4user();
  152. $this->assertEquals('TEST_QUERY_USER', $this->perforce->getUser());
  153. }
  154. public function testQueryP4UserStoresResponseToQueryForUserWithWindows()
  155. {
  156. $this->perforce->setUser(null);
  157. $this->perforce->setWindowsFlag(true);
  158. $expectedQuestion = 'Enter P4 User:';
  159. $expectedCommand = 'p4 set P4USER=TEST_QUERY_USER';
  160. $this->io->expects($this->at(0))
  161. ->method('ask')
  162. ->with($this->equalTo($expectedQuestion))
  163. ->will($this->returnValue('TEST_QUERY_USER'));
  164. $this->processExecutor->expects($this->at(1))
  165. ->method('execute')
  166. ->with($this->equalTo($expectedCommand))
  167. ->will($this->returnValue(0));
  168. $this->perforce->queryP4user();
  169. }
  170. public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows()
  171. {
  172. $this->perforce->setUser(null);
  173. $this->perforce->setWindowsFlag(false);
  174. $expectedQuestion = 'Enter P4 User:';
  175. $expectedCommand = 'export P4USER=TEST_QUERY_USER';
  176. $this->io->expects($this->at(0))
  177. ->method('ask')
  178. ->with($this->equalTo($expectedQuestion))
  179. ->will($this->returnValue('TEST_QUERY_USER'));
  180. $this->processExecutor->expects($this->at(1))
  181. ->method('execute')
  182. ->with($this->equalTo($expectedCommand))
  183. ->will($this->returnValue(0));
  184. $this->perforce->queryP4user();
  185. }
  186. public function testQueryP4PasswordWithPasswordAlreadySet()
  187. {
  188. $repoConfig = array(
  189. 'depot' => 'depot',
  190. 'branch' => 'branch',
  191. 'p4user' => 'user',
  192. 'p4password' => 'TEST_PASSWORD'
  193. );
  194. $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, $this->getMockIOInterface(), 'TEST');
  195. $password = $this->perforce->queryP4Password();
  196. $this->assertEquals('TEST_PASSWORD', $password);
  197. }
  198. public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS()
  199. {
  200. $this->perforce->setWindowsFlag(true);
  201. $expectedCommand = 'p4 set';
  202. $callback = function($command, &$output)
  203. {
  204. $output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
  205. return true;
  206. };
  207. $this->processExecutor->expects($this->at(0))
  208. ->method('execute')
  209. ->with($this->equalTo($expectedCommand))
  210. ->will($this->returnCallback($callback));
  211. $password = $this->perforce->queryP4Password();
  212. $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
  213. }
  214. public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS()
  215. {
  216. $this->perforce->setWindowsFlag(false);
  217. $expectedCommand = 'echo $P4PASSWD';
  218. $callback = function($command, &$output)
  219. {
  220. $output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
  221. return true;
  222. };
  223. $this->processExecutor->expects($this->at(0))
  224. ->method('execute')
  225. ->with($this->equalTo($expectedCommand))
  226. ->will($this->returnCallback($callback));
  227. $password = $this->perforce->queryP4Password();
  228. $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
  229. }
  230. public function testQueryP4PasswordQueriesForPassword()
  231. {
  232. $expectedQuestion = 'Enter password for Perforce user user: ';
  233. $this->io->expects($this->at(0))
  234. ->method('askAndHideAnswer')
  235. ->with($this->equalTo($expectedQuestion))
  236. ->will($this->returnValue('TEST_QUERY_PASSWORD'));
  237. $password = $this->perforce->queryP4Password();
  238. $this->assertEquals('TEST_QUERY_PASSWORD', $password);
  239. }
  240. public function testWriteP4ClientSpecWithoutStream()
  241. {
  242. $stream = fopen('php://memory', 'w+');
  243. $this->perforce->writeClientSpecToFile($stream);
  244. rewind($stream);
  245. $expectedArray = $this->getExpectedClientSpec(false);
  246. try {
  247. foreach ($expectedArray as $expected) {
  248. $this->assertStringStartsWith($expected, fgets($stream));
  249. }
  250. $this->assertFalse(fgets($stream));
  251. } catch (Exception $e) {
  252. fclose($stream);
  253. throw $e;
  254. }
  255. fclose($stream);
  256. }
  257. public function testWriteP4ClientSpecWithStream()
  258. {
  259. $this->setPerforceToStream();
  260. $stream = fopen('php://memory', 'w+');
  261. $this->perforce->writeClientSpecToFile($stream);
  262. rewind($stream);
  263. $expectedArray = $this->getExpectedClientSpec(true);
  264. try {
  265. foreach ($expectedArray as $expected) {
  266. $this->assertStringStartsWith($expected, fgets($stream));
  267. }
  268. $this->assertFalse(fgets($stream));
  269. } catch (Exception $e) {
  270. fclose($stream);
  271. throw $e;
  272. }
  273. fclose($stream);
  274. }
  275. public function testIsLoggedIn()
  276. {
  277. $expectedCommand = 'p4 -u user -p port login -s';
  278. $this->processExecutor->expects($this->at(0))
  279. ->method('execute')
  280. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  281. ->will($this->returnValue(0));
  282. $this->perforce->isLoggedIn();
  283. }
  284. public function testConnectClient()
  285. {
  286. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec';
  287. $this->processExecutor->expects($this->at(0))
  288. ->method('execute')
  289. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  290. ->will($this->returnValue(0));
  291. $this->perforce->connectClient();
  292. }
  293. public function testGetBranchesWithStream()
  294. {
  295. $this->setPerforceToStream();
  296. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/...';
  297. $this->processExecutor->expects($this->at(0))
  298. ->method('execute')
  299. ->with($this->equalTo($expectedCommand))
  300. ->will(
  301. $this->returnCallback(
  302. function ($command, &$output) {
  303. $output = 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL;
  304. return true;
  305. }
  306. )
  307. );
  308. $expectedCommand2 = 'p4 -u user -p port changes //depot/branch/...';
  309. $expectedCallback = function($command, &$output)
  310. {
  311. $output = 'Change 1234 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
  312. return true;
  313. };
  314. $this->processExecutor->expects($this->at(1))
  315. ->method('execute')
  316. ->with($this->equalTo($expectedCommand2))
  317. ->will($this->returnCallback($expectedCallback));
  318. $branches = $this->perforce->getBranches();
  319. $this->assertEquals('//depot/branch@1234', $branches['master']);
  320. }
  321. public function testGetBranchesWithoutStream()
  322. {
  323. $expectedCommand = 'p4 -u user -p port changes //depot/...';
  324. $expectedCallback = function($command, &$output)
  325. {
  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 //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 //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 //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 //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 //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 //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->getMock('Composer\Util\ProcessExecutor');
  534. $expectedCommand = 'p4 -p 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->getMock('Composer\Util\ProcessExecutor');
  552. $expectedCommand = 'p4 -p 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->getMock('Composer\Util\Filesystem');
  612. $this->perforce->setFilesystem($fs);
  613. $testClient = $this->perforce->getClient();
  614. $expectedCommand = 'p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . $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. }