PerforceTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. {
  120. $output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL;
  121. return true;
  122. };
  123. $this->processExecutor->expects($this->at(0))
  124. ->method('execute')
  125. ->with($this->equalTo($expectedCommand))
  126. ->will($this->returnCallback($callback));
  127. $this->perforce->queryP4user();
  128. $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
  129. }
  130. public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS()
  131. {
  132. $this->createNewPerforceWithWindowsFlag(false);
  133. $this->perforce->setUser(null);
  134. $expectedCommand = 'echo $P4USER';
  135. $callback = function($command, &$output)
  136. {
  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(), 'TEST');
  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. {
  208. $output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
  209. return true;
  210. };
  211. $this->processExecutor->expects($this->at(0))
  212. ->method('execute')
  213. ->with($this->equalTo($expectedCommand))
  214. ->will($this->returnCallback($callback));
  215. $password = $this->perforce->queryP4Password();
  216. $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
  217. }
  218. public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS()
  219. {
  220. $this->createNewPerforceWithWindowsFlag(false);
  221. $expectedCommand = 'echo $P4PASSWD';
  222. $callback = function($command, &$output)
  223. {
  224. $output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
  225. return true;
  226. };
  227. $this->processExecutor->expects($this->at(0))
  228. ->method('execute')
  229. ->with($this->equalTo($expectedCommand))
  230. ->will($this->returnCallback($callback));
  231. $password = $this->perforce->queryP4Password();
  232. $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
  233. }
  234. public function testQueryP4PasswordQueriesForPassword()
  235. {
  236. $expectedQuestion = 'Enter password for Perforce user user: ';
  237. $this->io->expects($this->at(0))
  238. ->method('askAndHideAnswer')
  239. ->with($this->equalTo($expectedQuestion))
  240. ->will($this->returnValue('TEST_QUERY_PASSWORD'));
  241. $password = $this->perforce->queryP4Password();
  242. $this->assertEquals('TEST_QUERY_PASSWORD', $password);
  243. }
  244. public function testWriteP4ClientSpecWithoutStream()
  245. {
  246. $stream = fopen('php://memory', 'w+');
  247. $this->perforce->writeClientSpecToFile($stream);
  248. rewind($stream);
  249. $expectedArray = $this->getExpectedClientSpec(false);
  250. try {
  251. foreach ($expectedArray as $expected) {
  252. $this->assertStringStartsWith($expected, fgets($stream));
  253. }
  254. $this->assertFalse(fgets($stream));
  255. } catch (Exception $e) {
  256. fclose($stream);
  257. throw $e;
  258. }
  259. fclose($stream);
  260. }
  261. public function testWriteP4ClientSpecWithStream()
  262. {
  263. $this->setPerforceToStream();
  264. $stream = fopen('php://memory', 'w+');
  265. $this->perforce->writeClientSpecToFile($stream);
  266. rewind($stream);
  267. $expectedArray = $this->getExpectedClientSpec(true);
  268. try {
  269. foreach ($expectedArray as $expected) {
  270. $this->assertStringStartsWith($expected, fgets($stream));
  271. }
  272. $this->assertFalse(fgets($stream));
  273. } catch (Exception $e) {
  274. fclose($stream);
  275. throw $e;
  276. }
  277. fclose($stream);
  278. }
  279. public function testIsLoggedIn()
  280. {
  281. $expectedCommand = 'p4 -u user -p port login -s';
  282. $this->processExecutor->expects($this->at(0))
  283. ->method('execute')
  284. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  285. ->will($this->returnValue(0));
  286. $this->perforce->isLoggedIn();
  287. }
  288. public function testConnectClient()
  289. {
  290. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec';
  291. $this->processExecutor->expects($this->at(0))
  292. ->method('execute')
  293. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  294. ->will($this->returnValue(0));
  295. $this->perforce->connectClient();
  296. }
  297. public function testGetBranchesWithStream()
  298. {
  299. $this->setPerforceToStream();
  300. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/...';
  301. $this->processExecutor->expects($this->at(0))
  302. ->method('execute')
  303. ->with($this->equalTo($expectedCommand))
  304. ->will(
  305. $this->returnCallback(
  306. function ($command, &$output) {
  307. $output = 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL;
  308. return true;
  309. }
  310. )
  311. );
  312. $expectedCommand2 = 'p4 -u user -p port changes //depot/branch/...';
  313. $expectedCallback = function($command, &$output)
  314. {
  315. $output = 'Change 1234 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
  316. return true;
  317. };
  318. $this->processExecutor->expects($this->at(1))
  319. ->method('execute')
  320. ->with($this->equalTo($expectedCommand2))
  321. ->will($this->returnCallback($expectedCallback));
  322. $branches = $this->perforce->getBranches();
  323. $this->assertEquals('//depot/branch@1234', $branches['master']);
  324. }
  325. public function testGetBranchesWithoutStream()
  326. {
  327. $expectedCommand = 'p4 -u user -p port changes //depot/...';
  328. $expectedCallback = function($command, &$output)
  329. {
  330. $output = 'Change 5678 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
  331. return true;
  332. };
  333. $this->processExecutor->expects($this->once())
  334. ->method('execute')
  335. ->with($this->equalTo($expectedCommand))
  336. ->will($this->returnCallback($expectedCallback));
  337. $branches = $this->perforce->getBranches();
  338. $this->assertEquals('//depot@5678', $branches['master']);
  339. }
  340. public function testGetTagsWithoutStream()
  341. {
  342. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port labels';
  343. $this->processExecutor->expects($this->at(0))
  344. ->method('execute')
  345. ->with($this->equalTo($expectedCommand))
  346. ->will(
  347. $this->returnCallback(
  348. function ($command, &$output) {
  349. $output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL;
  350. return true;
  351. }
  352. )
  353. );
  354. $tags = $this->perforce->getTags();
  355. $this->assertEquals('//depot@0.0.1', $tags['0.0.1']);
  356. $this->assertEquals('//depot@0.0.2', $tags['0.0.2']);
  357. }
  358. public function testGetTagsWithStream()
  359. {
  360. $this->setPerforceToStream();
  361. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port labels';
  362. $this->processExecutor->expects($this->at(0))
  363. ->method('execute')
  364. ->with($this->equalTo($expectedCommand))
  365. ->will(
  366. $this->returnCallback(
  367. function ($command, &$output) {
  368. $output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL;
  369. return true;
  370. }
  371. )
  372. );
  373. $tags = $this->perforce->getTags();
  374. $this->assertEquals('//depot/branch@0.0.1', $tags['0.0.1']);
  375. $this->assertEquals('//depot/branch@0.0.2', $tags['0.0.2']);
  376. }
  377. public function testCheckStreamWithoutStream()
  378. {
  379. $result = $this->perforce->checkStream('depot');
  380. $this->assertFalse($result);
  381. $this->assertFalse($this->perforce->isStream());
  382. }
  383. public function testCheckStreamWithStream()
  384. {
  385. $this->processExecutor->expects($this->any())->method('execute')
  386. ->will(
  387. $this->returnCallback(
  388. function ($command, &$output) {
  389. $output = 'Depot depot 2013/06/25 stream /p4/1/depots/depot/... \'Created by Me\'';
  390. return true;
  391. }
  392. )
  393. );
  394. $result = $this->perforce->checkStream('depot');
  395. $this->assertTrue($result);
  396. $this->assertTrue($this->perforce->isStream());
  397. }
  398. public function testGetComposerInformationWithoutLabelWithoutStream()
  399. {
  400. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json';
  401. $this->processExecutor->expects($this->at(0))
  402. ->method('execute')
  403. ->with($this->equalTo($expectedCommand))
  404. ->will(
  405. $this->returnCallback(
  406. function ($command, &$output) {
  407. $output = PerforceTest::getComposerJson();
  408. return true;
  409. }
  410. )
  411. );
  412. $result = $this->perforce->getComposerInformation('//depot');
  413. $expected = array(
  414. 'name' => 'test/perforce',
  415. 'description' => 'Basic project for testing',
  416. 'minimum-stability' => 'dev',
  417. 'autoload' => array('psr-0' => array())
  418. );
  419. $this->assertEquals($expected, $result);
  420. }
  421. public function testGetComposerInformationWithLabelWithoutStream()
  422. {
  423. $expectedCommand = 'p4 -u user -p port files //depot/composer.json@0.0.1';
  424. $this->processExecutor->expects($this->at(0))
  425. ->method('execute')
  426. ->with($this->equalTo($expectedCommand))
  427. ->will(
  428. $this->returnCallback(
  429. function ($command, &$output) {
  430. $output = '//depot/composer.json#1 - branch change 10001 (text)';
  431. return true;
  432. }
  433. )
  434. );
  435. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001';
  436. $this->processExecutor->expects($this->at(1))
  437. ->method('execute')
  438. ->with($this->equalTo($expectedCommand))
  439. ->will(
  440. $this->returnCallback(
  441. function ($command, &$output) {
  442. $output = PerforceTest::getComposerJson();
  443. return true;
  444. }
  445. )
  446. );
  447. $result = $this->perforce->getComposerInformation('//depot@0.0.1');
  448. $expected = array(
  449. 'name' => 'test/perforce',
  450. 'description' => 'Basic project for testing',
  451. 'minimum-stability' => 'dev',
  452. 'autoload' => array('psr-0' => array())
  453. );
  454. $this->assertEquals($expected, $result);
  455. }
  456. public function testGetComposerInformationWithoutLabelWithStream()
  457. {
  458. $this->setPerforceToStream();
  459. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json';
  460. $this->processExecutor->expects($this->at(0))
  461. ->method('execute')
  462. ->with($this->equalTo($expectedCommand))
  463. ->will(
  464. $this->returnCallback(
  465. function ($command, &$output) {
  466. $output = PerforceTest::getComposerJson();
  467. return true;
  468. }
  469. )
  470. );
  471. $result = $this->perforce->getComposerInformation('//depot/branch');
  472. $expected = array(
  473. 'name' => 'test/perforce',
  474. 'description' => 'Basic project for testing',
  475. 'minimum-stability' => 'dev',
  476. 'autoload' => array('psr-0' => array())
  477. );
  478. $this->assertEquals($expected, $result);
  479. }
  480. public function testGetComposerInformationWithLabelWithStream()
  481. {
  482. $this->setPerforceToStream();
  483. $expectedCommand = 'p4 -u user -p port files //depot/branch/composer.json@0.0.1';
  484. $this->processExecutor->expects($this->at(0))
  485. ->method('execute')
  486. ->with($this->equalTo($expectedCommand))
  487. ->will(
  488. $this->returnCallback(
  489. function ($command, &$output) {
  490. $output = '//depot/composer.json#1 - branch change 10001 (text)';
  491. return true;
  492. }
  493. )
  494. );
  495. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001';
  496. $this->processExecutor->expects($this->at(1))
  497. ->method('execute')
  498. ->with($this->equalTo($expectedCommand))
  499. ->will(
  500. $this->returnCallback(
  501. function ($command, &$output) {
  502. $output = PerforceTest::getComposerJson();
  503. return true;
  504. }
  505. )
  506. );
  507. $result = $this->perforce->getComposerInformation('//depot/branch@0.0.1');
  508. $expected = array(
  509. 'name' => 'test/perforce',
  510. 'description' => 'Basic project for testing',
  511. 'minimum-stability' => 'dev',
  512. 'autoload' => array('psr-0' => array())
  513. );
  514. $this->assertEquals($expected, $result);
  515. }
  516. public function testSyncCodeBaseWithoutStream()
  517. {
  518. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label';
  519. $this->processExecutor->expects($this->at(0))
  520. ->method('execute')
  521. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  522. ->will($this->returnValue(0));
  523. $this->perforce->syncCodeBase('label');
  524. }
  525. public function testSyncCodeBaseWithStream()
  526. {
  527. $this->setPerforceToStream();
  528. $expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label';
  529. $this->processExecutor->expects($this->at(0))
  530. ->method('execute')
  531. ->with($this->equalTo($expectedCommand))
  532. ->will($this->returnValue(0));
  533. $this->perforce->syncCodeBase('label');
  534. }
  535. public function testCheckServerExists()
  536. {
  537. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  538. $expectedCommand = 'p4 -p perforce.does.exist:port info -s';
  539. $processExecutor->expects($this->at(0))
  540. ->method('execute')
  541. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  542. ->will($this->returnValue(0));
  543. $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
  544. $this->assertTrue($result);
  545. }
  546. /**
  547. * Test if "p4" command is missing.
  548. *
  549. * @covers \Composer\Util\Perforce::checkServerExists
  550. *
  551. * @return void
  552. */
  553. public function testCheckServerClientError()
  554. {
  555. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  556. $expectedCommand = 'p4 -p perforce.does.exist:port info -s';
  557. $processExecutor->expects($this->at(0))
  558. ->method('execute')
  559. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  560. ->will($this->returnValue(127));
  561. $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
  562. $this->assertFalse($result);
  563. }
  564. public static function getComposerJson()
  565. {
  566. $composer_json = array(
  567. '{',
  568. '"name": "test/perforce",',
  569. '"description": "Basic project for testing",',
  570. '"minimum-stability": "dev",',
  571. '"autoload": {',
  572. '"psr-0" : {',
  573. '}',
  574. '}',
  575. '}'
  576. );
  577. return implode($composer_json);
  578. }
  579. private function getExpectedClientSpec($withStream)
  580. {
  581. $expectedArray = array(
  582. 'Client: composer_perforce_TEST_depot',
  583. PHP_EOL,
  584. 'Update:',
  585. PHP_EOL,
  586. 'Access:',
  587. 'Owner: user',
  588. PHP_EOL,
  589. 'Description:',
  590. ' Created by user from composer.',
  591. PHP_EOL,
  592. 'Root: path',
  593. PHP_EOL,
  594. 'Options: noallwrite noclobber nocompress unlocked modtime rmdir',
  595. PHP_EOL,
  596. 'SubmitOptions: revertunchanged',
  597. PHP_EOL,
  598. 'LineEnd: local',
  599. PHP_EOL
  600. );
  601. if ($withStream) {
  602. $expectedArray[] = 'Stream:';
  603. $expectedArray[] = ' //depot/branch';
  604. } else {
  605. $expectedArray[] = 'View: //depot/... //composer_perforce_TEST_depot/...';
  606. }
  607. return $expectedArray;
  608. }
  609. private function setPerforceToStream()
  610. {
  611. $this->perforce->setStream('//depot/branch');
  612. }
  613. public function testCleanupClientSpecShouldDeleteClient()
  614. {
  615. $fs = $this->getMock('Composer\Util\Filesystem');
  616. $this->perforce->setFilesystem($fs);
  617. $testClient = $this->perforce->getClient();
  618. $expectedCommand = 'p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . $testClient;
  619. $this->processExecutor->expects($this->once())->method('execute')->with($this->equalTo($expectedCommand));
  620. $fs->expects($this->once())->method('remove')->with($this->perforce->getP4ClientSpec());
  621. $this->perforce->cleanupClientSpec();
  622. }
  623. }