PerforceTest.php 25 KB

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