TestShellTest.php 9.7 KB

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