PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/console/libs/shell.test.php

http://github.com/Datawalke/Coordino
PHP | 513 lines | 278 code | 75 blank | 160 comment | 2 complexity | 14dd35252043d1f808b949b40bc797b9 MD5 | raw file
  1. <?php
  2. /**
  3. * ShellTest file
  4. *
  5. * Test Case for Shell
  6. *
  7. * PHP versions 4 and 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
  18. * @subpackage cake.tests.cases.console.libs
  19. * @since CakePHP v 1.2.0.7726
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::import('Core', 'Folder');
  23. App::import('Shell', 'Shell', false);
  24. if (!defined('DISABLE_AUTO_DISPATCH')) {
  25. define('DISABLE_AUTO_DISPATCH', true);
  26. }
  27. if (!class_exists('ShellDispatcher')) {
  28. ob_start();
  29. $argv = false;
  30. require CAKE . 'console' . DS . 'cake.php';
  31. ob_end_clean();
  32. }
  33. Mock::generatePartial('ShellDispatcher', 'TestShellMockShellDispatcher', array(
  34. 'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment'
  35. ));
  36. /**
  37. * TestShell class
  38. *
  39. * @package cake
  40. * @subpackage cake.tests.cases.console.libs
  41. */
  42. class TestShell extends Shell {
  43. /**
  44. * name property
  45. *
  46. * @var name
  47. * @access public
  48. */
  49. var $name = 'TestShell';
  50. /**
  51. * stopped property
  52. *
  53. * @var integer
  54. * @access public
  55. */
  56. var $stopped;
  57. /**
  58. * stop method
  59. *
  60. * @param integer $status
  61. * @return void
  62. * @access protected
  63. */
  64. function _stop($status = 0) {
  65. $this->stopped = $status;
  66. }
  67. }
  68. /**
  69. * TestAppleTask class
  70. *
  71. * @package cake
  72. * @subpackage cake.tests.cases.console.libs
  73. */
  74. class TestAppleTask extends Shell {
  75. }
  76. /**
  77. * TestBananaTask class
  78. *
  79. * @package cake
  80. * @subpackage cake.tests.cases.console.libs
  81. */
  82. class TestBananaTask extends Shell {
  83. }
  84. /**
  85. * ShellTest class
  86. *
  87. * @package cake
  88. * @subpackage cake.tests.cases.console.libs
  89. */
  90. class ShellTest extends CakeTestCase {
  91. /**
  92. * Fixtures used in this test case
  93. *
  94. * @var array
  95. * @access public
  96. */
  97. var $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. * @access public
  106. */
  107. function setUp() {
  108. $this->Dispatcher =& new TestShellMockShellDispatcher();
  109. $this->Shell =& new TestShell($this->Dispatcher);
  110. }
  111. /**
  112. * tearDown method
  113. *
  114. * @return void
  115. * @access public
  116. */
  117. function tearDown() {
  118. ClassRegistry::flush();
  119. }
  120. /**
  121. * testConstruct method
  122. *
  123. * @return void
  124. * @access public
  125. */
  126. function testConstruct() {
  127. $this->assertIsA($this->Shell->Dispatch, 'TestShellMockShellDispatcher');
  128. $this->assertEqual($this->Shell->name, 'TestShell');
  129. $this->assertEqual($this->Shell->alias, 'TestShell');
  130. }
  131. /**
  132. * testInitialize method
  133. *
  134. * @return void
  135. * @access public
  136. */
  137. function testInitialize() {
  138. App::build(array(
  139. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  140. 'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS)
  141. ), true);
  142. $this->Shell->uses = array('TestPlugin.TestPluginPost');
  143. $this->Shell->initialize();
  144. $this->assertTrue(isset($this->Shell->TestPluginPost));
  145. $this->assertIsA($this->Shell->TestPluginPost, 'TestPluginPost');
  146. $this->assertEqual($this->Shell->modelClass, 'TestPluginPost');
  147. $this->Shell->uses = array('Comment');
  148. $this->Shell->initialize();
  149. $this->assertTrue(isset($this->Shell->Comment));
  150. $this->assertIsA($this->Shell->Comment, 'Comment');
  151. $this->assertEqual($this->Shell->modelClass, 'Comment');
  152. $this->Shell->uses = true;
  153. $this->Shell->initialize();
  154. $this->assertTrue(isset($this->Shell->AppModel));
  155. $this->assertIsA($this->Shell->AppModel, 'AppModel');
  156. App::build();
  157. }
  158. /**
  159. * testIn method
  160. *
  161. * @return void
  162. * @access public
  163. */
  164. function testIn() {
  165. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  166. $this->Shell->Dispatch->expectAt(0, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
  167. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  168. $this->assertEqual($result, 'n');
  169. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'Y');
  170. $this->Shell->Dispatch->expectAt(1, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
  171. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  172. $this->assertEqual($result, 'Y');
  173. $this->Shell->Dispatch->setReturnValueAt(2, 'getInput', 'y');
  174. $this->Shell->Dispatch->expectAt(2, 'getInput', array('Just a test?', 'y,n', 'n'));
  175. $result = $this->Shell->in('Just a test?', 'y,n', 'n');
  176. $this->assertEqual($result, 'y');
  177. $this->Shell->Dispatch->setReturnValueAt(3, 'getInput', 'y');
  178. $this->Shell->Dispatch->expectAt(3, 'getInput', array('Just a test?', 'y/n', 'n'));
  179. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  180. $this->assertEqual($result, 'y');
  181. $this->Shell->Dispatch->setReturnValueAt(4, 'getInput', 'y');
  182. $this->Shell->Dispatch->expectAt(4, 'getInput', array('Just a test?', 'y', 'y'));
  183. $result = $this->Shell->in('Just a test?', 'y', 'y');
  184. $this->assertEqual($result, 'y');
  185. $this->Shell->Dispatch->setReturnValueAt(5, 'getInput', 'y');
  186. $this->Shell->Dispatch->expectAt(5, 'getInput', array('Just a test?', array(0, 1, 2), 0));
  187. $result = $this->Shell->in('Just a test?', array(0, 1, 2), 0);
  188. $this->assertEqual($result, 0);
  189. }
  190. /**
  191. * Test in() when not interactive
  192. *
  193. * @return void
  194. */
  195. function testInNonInteractive() {
  196. $this->Shell->interactive = false;
  197. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  198. $this->assertEqual($result, 'n');
  199. }
  200. /**
  201. * testOut method
  202. *
  203. * @return void
  204. * @access public
  205. */
  206. function testOut() {
  207. $this->Shell->Dispatch->expectAt(0, 'stdout', array("Just a test\n", false));
  208. $this->Shell->out('Just a test');
  209. $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", false));
  210. $this->Shell->out(array('Just', 'a', 'test'));
  211. $this->Shell->Dispatch->expectAt(2, 'stdout', array("Just\na\ntest\n\n", false));
  212. $this->Shell->out(array('Just', 'a', 'test'), 2);
  213. $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false));
  214. $this->Shell->out();
  215. }
  216. /**
  217. * testErr method
  218. *
  219. * @return void
  220. * @access public
  221. */
  222. function testErr() {
  223. $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n"));
  224. $this->Shell->err('Just a test');
  225. $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n"));
  226. $this->Shell->err(array('Just', 'a', 'test'));
  227. $this->Shell->Dispatch->expectAt(2, 'stderr', array("Just\na\ntest\n\n"));
  228. $this->Shell->err(array('Just', 'a', 'test'), 2);
  229. $this->Shell->Dispatch->expectAt(3, 'stderr', array("\n"));
  230. $this->Shell->err();
  231. }
  232. /**
  233. * testNl
  234. *
  235. * @return void
  236. * @access public
  237. */
  238. function testNl() {
  239. $this->assertEqual($this->Shell->nl(), "\n");
  240. $this->assertEqual($this->Shell->nl(true), "\n");
  241. $this->assertEqual($this->Shell->nl(false), "");
  242. $this->assertEqual($this->Shell->nl(2), "\n\n");
  243. $this->assertEqual($this->Shell->nl(1), "\n");
  244. }
  245. /**
  246. * testHr
  247. *
  248. * @return void
  249. * @access public
  250. */
  251. function testHr() {
  252. $bar = '---------------------------------------------------------------';
  253. $this->Shell->Dispatch->expectAt(0, 'stdout', array('', false));
  254. $this->Shell->Dispatch->expectAt(1, 'stdout', array($bar . "\n", false));
  255. $this->Shell->Dispatch->expectAt(2, 'stdout', array('', false));
  256. $this->Shell->hr();
  257. $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false));
  258. $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false));
  259. $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n", false));
  260. $this->Shell->hr(true);
  261. $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n\n", false));
  262. $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false));
  263. $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n\n", false));
  264. $this->Shell->hr(2);
  265. }
  266. /**
  267. * testError
  268. *
  269. * @return void
  270. * @access public
  271. */
  272. function testError() {
  273. $this->Shell->Dispatch->expectAt(0, 'stderr', array("Error: Foo Not Found\n"));
  274. $this->Shell->error('Foo Not Found');
  275. $this->assertIdentical($this->Shell->stopped, 1);
  276. $this->Shell->stopped = null;
  277. $this->Shell->Dispatch->expectAt(1, 'stderr', array("Error: Foo Not Found\n"));
  278. $this->Shell->Dispatch->expectAt(2, 'stderr', array("Searched all...\n"));
  279. $this->Shell->error('Foo Not Found', 'Searched all...');
  280. $this->assertIdentical($this->Shell->stopped, 1);
  281. }
  282. /**
  283. * testLoadTasks method
  284. *
  285. * @return void
  286. * @access public
  287. */
  288. function testLoadTasks() {
  289. $this->assertTrue($this->Shell->loadTasks());
  290. $this->Shell->tasks = null;
  291. $this->assertTrue($this->Shell->loadTasks());
  292. $this->Shell->tasks = false;
  293. $this->assertTrue($this->Shell->loadTasks());
  294. $this->Shell->tasks = true;
  295. $this->assertTrue($this->Shell->loadTasks());
  296. $this->Shell->tasks = array();
  297. $this->assertTrue($this->Shell->loadTasks());
  298. // Fatal Error
  299. // $this->Shell->tasks = 'TestIDontExist';
  300. // $this->assertFalse($this->Shell->loadTasks());
  301. // $this->assertFalse(isset($this->Shell->TestIDontExist));
  302. $this->Shell->tasks = 'TestApple';
  303. $this->assertTrue($this->Shell->loadTasks());
  304. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  305. $this->Shell->tasks = 'TestBanana';
  306. $this->assertTrue($this->Shell->loadTasks());
  307. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  308. $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
  309. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  310. $this->Shell->tasks = array('TestApple', 'TestBanana');
  311. $this->assertTrue($this->Shell->loadTasks());
  312. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  313. $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
  314. }
  315. /**
  316. * testShortPath method
  317. *
  318. * @return void
  319. * @access public
  320. */
  321. function testShortPath() {
  322. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  323. $this->assertEqual($this->Shell->shortPath($path), $expected);
  324. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS ;
  325. $this->assertEqual($this->Shell->shortPath($path), $expected);
  326. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
  327. $this->assertEqual($this->Shell->shortPath($path), $expected);
  328. // Shell::shortPath needs Folder::realpath
  329. // $path = DS . 'tmp' . DS . 'ab' . DS . '..' . DS . 'cd';
  330. // $expected = DS . 'tmp' . DS . 'cd';
  331. // $this->assertEqual($this->Shell->shortPath($path), $expected);
  332. $path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
  333. $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  334. $this->assertEqual($this->Shell->shortPath($path), $expected);
  335. $path = 'tmp' . DS . 'ab';
  336. $expected = 'tmp' . DS . 'ab';
  337. $this->assertEqual($this->Shell->shortPath($path), $expected);
  338. $path = 'tmp' . DS . 'ab';
  339. $expected = 'tmp' . DS . 'ab';
  340. $this->assertEqual($this->Shell->shortPath($path), $expected);
  341. $path = APP;
  342. $expected = DS . basename(APP) . DS;
  343. $this->assertEqual($this->Shell->shortPath($path), $expected);
  344. $path = APP . 'index.php';
  345. $expected = DS . basename(APP) . DS . 'index.php';
  346. $this->assertEqual($this->Shell->shortPath($path), $expected);
  347. }
  348. /**
  349. * testCreateFile method
  350. *
  351. * @return void
  352. * @access public
  353. */
  354. function testCreateFile() {
  355. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
  356. $path = TMP . 'shell_test';
  357. $file = $path . DS . 'file1.php';
  358. new Folder($path, true);
  359. $this->Shell->interactive = false;
  360. $contents = "<?php\necho 'test';\n\$te = 'st';\n?>";
  361. $result = $this->Shell->createFile($file, $contents);
  362. $this->assertTrue($result);
  363. $this->assertTrue(file_exists($file));
  364. $this->assertEqual(file_get_contents($file), $contents);
  365. $contents = "<?php\necho 'another test';\n\$te = 'st';\n?>";
  366. $result = $this->Shell->createFile($file, $contents);
  367. $this->assertTrue($result);
  368. $this->assertTrue(file_exists($file));
  369. $this->assertEqual(file_get_contents($file), $contents);
  370. $this->Shell->interactive = true;
  371. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  372. $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?', '*'));
  373. $contents = "<?php\necho 'yet another test';\n\$te = 'st';\n?>";
  374. $result = $this->Shell->createFile($file, $contents);
  375. $this->assertFalse($result);
  376. $this->assertTrue(file_exists($file));
  377. $this->assertNotEqual(file_get_contents($file), $contents);
  378. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
  379. $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?', '*'));
  380. $result = $this->Shell->createFile($file, $contents);
  381. $this->assertTrue($result);
  382. $this->assertTrue(file_exists($file));
  383. $this->assertEqual(file_get_contents($file), $contents);
  384. $Folder = new Folder($path);
  385. $Folder->delete();
  386. }
  387. /**
  388. * testCreateFileWindows method
  389. *
  390. * @return void
  391. * @access public
  392. */
  393. function testCreateFileWindows() {
  394. $this->skipUnless(DIRECTORY_SEPARATOR === '\\', 'testCreateFileWindows supported on Windows only');
  395. $path = TMP . 'shell_test';
  396. $file = $path . DS . 'file1.php';
  397. new Folder($path, true);
  398. $this->Shell->interactive = false;
  399. $contents = "<?php\necho 'test';\r\n\$te = 'st';\r\n?>";
  400. $result = $this->Shell->createFile($file, $contents);
  401. $this->assertTrue($result);
  402. $this->assertTrue(file_exists($file));
  403. $this->assertEqual(file_get_contents($file), $contents);
  404. $contents = "<?php\necho 'another test';\r\n\$te = 'st';\r\n?>";
  405. $result = $this->Shell->createFile($file, $contents);
  406. $this->assertTrue($result);
  407. $this->assertTrue(file_exists($file));
  408. $this->assertEqual(file_get_contents($file), $contents);
  409. $this->Shell->interactive = true;
  410. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  411. $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?'));
  412. $contents = "<?php\necho 'yet another test';\r\n\$te = 'st';\r\n?>";
  413. $result = $this->Shell->createFile($file, $contents);
  414. $this->assertFalse($result);
  415. $this->assertTrue(file_exists($file));
  416. $this->assertNotEqual(file_get_contents($file), $contents);
  417. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
  418. $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?'));
  419. $result = $this->Shell->createFile($file, $contents);
  420. $this->assertTrue($result);
  421. $this->assertTrue(file_exists($file));
  422. $this->assertEqual(file_get_contents($file), $contents);
  423. $Folder = new Folder($path);
  424. $Folder->delete();
  425. }
  426. }