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