PerforceTest.php 25 KB

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