TestShellTest.php 9.9 KB

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