ShellTest.php 22 KB

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