PerforceTest.php 22 KB

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