PerforceTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. * Contributor: Matt Whittom <Matt.Whittom@veteransunited.com>
  9. * Date: 7/17/13
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace Composer\Test\Util;
  15. use Composer\Util\Perforce;
  16. use Composer\Util\ProcessExecutor;
  17. /**
  18. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  19. */
  20. class PerforceTest extends \PHPUnit_Framework_TestCase {
  21. protected $perforce;
  22. protected $processExecutor;
  23. public function setUp() {
  24. $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  25. $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user");
  26. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST");
  27. }
  28. public function testGetClientWithoutStream() {
  29. $client = $this->perforce->getClient();
  30. $hostname = gethostname();
  31. $timestamp = time();
  32. $expected = "composer_perforce_TEST_depot";
  33. $this->assertEquals($expected, $client);
  34. }
  35. public function testGetClientFromStream() {
  36. $this->setPerforceToStream();
  37. $client = $this->perforce->getClient();
  38. $expected = "composer_perforce_TEST_depot_branch";
  39. $this->assertEquals($expected, $client);
  40. }
  41. public function testGetStreamWithoutStream() {
  42. $stream = $this->perforce->getStream();
  43. $this->assertEquals("//depot", $stream);
  44. }
  45. public function testGetStreamWithStream() {
  46. $this->setPerforceToStream();
  47. $stream = $this->perforce->getStream();
  48. $this->assertEquals("//depot/branch", $stream);
  49. }
  50. public function testGetStreamWithoutLabelWithStreamWithoutLabel(){
  51. $stream = $this->perforce->getStreamWithoutLabel("//depot/branch");
  52. $this->assertEquals("//depot/branch", $stream);
  53. }
  54. public function testGetStreamWithoutLabelWithStreamWithLabel(){
  55. $stream = $this->perforce->getStreamWithoutLabel("//depot/branching@label");
  56. $this->assertEquals("//depot/branching", $stream);
  57. }
  58. public function testGetClientSpec() {
  59. $clientSpec = $this->perforce->getP4ClientSpec();
  60. $expected = "path/composer_perforce_TEST_depot.p4.spec";
  61. $this->assertEquals($expected, $clientSpec);
  62. }
  63. public function testGenerateP4Command() {
  64. $command = "do something";
  65. $p4Command = $this->perforce->generateP4Command($command);
  66. $expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something";
  67. $this->assertEquals($expected, $p4Command);
  68. }
  69. public function testQueryP4UserWithUserAlreadySet(){
  70. $io = $this->getMock('Composer\IO\IOInterface');
  71. $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"TEST_USER");
  72. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST");
  73. $this->perforce->queryP4user($io);
  74. $this->assertEquals("TEST_USER", $this->perforce->getUser());
  75. }
  76. public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS(){
  77. $repoConfig = array("depot"=>"depot", "branch"=>"branch");
  78. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST");
  79. $io = $this->getMock('Composer\IO\IOInterface');
  80. $expectedCommand = "p4 set";
  81. $this->processExecutor->expects($this->at(0))
  82. ->method('execute')
  83. ->with($this->equalTo($expectedCommand))
  84. ->will($this->returnCallback(function($command, &$output) {$output = "P4USER=TEST_P4VARIABLE_USER\n"; return true;}));
  85. $this->perforce->queryP4user($io);
  86. $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser());
  87. }
  88. public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS(){
  89. $repoConfig = array("depot"=>"depot", "branch"=>"branch");
  90. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
  91. $io = $this->getMock('Composer\IO\IOInterface');
  92. $expectedCommand = 'echo $P4USER';
  93. $this->processExecutor->expects($this->at(0))
  94. ->method('execute')
  95. ->with($this->equalTo($expectedCommand))
  96. ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_USER\n"; return true;}));
  97. $this->perforce->queryP4user($io);
  98. $this->assertEquals("TEST_P4VARIABLE_USER", $this->perforce->getUser());
  99. }
  100. public function testQueryP4UserQueriesForUser(){
  101. $repoConfig = array("depot"=>"depot", "branch"=>"branch");
  102. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
  103. $io = $this->getMock('Composer\IO\IOInterface');
  104. $expectedQuestion = "Enter P4 User:";
  105. $io->expects($this->at(0))
  106. ->method('ask')
  107. ->with($this->equalTo($expectedQuestion))
  108. ->will($this->returnValue("TEST_QUERY_USER"));
  109. $this->perforce->queryP4user($io);
  110. $this->assertEquals("TEST_QUERY_USER", $this->perforce->getUser());
  111. }
  112. public function testQueryP4UserStoresResponseToQueryForUserWithWindows(){
  113. $repoConfig = array("depot"=>"depot", "branch"=>"branch");
  114. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST");
  115. $io = $this->getMock('Composer\IO\IOInterface');
  116. $expectedQuestion = "Enter P4 User:";
  117. $io->expects($this->at(0))
  118. ->method('ask')
  119. ->with($this->equalTo($expectedQuestion))
  120. ->will($this->returnValue("TEST_QUERY_USER"));
  121. $expectedCommand = "p4 set P4USER=TEST_QUERY_USER";
  122. $this->processExecutor->expects($this->at(1))
  123. ->method('execute')
  124. ->with($this->equalTo($expectedCommand))
  125. ->will($this->returnValue(0));
  126. $this->perforce->queryP4user($io);
  127. }
  128. public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows(){
  129. $repoConfig = array("depot"=>"depot", "branch"=>"branch");
  130. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
  131. $io = $this->getMock('Composer\IO\IOInterface');
  132. $expectedQuestion = "Enter P4 User:";
  133. $io->expects($this->at(0))
  134. ->method('ask')
  135. ->with($this->equalTo($expectedQuestion))
  136. ->will($this->returnValue("TEST_QUERY_USER"));
  137. $expectedCommand = "export P4USER=TEST_QUERY_USER";
  138. $this->processExecutor->expects($this->at(1))
  139. ->method('execute')
  140. ->with($this->equalTo($expectedCommand))
  141. ->will($this->returnValue(0));
  142. $this->perforce->queryP4user($io);
  143. }
  144. public function testQueryP4PasswordWithPasswordAlreadySet(){
  145. $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "p4password"=>"TEST_PASSWORD");
  146. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
  147. $io = $this->getMock('Composer\IO\IOInterface');
  148. $password = $this->perforce->queryP4Password($io);
  149. $this->assertEquals("TEST_PASSWORD", $password);
  150. }
  151. public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS(){
  152. $io = $this->getMock('Composer\IO\IOInterface');
  153. $expectedCommand = "p4 set";
  154. $this->processExecutor->expects($this->at(0))
  155. ->method('execute')
  156. ->with($this->equalTo($expectedCommand))
  157. ->will($this->returnCallback(function($command, &$output) {$output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; return true;}));
  158. $password = $this->perforce->queryP4Password($io);
  159. $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password);
  160. }
  161. public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS(){
  162. $repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user");
  163. $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
  164. $io = $this->getMock('Composer\IO\IOInterface');
  165. $expectedCommand = 'echo $P4PASSWD';
  166. $this->processExecutor->expects($this->at(0))
  167. ->method('execute')
  168. ->with($this->equalTo($expectedCommand))
  169. ->will($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_PASSWORD\n"; return true;}));
  170. $password = $this->perforce->queryP4Password($io);
  171. $this->assertEquals("TEST_P4VARIABLE_PASSWORD", $password);
  172. }
  173. public function testQueryP4PasswordQueriesForPassword(){
  174. $io = $this->getMock('Composer\IO\IOInterface');
  175. $expectedQuestion = "Enter password for Perforce user user: ";
  176. $io->expects($this->at(0))
  177. ->method('askAndHideAnswer')
  178. ->with($this->equalTo($expectedQuestion))
  179. ->will($this->returnValue("TEST_QUERY_PASSWORD"));
  180. $password = $this->perforce->queryP4Password($io);
  181. $this->assertEquals("TEST_QUERY_PASSWORD", $password);
  182. }
  183. public function testWriteP4ClientSpecWithoutStream() {
  184. $stream = fopen("php://memory", 'w+');
  185. $this->perforce->writeClientSpecToFile($stream);
  186. rewind($stream);
  187. $expectedArray = $this->getExpectedClientSpec(FALSE);
  188. try {
  189. foreach ($expectedArray as $expected) {
  190. $this->assertStringStartsWith($expected, fgets($stream));
  191. }
  192. $this->assertFalse(fgets($stream));
  193. } catch (Exception $e) {
  194. fclose($stream);
  195. throw $e;
  196. }
  197. fclose($stream);
  198. }
  199. public function testWriteP4ClientSpecWithStream() {
  200. $this->setPerforceToStream();
  201. $stream = fopen("php://memory", 'w+');
  202. $this->perforce->writeClientSpecToFile($stream);
  203. rewind($stream);
  204. $expectedArray = $this->getExpectedClientSpec(TRUE);
  205. try {
  206. foreach ($expectedArray as $expected) {
  207. $this->assertStringStartsWith($expected, fgets($stream));
  208. }
  209. $this->assertFalse(fgets($stream));
  210. } catch (Exception $e) {
  211. fclose($stream);
  212. throw $e;
  213. }
  214. fclose($stream);
  215. }
  216. public function testIsLoggedIn() {
  217. $expectedCommand = "p4 -u user -p port login -s";
  218. $this->processExecutor->expects($this->at(0))
  219. ->method('execute')
  220. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  221. ->will($this->returnValue(0));
  222. $this->perforce->isLoggedIn();
  223. }
  224. public function testConnectClient() {
  225. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec";
  226. $this->processExecutor->expects($this->at(0))
  227. ->method('execute')
  228. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  229. ->will($this->returnValue(0));
  230. $this->perforce->connectClient();
  231. }
  232. public function testGetBranchesWithStream() {
  233. $this->setPerforceToStream();
  234. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/...";
  235. $this->processExecutor->expects($this->at(0))
  236. ->method('execute')
  237. ->with($this->equalTo($expectedCommand))
  238. ->will($this->returnCallback(function($command, &$output) {$output = "Stream //depot/branch mainline none 'branch'\n"; return true;}));
  239. $branches = $this->perforce->getBranches();
  240. $this->assertEquals("//depot/branch", $branches['master']);
  241. }
  242. public function testGetBranchesWithoutStream() {
  243. $branches = $this->perforce->getBranches();
  244. $this->assertEquals("//depot", $branches['master']);
  245. }
  246. public function testGetTagsWithoutStream() {
  247. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port labels";
  248. $this->processExecutor->expects($this->at(0))
  249. ->method('execute')
  250. ->with($this->equalTo($expectedCommand))
  251. ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;}));
  252. $tags = $this->perforce->getTags();
  253. $this->assertEquals("//depot@0.0.1", $tags['0.0.1']);
  254. $this->assertEquals("//depot@0.0.2", $tags['0.0.2']);
  255. }
  256. public function testGetTagsWithStream() {
  257. $this->setPerforceToStream();
  258. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port labels";
  259. $this->processExecutor->expects($this->at(0))
  260. ->method('execute')
  261. ->with($this->equalTo($expectedCommand))
  262. ->will($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;}));
  263. $tags = $this->perforce->getTags();
  264. $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']);
  265. $this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']);
  266. }
  267. public function testCheckStreamWithoutStream() {
  268. $result = $this->perforce->checkStream("depot");
  269. $this->assertFalse($result);
  270. $this->assertFalse($this->perforce->isStream());
  271. }
  272. public function testCheckStreamWithStream() {
  273. $this->processExecutor->expects($this->any())->method('execute')
  274. ->will($this->returnCallback(function($command, &$output) {$output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; return true;}));
  275. $result = $this->perforce->checkStream("depot");
  276. $this->assertTrue($result);
  277. $this->assertTrue($this->perforce->isStream());
  278. }
  279. public function testGetComposerInformationWithoutLabelWithoutStream() {
  280. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json";
  281. $this->processExecutor->expects($this->at(0))
  282. ->method('execute')
  283. ->with($this->equalTo($expectedCommand))
  284. ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;}));
  285. $result = $this->perforce->getComposerInformation("//depot");
  286. $expected = array(
  287. "name" => "test/perforce",
  288. "description" => "Basic project for testing",
  289. "minimum-stability" => "dev",
  290. "autoload" => array("psr-0" => array())
  291. );
  292. $this->assertEquals($expected, $result);
  293. }
  294. public function testGetComposerInformationWithLabelWithoutStream() {
  295. $expectedCommand = "p4 -u user -p port files //depot/composer.json@0.0.1";
  296. $this->processExecutor->expects($this->at(0))
  297. ->method('execute')
  298. ->with($this->equalTo($expectedCommand))
  299. ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;}));
  300. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001";
  301. $this->processExecutor->expects($this->at(1))
  302. ->method('execute')
  303. ->with($this->equalTo($expectedCommand))
  304. ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;}));
  305. $result = $this->perforce->getComposerInformation("//depot@0.0.1");
  306. $expected = array(
  307. "name" => "test/perforce",
  308. "description" => "Basic project for testing",
  309. "minimum-stability" => "dev",
  310. "autoload" => array("psr-0" => array())
  311. );
  312. $this->assertEquals($expected, $result);
  313. }
  314. public function testGetComposerInformationWithoutLabelWithStream() {
  315. $this->setPerforceToStream();
  316. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json";
  317. $this->processExecutor->expects($this->at(0))
  318. ->method('execute')
  319. ->with($this->equalTo($expectedCommand))
  320. ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;}));
  321. $result = $this->perforce->getComposerInformation("//depot/branch");
  322. $expected = array(
  323. "name" => "test/perforce",
  324. "description" => "Basic project for testing",
  325. "minimum-stability" => "dev",
  326. "autoload" => array("psr-0" => array())
  327. );
  328. $this->assertEquals($expected, $result);
  329. }
  330. public function testGetComposerInformationWithLabelWithStream() {
  331. $this->setPerforceToStream();
  332. $expectedCommand = "p4 -u user -p port files //depot/branch/composer.json@0.0.1";
  333. $this->processExecutor->expects($this->at(0))
  334. ->method('execute')
  335. ->with($this->equalTo($expectedCommand))
  336. ->will($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;}));
  337. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001";
  338. $this->processExecutor->expects($this->at(1))
  339. ->method('execute')
  340. ->with($this->equalTo($expectedCommand))
  341. ->will($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;}));
  342. $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1");
  343. $expected = array(
  344. "name" => "test/perforce",
  345. "description" => "Basic project for testing",
  346. "minimum-stability" => "dev",
  347. "autoload" => array("psr-0" => array())
  348. );
  349. $this->assertEquals($expected, $result);
  350. }
  351. public function testSyncCodeBaseWithoutStream() {
  352. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label";
  353. $this->processExecutor->expects($this->at(1))
  354. ->method('execute')
  355. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  356. ->will($this->returnValue(0));
  357. $this->perforce->syncCodeBase("label");
  358. }
  359. public function testSyncCodeBaseWithStream() {
  360. $this->setPerforceToStream();
  361. $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label";
  362. $this->processExecutor->expects($this->at(1))
  363. ->method('execute')
  364. ->with($this->equalTo($expectedCommand))
  365. ->will($this->returnValue(0));
  366. $this->perforce->syncCodeBase("label");
  367. }
  368. public function testCheckServerExists() {
  369. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  370. $expectedCommand = "p4 -p perforce.does.exist:port info -s";
  371. $processExecutor->expects($this->at(0))
  372. ->method('execute')
  373. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  374. ->will($this->returnValue(0));
  375. $result = $this->perforce->checkServerExists("perforce.does.exist:port", $processExecutor);
  376. $this->assertTrue($result);
  377. }
  378. public function testCheckServerExistsWithFailure() {
  379. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  380. $expectedCommand = "p4 -p perforce.does.not.exist:port info -s";
  381. $processExecutor->expects($this->at(0))
  382. ->method('execute')
  383. ->with($this->equalTo($expectedCommand), $this->equalTo(null))
  384. ->will($this->returnValue("Perforce client error:"));
  385. $result = $this->perforce->checkServerExists("perforce.does.not.exist:port", $processExecutor);
  386. $this->assertTrue($result);
  387. }
  388. public static function getComposerJson() {
  389. $composer_json = array(
  390. '{',
  391. '"name": "test/perforce",',
  392. '"description": "Basic project for testing",',
  393. '"minimum-stability": "dev",',
  394. '"autoload": {',
  395. '"psr-0" : {',
  396. '}',
  397. '}',
  398. '}'
  399. );
  400. return implode($composer_json);
  401. }
  402. private function getExpectedClientSpec($withStream) {
  403. $expectedArray = array(
  404. "Client: composer_perforce_TEST_depot",
  405. "\n",
  406. "Update:",
  407. "\n",
  408. "Access:",
  409. "Owner: user",
  410. "\n",
  411. "Description:",
  412. " Created by user from composer.",
  413. "\n",
  414. "Root: path",
  415. "\n",
  416. "Options: noallwrite noclobber nocompress unlocked modtime rmdir",
  417. "\n",
  418. "SubmitOptions: revertunchanged",
  419. "\n",
  420. "LineEnd: local",
  421. "\n"
  422. );
  423. if ($withStream) {
  424. $expectedArray[] = "Stream:";
  425. $expectedArray[] = " //depot/branch";
  426. }
  427. else {
  428. $expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/...";
  429. }
  430. return $expectedArray;
  431. }
  432. private function setPerforceToStream(){
  433. $this->perforce->setStream("//depot/branch");
  434. }
  435. }