ShellTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. /**
  3. * ShellTest file
  4. *
  5. * Test Case for Shell
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2006-2010, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package cake.tests.cases.console.libs
  18. * @since CakePHP v 1.2.0.7726
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('ShellDispatcher', 'Console');
  22. App::uses('Shell', 'Console');
  23. App::uses('Folder', 'Utility');
  24. /**
  25. * ShellTestShell class
  26. *
  27. * @package cake.tests.cases.console.libs
  28. */
  29. class ShellTestShell extends Shell {
  30. /**
  31. * name property
  32. *
  33. * @var name
  34. * @access public
  35. */
  36. public $name = 'ShellTestShell';
  37. /**
  38. * stopped property
  39. *
  40. * @var integer
  41. * @access public
  42. */
  43. public $stopped;
  44. /**
  45. * stop method
  46. *
  47. * @param integer $status
  48. * @return void
  49. */
  50. protected function _stop($status = 0) {
  51. $this->stopped = $status;
  52. }
  53. public function do_something() {
  54. }
  55. public function _secret() {
  56. }
  57. protected function no_access() {
  58. }
  59. public function mergeVars($properties, $class, $normalize = true) {
  60. return $this->_mergeVars($properties, $class, $normalize);
  61. }
  62. }
  63. /**
  64. * Class for testing merging vars
  65. *
  66. * @package cake.tests.cases.console
  67. */
  68. class TestMergeShell extends Shell {
  69. var $tasks = array('DbConfig', 'Fixture');
  70. var $uses = array('Comment');
  71. }
  72. /**
  73. * TestAppleTask class
  74. *
  75. * @package cake.tests.cases.console.libs
  76. */
  77. class TestAppleTask extends Shell {
  78. }
  79. /**
  80. * TestBananaTask class
  81. *
  82. * @package cake.tests.cases.console.libs
  83. */
  84. class TestBananaTask extends Shell {
  85. }
  86. /**
  87. * ShellTest class
  88. *
  89. * @package cake.tests.cases.console.libs
  90. */
  91. class ShellTest extends CakeTestCase {
  92. /**
  93. * Fixtures used in this test case
  94. *
  95. * @var array
  96. * @access public
  97. */
  98. public $fixtures = array(
  99. 'core.post', 'core.comment', 'core.article', 'core.user',
  100. 'core.tag', 'core.articles_tag', 'core.attachment'
  101. );
  102. /**
  103. * setUp method
  104. *
  105. * @return void
  106. */
  107. public function setUp() {
  108. parent::setUp();
  109. $output = $this->getMock('ConsoleOutput', array(), array(), '', false);
  110. $error = $this->getMock('ConsoleOutput', array(), array(), '', false);
  111. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  112. $this->Shell = new ShellTestShell($output, $error, $in);
  113. }
  114. /**
  115. * testConstruct method
  116. *
  117. * @return void
  118. */
  119. public function testConstruct() {
  120. $this->assertEqual($this->Shell->name, 'ShellTestShell');
  121. $this->assertInstanceOf('ConsoleInput', $this->Shell->stdin);
  122. $this->assertInstanceOf('ConsoleOutput', $this->Shell->stdout);
  123. $this->assertInstanceOf('ConsoleOutput', $this->Shell->stderr);
  124. }
  125. /**
  126. * test merging vars
  127. *
  128. * @return void
  129. */
  130. function testMergeVars() {
  131. $this->Shell->tasks = array('DbConfig' => array('one', 'two'));
  132. $this->Shell->uses = array('Posts');
  133. $this->Shell->mergeVars(array('tasks'), 'TestMergeShell');
  134. $this->Shell->mergeVars(array('uses'), 'TestMergeShell', false);
  135. $expected = array('DbConfig' => null, 'Fixture' => null, 'DbConfig' => array('one', 'two'));
  136. $this->assertEquals($expected, $this->Shell->tasks);
  137. $expected = array('Fixture' => null, 'DbConfig' => array('one', 'two'));
  138. $this->assertEquals($expected, Set::normalize($this->Shell->tasks), 'Normalized results are wrong.');
  139. $this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.');
  140. }
  141. /**
  142. * testInitialize method
  143. *
  144. * @return void
  145. */
  146. public function testInitialize() {
  147. App::build(array(
  148. 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  149. 'models' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'Model' . DS)
  150. ), true);
  151. $this->Shell->uses = array('TestPlugin.TestPluginPost');
  152. $this->Shell->initialize();
  153. $this->assertTrue(isset($this->Shell->TestPluginPost));
  154. $this->assertInstanceOf('TestPluginPost', $this->Shell->TestPluginPost);
  155. $this->assertEqual($this->Shell->modelClass, 'TestPluginPost');
  156. $this->Shell->uses = array('Comment');
  157. $this->Shell->initialize();
  158. $this->assertTrue(isset($this->Shell->Comment));
  159. $this->assertInstanceOf('Comment', $this->Shell->Comment);
  160. $this->assertEqual($this->Shell->modelClass, 'Comment');
  161. App::build();
  162. }
  163. /**
  164. * testIn method
  165. *
  166. * @return void
  167. */
  168. public function testIn() {
  169. $this->Shell->stdin->expects($this->at(0))
  170. ->method('read')
  171. ->will($this->returnValue('n'));
  172. $this->Shell->stdin->expects($this->at(1))
  173. ->method('read')
  174. ->will($this->returnValue('Y'));
  175. $this->Shell->stdin->expects($this->at(2))
  176. ->method('read')
  177. ->will($this->returnValue('y'));
  178. $this->Shell->stdin->expects($this->at(3))
  179. ->method('read')
  180. ->will($this->returnValue('y'));
  181. $this->Shell->stdin->expects($this->at(4))
  182. ->method('read')
  183. ->will($this->returnValue('y'));
  184. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  185. $this->assertEqual($result, 'n');
  186. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  187. $this->assertEqual($result, 'Y');
  188. $result = $this->Shell->in('Just a test?', 'y,n', 'n');
  189. $this->assertEqual($result, 'y');
  190. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  191. $this->assertEqual($result, 'y');
  192. $result = $this->Shell->in('Just a test?', 'y', 'y');
  193. $this->assertEqual($result, 'y');
  194. $this->Shell->interactive = false;
  195. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  196. $this->assertEqual($result, 'n');
  197. }
  198. /**
  199. * testOut method
  200. *
  201. * @return void
  202. */
  203. public function testOut() {
  204. $this->Shell->stdout->expects($this->at(0))
  205. ->method('write')
  206. ->with("Just a test", 1);
  207. $this->Shell->stdout->expects($this->at(1))
  208. ->method('write')
  209. ->with(array('Just', 'a', 'test'), 1);
  210. $this->Shell->stdout->expects($this->at(2))
  211. ->method('write')
  212. ->with(array('Just', 'a', 'test'), 2);
  213. $this->Shell->stdout->expects($this->at(3))
  214. ->method('write')
  215. ->with('', 1);
  216. $this->Shell->out('Just a test');
  217. $this->Shell->out(array('Just', 'a', 'test'));
  218. $this->Shell->out(array('Just', 'a', 'test'), 2);
  219. $this->Shell->out();
  220. }
  221. /**
  222. * test that verbose and quiet output levels work
  223. *
  224. * @return void
  225. */
  226. function testVerboseOutput() {
  227. $this->Shell->stdout->expects($this->at(0))->method('write')
  228. ->with('Verbose', 1);
  229. $this->Shell->stdout->expects($this->at(1))->method('write')
  230. ->with('Normal', 1);
  231. $this->Shell->stdout->expects($this->at(2))->method('write')
  232. ->with('Quiet', 1);
  233. $this->Shell->params['verbose'] = true;
  234. $this->Shell->params['quiet'] = false;
  235. $this->Shell->out('Verbose', 1, Shell::VERBOSE);
  236. $this->Shell->out('Normal', 1, Shell::NORMAL);
  237. $this->Shell->out('Quiet', 1, Shell::QUIET);
  238. }
  239. /**
  240. * test that verbose and quiet output levels work
  241. *
  242. * @return void
  243. */
  244. function testQuietOutput() {
  245. $this->Shell->stdout->expects($this->once())->method('write')
  246. ->with('Quiet', 1);
  247. $this->Shell->params['verbose'] = false;
  248. $this->Shell->params['quiet'] = true;
  249. $this->Shell->out('Verbose', 1, Shell::VERBOSE);
  250. $this->Shell->out('Normal', 1, Shell::NORMAL);
  251. $this->Shell->out('Quiet', 1, Shell::QUIET);
  252. }
  253. /**
  254. * testErr method
  255. *
  256. * @return void
  257. */
  258. public function testErr() {
  259. $this->Shell->stderr->expects($this->at(0))
  260. ->method('write')
  261. ->with("Just a test", 1);
  262. $this->Shell->stderr->expects($this->at(1))
  263. ->method('write')
  264. ->with(array('Just', 'a', 'test'), 1);
  265. $this->Shell->stderr->expects($this->at(2))
  266. ->method('write')
  267. ->with(array('Just', 'a', 'test'), 2);
  268. $this->Shell->stderr->expects($this->at(3))
  269. ->method('write')
  270. ->with('', 1);
  271. $this->Shell->err('Just a test');
  272. $this->Shell->err(array('Just', 'a', 'test'));
  273. $this->Shell->err(array('Just', 'a', 'test'), 2);
  274. $this->Shell->err();
  275. }
  276. /**
  277. * testNl
  278. *
  279. * @return void
  280. */
  281. public function testNl() {
  282. $this->assertEqual($this->Shell->nl(), "\n");
  283. $this->assertEqual($this->Shell->nl(true), "\n");
  284. $this->assertEqual($this->Shell->nl(false), "");
  285. $this->assertEqual($this->Shell->nl(2), "\n\n");
  286. $this->assertEqual($this->Shell->nl(1), "\n");
  287. }
  288. /**
  289. * testHr
  290. *
  291. * @return void
  292. */
  293. public function testHr() {
  294. $bar = '---------------------------------------------------------------';
  295. $this->Shell->stdout->expects($this->at(0))->method('write')->with('', 0);
  296. $this->Shell->stdout->expects($this->at(1))->method('write')->with($bar, 1);
  297. $this->Shell->stdout->expects($this->at(2))->method('write')->with('', 0);
  298. $this->Shell->stdout->expects($this->at(3))->method('write')->with("", true);
  299. $this->Shell->stdout->expects($this->at(4))->method('write')->with($bar, 1);
  300. $this->Shell->stdout->expects($this->at(5))->method('write')->with("", true);
  301. $this->Shell->stdout->expects($this->at(6))->method('write')->with("", 2);
  302. $this->Shell->stdout->expects($this->at(7))->method('write')->with($bar, 1);
  303. $this->Shell->stdout->expects($this->at(8))->method('write')->with("", 2);
  304. $this->Shell->hr();
  305. $this->Shell->hr(true);
  306. $this->Shell->hr(2);
  307. }
  308. /**
  309. * testError
  310. *
  311. * @return void
  312. */
  313. public function testError() {
  314. $this->Shell->stderr->expects($this->at(0))
  315. ->method('write')
  316. ->with("<error>Error:</error> Foo Not Found", 1);
  317. $this->Shell->stderr->expects($this->at(1))
  318. ->method('write')
  319. ->with("<error>Error:</error> Foo Not Found", 1);
  320. $this->Shell->stderr->expects($this->at(2))
  321. ->method('write')
  322. ->with("Searched all...", 1);
  323. $this->Shell->error('Foo Not Found');
  324. $this->assertIdentical($this->Shell->stopped, 1);
  325. $this->Shell->stopped = null;
  326. $this->Shell->error('Foo Not Found', 'Searched all...');
  327. $this->assertIdentical($this->Shell->stopped, 1);
  328. }
  329. /**
  330. * testLoadTasks method
  331. *
  332. * @return void
  333. */
  334. public function testLoadTasks() {
  335. $this->assertTrue($this->Shell->loadTasks());
  336. $this->Shell->tasks = null;
  337. $this->assertTrue($this->Shell->loadTasks());
  338. $this->Shell->tasks = false;
  339. $this->assertTrue($this->Shell->loadTasks());
  340. $this->Shell->tasks = true;
  341. $this->assertTrue($this->Shell->loadTasks());
  342. $this->Shell->tasks = array();
  343. $this->assertTrue($this->Shell->loadTasks());
  344. $this->Shell->tasks = array('TestApple');
  345. $this->assertTrue($this->Shell->loadTasks());
  346. $this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
  347. $this->Shell->tasks = 'TestBanana';
  348. $this->assertTrue($this->Shell->loadTasks());
  349. $this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
  350. $this->assertInstanceOf('TestBananaTask', $this->Shell->TestBanana);
  351. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  352. $this->Shell->tasks = array('TestApple', 'TestBanana');
  353. $this->assertTrue($this->Shell->loadTasks());
  354. $this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
  355. $this->assertInstanceOf('TestBananaTask', $this->Shell->TestBanana);
  356. }
  357. /**
  358. * test that __get() makes args and params references
  359. *
  360. * @return void
  361. */
  362. function test__getArgAndParamReferences() {
  363. $this->Shell->tasks = array('TestApple');
  364. $this->Shell->args = array('one');
  365. $this->Shell->params = array('help' => false);
  366. $this->Shell->loadTasks();
  367. $result = $this->Shell->TestApple;
  368. $this->Shell->args = array('one', 'two');
  369. $this->assertSame($this->Shell->args, $result->args);
  370. $this->assertSame($this->Shell->params, $result->params);
  371. }
  372. /**
  373. * testShortPath method
  374. *
  375. * @return void
  376. */
  377. public function testShortPath() {
  378. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  379. $this->assertEqual($this->Shell->shortPath($path), $expected);
  380. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS ;
  381. $this->assertEqual($this->Shell->shortPath($path), $expected);
  382. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
  383. $this->assertEqual($this->Shell->shortPath($path), $expected);
  384. // Shell::shortPath needs Folder::realpath
  385. // $path = DS . 'tmp' . DS . 'ab' . DS . '..' . DS . 'cd';
  386. // $expected = DS . 'tmp' . DS . 'cd';
  387. // $this->assertEqual($this->Shell->shortPath($path), $expected);
  388. $path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
  389. $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  390. $this->assertEqual($this->Shell->shortPath($path), $expected);
  391. $path = 'tmp' . DS . 'ab';
  392. $expected = 'tmp' . DS . 'ab';
  393. $this->assertEqual($this->Shell->shortPath($path), $expected);
  394. $path = 'tmp' . DS . 'ab';
  395. $expected = 'tmp' . DS . 'ab';
  396. $this->assertEqual($this->Shell->shortPath($path), $expected);
  397. $path = APP;
  398. $expected = DS . basename(APP) . DS;
  399. $this->assertEqual($this->Shell->shortPath($path), $expected);
  400. $path = APP . 'index.php';
  401. $expected = DS . basename(APP) . DS . 'index.php';
  402. $this->assertEqual($this->Shell->shortPath($path), $expected);
  403. }
  404. /**
  405. * testCreateFile method
  406. *
  407. * @return void
  408. */
  409. public function testCreateFileNonInteractive() {
  410. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
  411. $path = TMP . 'shell_test';
  412. $file = $path . DS . 'file1.php';
  413. $Folder = new Folder($path, true);
  414. $this->Shell->interactive = false;
  415. $contents = "<?php\necho 'test';\n\$te = 'st';\n?>";
  416. $result = $this->Shell->createFile($file, $contents);
  417. $this->assertTrue($result);
  418. $this->assertTrue(file_exists($file));
  419. $this->assertEqual(file_get_contents($file), $contents);
  420. $contents = "<?php\necho 'another test';\n\$te = 'st';\n?>";
  421. $result = $this->Shell->createFile($file, $contents);
  422. $this->assertTrue($result);
  423. $this->assertTrue(file_exists($file));
  424. $this->assertEqual(file_get_contents($file), $contents);
  425. $Folder->delete();
  426. }
  427. /**
  428. * test createFile when the shell is interactive.
  429. *
  430. * @return void
  431. */
  432. function testCreateFileInteractive() {
  433. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
  434. $path = TMP . 'shell_test';
  435. $file = $path . DS . 'file1.php';
  436. $Folder = new Folder($path, true);
  437. $this->Shell->interactive = true;
  438. $this->Shell->stdin->expects($this->at(0))
  439. ->method('read')
  440. ->will($this->returnValue('n'));
  441. $this->Shell->stdin->expects($this->at(1))
  442. ->method('read')
  443. ->will($this->returnValue('y'));
  444. $contents = "<?php\necho 'yet another test';\n\$te = 'st';\n?>";
  445. $result = $this->Shell->createFile($file, $contents);
  446. $this->assertTrue($result);
  447. $this->assertTrue(file_exists($file));
  448. $this->assertEqual(file_get_contents($file), $contents);
  449. // no overwrite
  450. $contents = 'new contents';
  451. $result = $this->Shell->createFile($file, $contents);
  452. $this->assertFalse($result);
  453. $this->assertTrue(file_exists($file));
  454. $this->assertNotEqual($contents, file_get_contents($file));
  455. // overwrite
  456. $contents = 'more new contents';
  457. $result = $this->Shell->createFile($file, $contents);
  458. $this->assertTrue($result);
  459. $this->assertTrue(file_exists($file));
  460. $this->assertEquals($contents, file_get_contents($file));
  461. $Folder->delete();
  462. }
  463. /**
  464. * testCreateFileWindows method
  465. *
  466. * @return void
  467. */
  468. public function testCreateFileWindowsNonInteractive() {
  469. $this->skipIf(DIRECTORY_SEPARATOR === '/', 'testCreateFileWindows supported on Windows only');
  470. $path = TMP . 'shell_test';
  471. $file = $path . DS . 'file1.php';
  472. $Folder = new Folder($path, true);
  473. $this->Shell->interactive = false;
  474. $contents = "<?php\necho 'test';\r\n\$te = 'st';\r\n?>";
  475. $result = $this->Shell->createFile($file, $contents);
  476. $this->assertTrue($result);
  477. $this->assertTrue(file_exists($file));
  478. $this->assertEqual(file_get_contents($file), $contents);
  479. $contents = "<?php\necho 'another test';\r\n\$te = 'st';\r\n?>";
  480. $result = $this->Shell->createFile($file, $contents);
  481. $this->assertTrue($result);
  482. $this->assertTrue(file_exists($file));
  483. $this->assertEqual(file_get_contents($file), $contents);
  484. $Folder = new Folder($path);
  485. $Folder->delete();
  486. }
  487. /**
  488. * test createFile on windows with interactive on.
  489. *
  490. * @return void
  491. */
  492. function testCreateFileWindowsInteractive() {
  493. $this->skipIf(DIRECTORY_SEPARATOR === '/', 'testCreateFileWindowsInteractive supported on Windows only');
  494. $path = TMP . 'shell_test';
  495. $file = $path . DS . 'file1.php';
  496. $Folder = new Folder($path, true);
  497. $this->Shell->interactive = true;
  498. $this->Shell->Dispatch->expects($this->at(5))
  499. ->method('getInput')
  500. ->will($this->returnValue('y'));
  501. $this->Shell->Dispatch->expects($this->at(9))
  502. ->method('getInput')
  503. ->will($this->returnValue('n'));
  504. $contents = "<?php\necho 'yet another test';\r\n\$te = 'st';\r\n?>";
  505. $result = $this->Shell->createFile($file, $contents);
  506. $this->assertFalse($result);
  507. $this->assertTrue(file_exists($file));
  508. $this->assertNotEqual(file_get_contents($file), $contents);
  509. $result = $this->Shell->createFile($file, $contents);
  510. $this->assertTrue($result);
  511. $this->assertTrue(file_exists($file));
  512. $this->assertEqual(file_get_contents($file), $contents);
  513. $Folder->delete();
  514. }
  515. /**
  516. * test hasTask method
  517. *
  518. * @return void
  519. */
  520. function testHasTask() {
  521. $this->Shell->tasks = array('Extract', 'DbConfig');
  522. $this->Shell->loadTasks();
  523. $this->assertTrue($this->Shell->hasTask('extract'));
  524. $this->assertTrue($this->Shell->hasTask('Extract'));
  525. $this->assertFalse($this->Shell->hasTask('random'));
  526. $this->assertTrue($this->Shell->hasTask('db_config'));
  527. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  528. }
  529. /**
  530. * test the hasMethod
  531. *
  532. * @return void
  533. */
  534. function testHasMethod() {
  535. $this->assertTrue($this->Shell->hasMethod('do_something'));
  536. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  537. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  538. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  539. }
  540. /**
  541. * test run command calling main.
  542. *
  543. * @return void
  544. */
  545. function testRunCommandMain() {
  546. $methods = get_class_methods('Shell');
  547. $Mock = $this->getMock('Shell', array('main', 'startup'), array(), '', false);
  548. $Mock->expects($this->once())->method('main')->will($this->returnValue(true));
  549. $result = $Mock->runCommand(null, array());
  550. $this->assertTrue($result);
  551. }
  552. /**
  553. * test run command calling a legit method.
  554. *
  555. * @return void
  556. */
  557. function testRunCommandWithMethod() {
  558. $methods = get_class_methods('Shell');
  559. $Mock = $this->getMock('Shell', array('hit_me', 'startup'), array(), '', false);
  560. $Mock->expects($this->once())->method('hit_me')->will($this->returnValue(true));
  561. $result = $Mock->runCommand('hit_me', array());
  562. $this->assertTrue($result);
  563. }
  564. /**
  565. * test run command causing exception on Shell method.
  566. *
  567. * @return void
  568. */
  569. function testRunCommandBaseclassMethod() {
  570. $Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
  571. $Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
  572. $Parser->expects($this->once())->method('help');
  573. $Mock->expects($this->once())->method('getOptionParser')
  574. ->will($this->returnValue($Parser));
  575. $Mock->expects($this->never())->method('hr');
  576. $Mock->expects($this->once())->method('out');
  577. $result = $Mock->runCommand('hr', array());
  578. }
  579. /**
  580. * test run command causing exception on Shell method.
  581. *
  582. * @return void
  583. */
  584. function testRunCommandMissingMethod() {
  585. $methods = get_class_methods('Shell');
  586. $Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
  587. $Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
  588. $Parser->expects($this->once())->method('help');
  589. $Mock->expects($this->never())->method('idontexist');
  590. $Mock->expects($this->once())->method('getOptionParser')
  591. ->will($this->returnValue($Parser));
  592. $Mock->expects($this->once())->method('out');
  593. $result = $Mock->runCommand('idontexist', array());
  594. }
  595. /**
  596. * test that a --help causes help to show.
  597. *
  598. * @return void
  599. */
  600. function testRunCommandTriggeringHelp() {
  601. $Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
  602. $Parser->expects($this->once())->method('parse')
  603. ->with(array('--help'))
  604. ->will($this->returnValue(array(array('help' => true), array())));
  605. $Parser->expects($this->once())->method('help');
  606. $Shell = $this->getMock('Shell', array('getOptionParser', 'out', 'startup', '_welcome'), array(), '', false);
  607. $Shell->expects($this->once())->method('getOptionParser')
  608. ->will($this->returnValue($Parser));
  609. $Shell->expects($this->once())->method('out');
  610. $Shell->runCommand(null, array('--help'));
  611. }
  612. /**
  613. * test that runCommand will call runCommand on the task.
  614. *
  615. * @return void
  616. */
  617. function testRunCommandHittingTask() {
  618. $Shell = $this->getMock('Shell', array('hasTask', 'startup'), array(), '', false);
  619. $task = $this->getMock('Shell', array('execute', 'runCommand'), array(), '', false);
  620. $task->expects($this->any())->method('runCommand')
  621. ->with('execute', array('one', 'value'));
  622. $Shell->expects($this->once())->method('startup');
  623. $Shell->expects($this->any())->method('hasTask')->will($this->returnValue(true));
  624. $Shell->RunCommand = $task;
  625. $Shell->runCommand('run_command', array('run_command', 'one', 'value'));
  626. }
  627. /**
  628. * test wrapBlock wrapping text.
  629. *
  630. * @return void
  631. */
  632. function testWrapText() {
  633. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  634. $result = $this->Shell->wrapText($text, 33);
  635. $expected = <<<TEXT
  636. This is the song that never ends.
  637. This is the song that never ends.
  638. This is the song that never ends.
  639. TEXT;
  640. $this->assertEquals($expected, $result, 'Text not wrapped.');
  641. $result = $this->Shell->wrapText($text, array('indent' => ' ', 'width' => 33));
  642. $expected = <<<TEXT
  643. This is the song that never ends.
  644. This is the song that never ends.
  645. This is the song that never ends.
  646. TEXT;
  647. $this->assertEquals($expected, $result, 'Text not wrapped.');
  648. }
  649. }