TestShellTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * TestSuiteShell test case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Console.Command
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ShellDispatcher', 'Console');
  20. App::uses('TestShell', 'Console/Command');
  21. class TestTestShell extends TestShell {
  22. public function mapFileToCase($file, $category, $throwOnMissingFile = true) {
  23. return $this->_mapFileToCase($file, $category, $throwOnMissingFile);
  24. }
  25. public function mapFileToCategory($file) {
  26. return $this->_mapFileToCategory($file);
  27. }
  28. }
  29. class TestShellTest extends CakeTestCase {
  30. /**
  31. * setUp test case
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  37. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  38. $this->Shell = $this->getMock(
  39. 'TestTestShell',
  40. array('in', 'out', 'hr', 'help', 'error', 'err', '_stop', 'initialize', '_run', 'clear'),
  41. array($out, $out, $in)
  42. );
  43. $this->Shell->OptionParser = $this->getMock('ConsoleOptionParser', array(), array(null, false));
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown() {
  51. unset($this->Dispatch, $this->Shell);
  52. }
  53. /**
  54. * testMapCoreFileToCategory
  55. *
  56. * @return void
  57. */
  58. public function testMapCoreFileToCategory() {
  59. $this->Shell->startup();
  60. $return = $this->Shell->mapFileToCategory('lib/Cake/basics.php');
  61. $this->assertSame('core', $return);
  62. $return = $this->Shell->mapFileToCategory('lib/Cake/Core/App.php');
  63. $this->assertSame('core', $return);
  64. $return = $this->Shell->mapFileToCategory('lib/Cake/Some/Deeply/Nested/Structure.php');
  65. $this->assertSame('core', $return);
  66. }
  67. /**
  68. * testMapCoreFileToCase
  69. *
  70. * basics.php is a slightly special case - it's the only file in the core with a test that isn't Capitalized
  71. *
  72. * @return void
  73. */
  74. public function testMapCoreFileToCase() {
  75. $this->Shell->startup();
  76. $return = $this->Shell->mapFileToCase('lib/Cake/basics.php', 'core');
  77. $this->assertSame('Basics', $return);
  78. $return = $this->Shell->mapFileToCase('lib/Cake/Core/App.php', 'core');
  79. $this->assertSame('Core/App', $return);
  80. $return = $this->Shell->mapFileToCase('lib/Cake/Some/Deeply/Nested/Structure.php', 'core', false);
  81. $this->assertSame('Some/Deeply/Nested/Structure', $return);
  82. }
  83. /**
  84. * testMapAppFileToCategory
  85. *
  86. * @return void
  87. */
  88. public function testMapAppFileToCategory() {
  89. $this->Shell->startup();
  90. $return = $this->Shell->mapFileToCategory(APP . 'Controller/ExampleController.php');
  91. $this->assertSame('app', $return);
  92. $return = $this->Shell->mapFileToCategory(APP . 'My/File/Is/Here.php');
  93. $this->assertSame('app', $return);
  94. }
  95. /**
  96. * testMapAppFileToCase
  97. *
  98. * @return void
  99. */
  100. public function testMapAppFileToCase() {
  101. $this->Shell->startup();
  102. $return = $this->Shell->mapFileToCase(APP . 'Controller/ExampleController.php', 'app', false);
  103. $this->assertSame('Controller/ExampleController', $return);
  104. $return = $this->Shell->mapFileToCase(APP . 'My/File/Is/Here.php', 'app', false);
  105. $this->assertSame('My/File/Is/Here', $return);
  106. }
  107. /**
  108. * testMapPluginFileToCategory
  109. *
  110. * @return void
  111. */
  112. public function testMapPluginFileToCategory() {
  113. $this->Shell->startup();
  114. $return = $this->Shell->mapFileToCategory(APP . 'Plugin/awesome/Controller/ExampleController.php');
  115. $this->assertSame('awesome', $return);
  116. $return = $this->Shell->mapFileToCategory(dirname(CAKE) . 'plugins/awesome/Controller/ExampleController.php');
  117. $this->assertSame('awesome', $return);
  118. }
  119. /**
  120. * testMapPluginFileToCase
  121. *
  122. * @return void
  123. */
  124. public function testMapPluginFileToCase() {
  125. $this->Shell->startup();
  126. $return = $this->Shell->mapFileToCase(APP . 'Plugin/awesome/Controller/ExampleController.php', 'awesome', false);
  127. $this->assertSame('Controller/ExampleController', $return);
  128. $return = $this->Shell->mapFileToCase(dirname(CAKE) . 'plugins/awesome/Controller/ExampleController.php', 'awesome', false);
  129. $this->assertSame('Controller/ExampleController', $return);
  130. }
  131. /**
  132. * testMapCoreTestToCategory
  133. *
  134. * @return void
  135. */
  136. public function testMapCoreTestToCategory() {
  137. $this->Shell->startup();
  138. $return = $this->Shell->mapFileToCategory('lib/Cake/Test/Case/BasicsTest.php');
  139. $this->assertSame('core', $return);
  140. $return = $this->Shell->mapFileToCategory('lib/Cake/Test/Case/BasicsTest.php');
  141. $this->assertSame('core', $return);
  142. $return = $this->Shell->mapFileToCategory('lib/Cake/Test/Case/Some/Deeply/Nested/StructureTest.php');
  143. $this->assertSame('core', $return);
  144. }
  145. /**
  146. * testMapCoreTestToCase
  147. *
  148. * basics.php is a slightly special case - it's the only file in the core with a test that isn't Capitalized
  149. *
  150. * @return void
  151. */
  152. public function testMapCoreTestToCase() {
  153. $this->Shell->startup();
  154. $return = $this->Shell->mapFileToCase('lib/Cake/Test/Case/BasicsTest.php', 'core');
  155. $this->assertSame('Basics', $return);
  156. $return = $this->Shell->mapFileToCase('lib/Cake/Test/Case/Core/AppTest.php', 'core');
  157. $this->assertSame('Core/App', $return);
  158. $return = $this->Shell->mapFileToCase('lib/Cake/Test/Case/Some/Deeply/Nested/StructureTest.php', 'core', false);
  159. $this->assertSame('Some/Deeply/Nested/Structure', $return);
  160. }
  161. /**
  162. * testMapAppTestToCategory
  163. *
  164. * @return void
  165. */
  166. public function testMapAppTestToCategory() {
  167. $this->Shell->startup();
  168. $return = $this->Shell->mapFileToCategory(APP . 'Test/Case/Controller/ExampleControllerTest.php');
  169. $this->assertSame('app', $return);
  170. $return = $this->Shell->mapFileToCategory(APP . 'Test/Case/My/File/Is/HereTest.php');
  171. $this->assertSame('app', $return);
  172. }
  173. /**
  174. * testMapAppTestToCase
  175. *
  176. * @return void
  177. */
  178. public function testMapAppTestToCase() {
  179. $this->Shell->startup();
  180. $return = $this->Shell->mapFileToCase(APP . 'Test/Case/Controller/ExampleControllerTest.php', 'app', false);
  181. $this->assertSame('Controller/ExampleController', $return);
  182. $return = $this->Shell->mapFileToCase(APP . 'Test/Case/My/File/Is/HereTest.php', 'app', false);
  183. $this->assertSame('My/File/Is/Here', $return);
  184. }
  185. /**
  186. * testMapPluginTestToCategory
  187. *
  188. * @return void
  189. */
  190. public function testMapPluginTestToCategory() {
  191. $this->Shell->startup();
  192. $return = $this->Shell->mapFileToCategory(APP . 'Plugin/awesome/Test/Case/Controller/ExampleControllerTest.php');
  193. $this->assertSame('awesome', $return);
  194. $return = $this->Shell->mapFileToCategory(dirname(CAKE) . 'plugins/awesome/Test/Case/Controller/ExampleControllerTest.php');
  195. $this->assertSame('awesome', $return);
  196. }
  197. /**
  198. * testMapPluginTestToCase
  199. *
  200. * @return void
  201. */
  202. public function testMapPluginTestToCase() {
  203. $this->Shell->startup();
  204. $return = $this->Shell->mapFileToCase(APP . 'Plugin/awesome/Test/Case/Controller/ExampleControllerTest.php', 'awesome', false);
  205. $this->assertSame('Controller/ExampleController', $return);
  206. $return = $this->Shell->mapFileToCase(dirname(CAKE) . 'plugins/awesome/Test/Case/Controller/ExampleControllerTest.php', 'awesome', false);
  207. $this->assertSame('Controller/ExampleController', $return);
  208. }
  209. /**
  210. * testMapNotTestToNothing
  211. *
  212. * @return void
  213. */
  214. public function testMapNotTestToNothing() {
  215. $this->Shell->startup();
  216. $return = $this->Shell->mapFileToCategory(APP . 'Test/Case/NotATestFile.php');
  217. $this->assertSame('app', $return);
  218. $return = $this->Shell->mapFileToCase(APP . 'Test/Case/NotATestFile.php', false, false);
  219. $this->assertFalse($return);
  220. $return = $this->Shell->mapFileToCategory(APP . 'Test/Fixture/SomeTest.php');
  221. $this->assertSame('app', $return);
  222. $return = $this->Shell->mapFileToCase(APP . 'Test/Fixture/SomeTest.php', false, false);
  223. $this->assertFalse($return);
  224. }
  225. /**
  226. * test available list of test cases for an empty category
  227. *
  228. * @return void
  229. */
  230. public function testAvailableWithEmptyList() {
  231. $this->Shell->startup();
  232. $this->Shell->args = array('unexistant-category');
  233. $this->Shell->expects($this->at(0))->method('out')->with(__d('cake_console', "No test cases available \n\n"));
  234. $this->Shell->OptionParser->expects($this->once())->method('help');
  235. $this->Shell->available();
  236. }
  237. /**
  238. * test available list of test cases for core category
  239. *
  240. * @return void
  241. */
  242. public function testAvailableCoreCategory() {
  243. $this->Shell->startup();
  244. $this->Shell->args = array('core');
  245. $this->Shell->expects($this->at(0))->method('out')->with('Core Test Cases:');
  246. $this->Shell->expects($this->at(1))->method('out')
  247. ->with($this->stringContains('[1]'));
  248. $this->Shell->expects($this->at(2))->method('out')
  249. ->with($this->stringContains('[2]'));
  250. $this->Shell->expects($this->once())->method('in')
  251. ->with(__d('cake_console', 'What test case would you like to run?'), null, 'q')
  252. ->will($this->returnValue('1'));
  253. $this->Shell->expects($this->once())->method('_run');
  254. $this->Shell->available();
  255. $this->assertEquals($this->Shell->args, array('core', 'AllBehaviors'));
  256. }
  257. /**
  258. * Tests that correct option for test runner are passed
  259. *
  260. * @return void
  261. */
  262. public function testRunnerOptions() {
  263. $this->Shell->startup();
  264. $this->Shell->args = array('core', 'Basics');
  265. $this->Shell->params = array('filter' => 'myFilter', 'colors' => true, 'verbose' => true);
  266. $this->Shell->expects($this->once())->method('_run')
  267. ->with(
  268. array('app' => false, 'plugin' => null, 'core' => true, 'output' => 'text', 'case' => 'Basics'),
  269. array('--filter', 'myFilter', '--colors', '--verbose')
  270. );
  271. $this->Shell->main();
  272. }
  273. }