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

https://github.com/hardsshah/bookmarks · PHP · 381 lines · 205 code · 53 blank · 123 comment · 2 complexity · 86789c2f4c40f64bae060b663ba1250a 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', array('Shell', 'Folder'));
  28. if (!defined('DISABLE_AUTO_DISPATCH')) {
  29. define('DISABLE_AUTO_DISPATCH', true);
  30. }
  31. if (!class_exists('ShellDispatcher')) {
  32. ob_start();
  33. $argv = false;
  34. require CAKE . 'console' . DS . 'cake.php';
  35. ob_end_clean();
  36. }
  37. Mock::generatePartial(
  38. 'ShellDispatcher', 'TestShellMockShellDispatcher',
  39. array('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. /**
  50. * TestAppleTask class
  51. *
  52. * @package cake
  53. * @subpackage cake.tests.cases.console.libs
  54. */
  55. class TestAppleTask extends Shell {
  56. }
  57. /**
  58. * TestBananaTask class
  59. *
  60. * @package cake
  61. * @subpackage cake.tests.cases.console.libs
  62. */
  63. class TestBananaTask extends Shell {
  64. }
  65. /**
  66. * ShellTest class
  67. *
  68. * @package cake
  69. * @subpackage cake.tests.cases.console.libs
  70. */
  71. class ShellTest extends CakeTestCase {
  72. /**
  73. * Fixtures used in this test case
  74. *
  75. * @var array
  76. * @access public
  77. */
  78. var $fixtures = array('core.post', 'core.comment');
  79. /**
  80. * setUp method
  81. *
  82. * @return void
  83. * @access public
  84. */
  85. function setUp() {
  86. $this->Dispatcher =& new TestShellMockShellDispatcher();
  87. $this->Shell =& new TestShell($this->Dispatcher);
  88. }
  89. /**
  90. * tearDown method
  91. *
  92. * @return void
  93. * @access public
  94. */
  95. function tearDown() {
  96. ClassRegistry::flush();
  97. }
  98. /**
  99. * testConstruct method
  100. *
  101. * @return void
  102. * @access public
  103. */
  104. function testConstruct() {
  105. $this->assertIsA($this->Shell->Dispatch, 'TestShellMockShellDispatcher');
  106. $this->assertEqual($this->Shell->name, 'TestShell');
  107. $this->assertEqual($this->Shell->alias, 'TestShell');
  108. }
  109. /**
  110. * testInitialize method
  111. *
  112. * @return void
  113. * @access public
  114. */
  115. function testInitialize() {
  116. $_back = array(
  117. 'modelPaths' => Configure::read('modelPaths'),
  118. 'pluginPaths' => Configure::read('pluginPaths'),
  119. 'viewPaths' => Configure::read('viewPaths'),
  120. );
  121. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  122. Configure::write('modelPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS));
  123. $this->Shell->uses = array('TestPlugin.TestPluginPost');
  124. $this->Shell->initialize();
  125. $this->assertTrue(isset($this->Shell->TestPluginPost));
  126. $this->assertIsA($this->Shell->TestPluginPost, 'TestPluginPost');
  127. $this->assertEqual($this->Shell->modelClass, 'TestPluginPost');
  128. $this->Shell->uses = array('Comment');
  129. $this->Shell->initialize();
  130. $this->assertTrue(isset($this->Shell->Comment));
  131. $this->assertIsA($this->Shell->Comment, 'Comment');
  132. $this->assertEqual($this->Shell->modelClass, 'Comment');
  133. $this->Shell->uses = true;
  134. $this->Shell->initialize();
  135. $this->assertTrue(isset($this->Shell->AppModel));
  136. $this->assertIsA($this->Shell->AppModel, 'AppModel');
  137. Configure::write('pluginPaths', $_back['pluginPaths']);
  138. Configure::write('modelPaths', $_back['modelPaths']);
  139. }
  140. /**
  141. * testOut method
  142. *
  143. * @return void
  144. * @access public
  145. */
  146. function testOut() {
  147. $this->Shell->Dispatch->expectAt(0, 'stdout', array('Just a test', true));
  148. $this->Shell->out('Just a test');
  149. $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", true));
  150. $this->Shell->out(array('Just', 'a', 'test'));
  151. }
  152. /**
  153. * testIn method
  154. *
  155. * @return void
  156. * @access public
  157. */
  158. function testIn() {
  159. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  160. $this->Shell->Dispatch->expectAt(0, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
  161. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  162. $this->assertEqual($result, 'n');
  163. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'Y');
  164. $this->Shell->Dispatch->expectAt(1, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
  165. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  166. $this->assertEqual($result, 'Y');
  167. $this->Shell->Dispatch->setReturnValueAt(2, 'getInput', 'y');
  168. $this->Shell->Dispatch->expectAt(2, 'getInput', array('Just a test?', 'y,n', 'n'));
  169. $result = $this->Shell->in('Just a test?', 'y,n', 'n');
  170. $this->assertEqual($result, 'y');
  171. $this->Shell->Dispatch->setReturnValueAt(3, 'getInput', 'y');
  172. $this->Shell->Dispatch->expectAt(3, 'getInput', array('Just a test?', 'y/n', 'n'));
  173. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  174. $this->assertEqual($result, 'y');
  175. $this->Shell->Dispatch->setReturnValueAt(4, 'getInput', 'y');
  176. $this->Shell->Dispatch->expectAt(4, 'getInput', array('Just a test?', 'y', 'y'));
  177. $result = $this->Shell->in('Just a test?', 'y', 'y');
  178. $this->assertEqual($result, 'y');
  179. $this->Shell->interactive = false;
  180. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  181. $this->assertEqual($result, 'n');
  182. }
  183. /**
  184. * testLoadTasks method
  185. *
  186. * @return void
  187. * @access public
  188. */
  189. function testLoadTasks() {
  190. $this->assertTrue($this->Shell->loadTasks());
  191. $this->Shell->tasks = null;
  192. $this->assertTrue($this->Shell->loadTasks());
  193. $this->Shell->tasks = false;
  194. $this->assertTrue($this->Shell->loadTasks());
  195. $this->Shell->tasks = true;
  196. $this->assertTrue($this->Shell->loadTasks());
  197. $this->Shell->tasks = array();
  198. $this->assertTrue($this->Shell->loadTasks());
  199. // Fatal Error
  200. // $this->Shell->tasks = 'TestIDontExist';
  201. // $this->assertFalse($this->Shell->loadTasks());
  202. // $this->assertFalse(isset($this->Shell->TestIDontExist));
  203. $this->Shell->tasks = 'TestApple';
  204. $this->assertTrue($this->Shell->loadTasks());
  205. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  206. $this->Shell->tasks = 'TestBanana';
  207. $this->assertTrue($this->Shell->loadTasks());
  208. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  209. $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
  210. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  211. $this->Shell->tasks = array('TestApple', 'TestBanana');
  212. $this->assertTrue($this->Shell->loadTasks());
  213. $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
  214. $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
  215. }
  216. /**
  217. * testShortPath method
  218. *
  219. * @return void
  220. * @access public
  221. */
  222. function testShortPath() {
  223. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  224. $this->assertEqual($this->Shell->shortPath($path), $expected);
  225. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS ;
  226. $this->assertEqual($this->Shell->shortPath($path), $expected);
  227. $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
  228. $this->assertEqual($this->Shell->shortPath($path), $expected);
  229. // Shell::shortPath needs Folder::realpath
  230. // $path = DS . 'tmp' . DS . 'ab' . DS . '..' . DS . 'cd';
  231. // $expected = DS . 'tmp' . DS . 'cd';
  232. // $this->assertEqual($this->Shell->shortPath($path), $expected);
  233. $path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
  234. $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
  235. $this->assertEqual($this->Shell->shortPath($path), $expected);
  236. $path = 'tmp' . DS . 'ab';
  237. $expected = 'tmp' . DS . 'ab';
  238. $this->assertEqual($this->Shell->shortPath($path), $expected);
  239. $path = 'tmp' . DS . 'ab';
  240. $expected = 'tmp' . DS . 'ab';
  241. $this->assertEqual($this->Shell->shortPath($path), $expected);
  242. $path = APP;
  243. $expected = DS . basename(APP) . DS;
  244. $this->assertEqual($this->Shell->shortPath($path), $expected);
  245. $path = APP . 'index.php';
  246. $expected = DS . basename(APP) . DS . 'index.php';
  247. $this->assertEqual($this->Shell->shortPath($path), $expected);
  248. }
  249. /**
  250. * testCreateFile method
  251. *
  252. * @return void
  253. * @access public
  254. */
  255. function testCreateFile() {
  256. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
  257. $path = TMP . 'shell_test';
  258. $file = $path . DS . 'file1.php';
  259. new Folder($path, true);
  260. $this->Shell->interactive = false;
  261. $contents = "<?php\necho 'test';\n\$te = 'st';\n?>";
  262. $result = $this->Shell->createFile($file, $contents);
  263. $this->assertTrue($result);
  264. $this->assertTrue(file_exists($file));
  265. $this->assertEqual(file_get_contents($file), $contents);
  266. $contents = "<?php\necho 'another test';\n\$te = 'st';\n?>";
  267. $result = $this->Shell->createFile($file, $contents);
  268. $this->assertTrue($result);
  269. $this->assertTrue(file_exists($file));
  270. $this->assertEqual(file_get_contents($file), $contents);
  271. $this->Shell->interactive = true;
  272. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  273. $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?', '*'));
  274. $contents = "<?php\necho 'yet another test';\n\$te = 'st';\n?>";
  275. $result = $this->Shell->createFile($file, $contents);
  276. $this->assertFalse($result);
  277. $this->assertTrue(file_exists($file));
  278. $this->assertNotEqual(file_get_contents($file), $contents);
  279. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
  280. $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?', '*'));
  281. $result = $this->Shell->createFile($file, $contents);
  282. $this->assertTrue($result);
  283. $this->assertTrue(file_exists($file));
  284. $this->assertEqual(file_get_contents($file), $contents);
  285. $Folder = new Folder($path);
  286. $Folder->delete();
  287. }
  288. /**
  289. * testCreateFileWindows method
  290. *
  291. * @return void
  292. * @access public
  293. */
  294. function testCreateFileWindows() {
  295. $this->skipUnless(DIRECTORY_SEPARATOR === '\\', '%s Supported on Windows only');
  296. $path = TMP . 'shell_test';
  297. $file = $path . DS . 'file1.php';
  298. new Folder($path, true);
  299. $this->Shell->interactive = false;
  300. $contents = "<?php\necho 'test';\r\n\$te = 'st';\r\n?>";
  301. $result = $this->Shell->createFile($file, $contents);
  302. $this->assertTrue($result);
  303. $this->assertTrue(file_exists($file));
  304. $this->assertEqual(file_get_contents($file), $contents);
  305. $contents = "<?php\necho 'another test';\r\n\$te = 'st';\r\n?>";
  306. $result = $this->Shell->createFile($file, $contents);
  307. $this->assertTrue($result);
  308. $this->assertTrue(file_exists($file));
  309. $this->assertEqual(file_get_contents($file), $contents);
  310. $this->Shell->interactive = true;
  311. $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
  312. $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?'));
  313. $contents = "<?php\necho 'yet another test';\r\n\$te = 'st';\r\n?>";
  314. $result = $this->Shell->createFile($file, $contents);
  315. $this->assertFalse($result);
  316. $this->assertTrue(file_exists($file));
  317. $this->assertNotEqual(file_get_contents($file), $contents);
  318. $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
  319. $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?'));
  320. $result = $this->Shell->createFile($file, $contents);
  321. $this->assertTrue($result);
  322. $this->assertTrue(file_exists($file));
  323. $this->assertEqual(file_get_contents($file), $contents);
  324. $Folder = new Folder($path);
  325. $Folder->delete();
  326. }
  327. }
  328. ?>