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

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

https://github.com/vidor/myworks
PHP | 501 lines | 272 code | 74 blank | 155 comment | 2 complexity | 1b3c7be64b9bcc68cf6c82b7a8bd602b 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-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
  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->interactive = false;
  186. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  187. $this->assertEqual($result, 'n');
  188. }
  189. /**
  190. * testOut method
  191. *
  192. * @return void
  193. * @access public
  194. */
  195. function testOut() {
  196. $this->Shell->Dispatch->expectAt(0, 'stdout', array("Just a test\n", false));
  197. $this->Shell->out('Just a test');
  198. $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", false));
  199. $this->Shell->out(array('Just', 'a', 'test'));
  200. $this->Shell->Dispatch->expectAt(2, 'stdout', array("Just\na\ntest\n\n", false));
  201. $this->Shell->out(array('Just', 'a', 'test'), 2);
  202. $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false));
  203. $this->Shell->out();
  204. }
  205. /**
  206. * testErr method
  207. *
  208. * @return void
  209. * @access public
  210. */
  211. function testErr() {
  212. $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n"));
  213. $this->Shell->err('Just a test');
  214. $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n"));
  215. $this->Shell->err(array('Just', 'a', 'test'));
  216. $this->Shell->Dispatch->expectAt(2, 'stderr', array("Just\na\ntest\n\n"));
  217. $this->Shell->err(array('Just', 'a', 'test'), 2);
  218. $this->Shell->Dispatch->expectAt(3, 'stderr', array("\n"));
  219. $this->Shell->err();
  220. }
  221. /**
  222. * testNl
  223. *
  224. * @return void
  225. * @access public
  226. */
  227. function testNl() {
  228. $this->assertEqual($this->Shell->nl(), "\n");
  229. $this->assertEqual($this->Shell->nl(true), "\n");
  230. $this->assertEqual($this->Shell->nl(false), "");
  231. $this->assertEqual($this->Shell->nl(2), "\n\n");
  232. $this->assertEqual($this->Shell->nl(1), "\n");
  233. }
  234. /**
  235. * testHr
  236. *
  237. * @return void
  238. * @access public
  239. */
  240. function testHr() {
  241. $bar = '---------------------------------------------------------------';
  242. $this->Shell->Dispatch->expectAt(0, 'stdout', array('', false));
  243. $this->Shell->Dispatch->expectAt(1, 'stdout', array($bar . "\n", false));
  244. $this->Shell->Dispatch->expectAt(2, 'stdout', array('', false));
  245. $this->Shell->hr();
  246. $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false));
  247. $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false));
  248. $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n", false));
  249. $this->Shell->hr(true);
  250. $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n\n", false));
  251. $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false));
  252. $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n\n", false));
  253. $this->Shell->hr(2);
  254. }
  255. /**
  256. * testError
  257. *
  258. * @return void
  259. * @access public
  260. */
  261. function testError() {
  262. $this->Shell->Dispatch->expectAt(0, 'stderr', array("Error: Foo Not Found\n"));
  263. $this->Shell->error('Foo Not Found');
  264. $this->assertIdentical($this->Shell->stopped, 1);
  265. $this->Shell->stopped = null;
  266. $this->Shell->Dispatch->expectAt(1, 'stderr', array("Error: Foo Not Found\n"));
  267. $this->Shell->Dispatch->expectAt(2, 'stderr', array("Searched all...\n"));
  268. $this->Shell->error('Foo Not Found', 'Searched all...');
  269. $this->assertIdentical($this->Shell->stopped, 1);
  270. }
  271. /**
  272. * testLoadTasks method
  273. *
  274. * @return void
  275. * @access public
  276. */
  277. function testLoadTasks() {
  278. $this->assertTrue($this->Shell->loadTasks());
  279. $this->Shell->tasks = null;
  280. $this->assertTrue($this->Shell->loadTasks());
  281. $this->Shell->tasks = false;
  282. $this->assertTrue($this->Shell->loadTasks());
  283. $this->Shell->tasks = true;
  284. $this->assertTrue($this->Shell->loadTasks());
  285. $this->Shell->tasks = array();
  286. $this->assertTrue($this->Shell->loadTasks());
  287. // Fatal Error
  288. // $this->Shell->tasks = 'TestIDontExist';
  289. // $this->assertFalse($this->Shell->loadTasks());
  290. // $this->assertFalse(isset($this->Shell->TestIDontExist));
  291. $this->Shell->tasks = 'TestApple';
  292. $this->assertTrue($this->Shell->loadTasks());
  293. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  294. $this->Shell->tasks = 'TestBanana';
  295. $this->assertTrue($this->Shell->loadTasks());
  296. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  297. $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
  298. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  299. $this->Shell->tasks = array('TestApple', 'TestBanana');
  300. $this->assertTrue($this->Shell->loadTasks());
  301. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  302. $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
  303. }
  304. /**
  305. * testShortPath method
  306. *
  307. * @return void
  308. * @access public
  309. */
  310. function testShortPath() {
  311. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  312. $this->assertEqual($this->Shell->shortPath($path), $expected);
  313. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS ;
  314. $this->assertEqual($this->Shell->shortPath($path), $expected);
  315. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
  316. $this->assertEqual($this->Shell->shortPath($path), $expected);
  317. // Shell::shortPath needs Folder::realpath
  318. // $path = DS . 'tmp' . DS . 'ab' . DS . '..' . DS . 'cd';
  319. // $expected = DS . 'tmp' . DS . 'cd';
  320. // $this->assertEqual($this->Shell->shortPath($path), $expected);
  321. $path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
  322. $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  323. $this->assertEqual($this->Shell->shortPath($path), $expected);
  324. $path = 'tmp' . DS . 'ab';
  325. $expected = 'tmp' . DS . 'ab';
  326. $this->assertEqual($this->Shell->shortPath($path), $expected);
  327. $path = 'tmp' . DS . 'ab';
  328. $expected = 'tmp' . DS . 'ab';
  329. $this->assertEqual($this->Shell->shortPath($path), $expected);
  330. $path = APP;
  331. $expected = DS . basename(APP) . DS;
  332. $this->assertEqual($this->Shell->shortPath($path), $expected);
  333. $path = APP . 'index.php';
  334. $expected = DS . basename(APP) . DS . 'index.php';
  335. $this->assertEqual($this->Shell->shortPath($path), $expected);
  336. }
  337. /**
  338. * testCreateFile method
  339. *
  340. * @return void
  341. * @access public
  342. */
  343. function testCreateFile() {
  344. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
  345. $path = TMP . 'shell_test';
  346. $file = $path . DS . 'file1.php';
  347. new Folder($path, true);
  348. $this->Shell->interactive = false;
  349. $contents = "<?php\necho 'test';\n\$te = 'st';\n?>";
  350. $result = $this->Shell->createFile($file, $contents);
  351. $this->assertTrue($result);
  352. $this->assertTrue(file_exists($file));
  353. $this->assertEqual(file_get_contents($file), $contents);
  354. $contents = "<?php\necho 'another test';\n\$te = 'st';\n?>";
  355. $result = $this->Shell->createFile($file, $contents);
  356. $this->assertTrue($result);
  357. $this->assertTrue(file_exists($file));
  358. $this->assertEqual(file_get_contents($file), $contents);
  359. $this->Shell->interactive = true;
  360. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  361. $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?', '*'));
  362. $contents = "<?php\necho 'yet another test';\n\$te = 'st';\n?>";
  363. $result = $this->Shell->createFile($file, $contents);
  364. $this->assertFalse($result);
  365. $this->assertTrue(file_exists($file));
  366. $this->assertNotEqual(file_get_contents($file), $contents);
  367. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
  368. $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?', '*'));
  369. $result = $this->Shell->createFile($file, $contents);
  370. $this->assertTrue($result);
  371. $this->assertTrue(file_exists($file));
  372. $this->assertEqual(file_get_contents($file), $contents);
  373. $Folder = new Folder($path);
  374. $Folder->delete();
  375. }
  376. /**
  377. * testCreateFileWindows method
  378. *
  379. * @return void
  380. * @access public
  381. */
  382. function testCreateFileWindows() {
  383. $this->skipUnless(DIRECTORY_SEPARATOR === '\\', 'testCreateFileWindows supported on Windows only');
  384. $path = TMP . 'shell_test';
  385. $file = $path . DS . 'file1.php';
  386. new Folder($path, true);
  387. $this->Shell->interactive = false;
  388. $contents = "<?php\necho 'test';\r\n\$te = 'st';\r\n?>";
  389. $result = $this->Shell->createFile($file, $contents);
  390. $this->assertTrue($result);
  391. $this->assertTrue(file_exists($file));
  392. $this->assertEqual(file_get_contents($file), $contents);
  393. $contents = "<?php\necho 'another test';\r\n\$te = 'st';\r\n?>";
  394. $result = $this->Shell->createFile($file, $contents);
  395. $this->assertTrue($result);
  396. $this->assertTrue(file_exists($file));
  397. $this->assertEqual(file_get_contents($file), $contents);
  398. $this->Shell->interactive = true;
  399. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  400. $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?'));
  401. $contents = "<?php\necho 'yet another test';\r\n\$te = 'st';\r\n?>";
  402. $result = $this->Shell->createFile($file, $contents);
  403. $this->assertFalse($result);
  404. $this->assertTrue(file_exists($file));
  405. $this->assertNotEqual(file_get_contents($file), $contents);
  406. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
  407. $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?'));
  408. $result = $this->Shell->createFile($file, $contents);
  409. $this->assertTrue($result);
  410. $this->assertTrue(file_exists($file));
  411. $this->assertEqual(file_get_contents($file), $contents);
  412. $Folder = new Folder($path);
  413. $Folder->delete();
  414. }
  415. }