ShellTest.php 23 KB

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