TestShellTest.php 9.8 KB

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