PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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