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