/lib/Cake/Test/Case/Console/Command/TestShellTest.php

https://github.com/kunit/cakephp · PHP · 346 lines · 164 code · 60 blank · 122 comment · 0 complexity · 8197798868659286b40bb2a23425af4f MD5 · raw file

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