ShellTest.php 23 KB

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