TestTaskTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console\Command\Task;
  16. use Cake\Console\Command\Task\TemplateTask;
  17. use Cake\Console\Command\Task\TestTask;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\ORM\Table;
  23. use Cake\ORM\TableRegistry;
  24. use Cake\TestSuite\TestCase;
  25. use TestApp\Controller\PostsController;
  26. use TestApp\Model\Table\ArticlesTable;
  27. /**
  28. * TestTaskTest class
  29. *
  30. */
  31. class TestTaskTest extends TestCase {
  32. /**
  33. * Fixtures
  34. *
  35. * @var string
  36. */
  37. public $fixtures = ['core.article', 'core.author',
  38. 'core.comment', 'core.articles_tag', 'core.tag'];
  39. /**
  40. * setUp method
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  47. $this->Task = $this->getMock('Cake\Console\Command\Task\TestTask',
  48. array('in', 'err', 'createFile', '_stop', 'isLoadableClass'),
  49. array($this->io)
  50. );
  51. $this->Task->name = 'Test';
  52. $this->Task->Template = new TemplateTask($this->io);
  53. $this->Task->Template->interactive = false;
  54. }
  55. /**
  56. * tearDown method
  57. *
  58. * @return void
  59. */
  60. public function tearDown() {
  61. parent::tearDown();
  62. unset($this->Task);
  63. Plugin::unload();
  64. }
  65. /**
  66. * Test that with no args execute() outputs the types you can generate
  67. * tests for.
  68. *
  69. * @return void
  70. */
  71. public function testExecuteNoArgsPrintsTypeOptions() {
  72. $this->Task = $this->getMockBuilder('Cake\Console\Command\Task\TestTask')
  73. ->disableOriginalConstructor()
  74. ->setMethods(['outputTypeChoices'])
  75. ->getMock();
  76. $this->Task->expects($this->once())
  77. ->method('outputTypeChoices');
  78. $this->Task->execute();
  79. }
  80. /**
  81. * Test outputTypeChoices method
  82. *
  83. * @return void
  84. */
  85. public function testOutputTypeChoices() {
  86. $this->io->expects($this->at(0))
  87. ->method('out')
  88. ->with($this->stringContains('You must provide'));
  89. $this->io->expects($this->at(1))
  90. ->method('out')
  91. ->with($this->stringContains('1. Entity'));
  92. $this->io->expects($this->at(2))
  93. ->method('out')
  94. ->with($this->stringContains('2. Table'));
  95. $this->io->expects($this->at(3))
  96. ->method('out')
  97. ->with($this->stringContains('3. Controller'));
  98. $this->Task->outputTypeChoices();
  99. }
  100. /**
  101. * Test that with no args execute() outputs the types you can generate
  102. * tests for.
  103. *
  104. * @return void
  105. */
  106. public function testExecuteOneArgPrintsClassOptions() {
  107. $this->Task = $this->getMockBuilder('Cake\Console\Command\Task\TestTask')
  108. ->disableOriginalConstructor()
  109. ->setMethods(['outputClassChoices'])
  110. ->getMock();
  111. $this->Task->expects($this->once())
  112. ->method('outputClassChoices');
  113. $this->Task->execute('Entity');
  114. }
  115. /**
  116. * test execute with type and class name defined
  117. *
  118. * @return void
  119. */
  120. public function testExecuteWithTwoArgs() {
  121. $this->Task->expects($this->once())->method('createFile')
  122. ->with(
  123. $this->stringContains('TestCase' . DS . 'Model' . DS . 'Table' . DS . 'TestTaskTagTableTest.php'),
  124. $this->stringContains('class TestTaskTagTableTest extends TestCase')
  125. );
  126. $this->Task->execute('Table', 'TestTaskTag');
  127. }
  128. /**
  129. * Test generating class options for table.
  130. *
  131. * @return void
  132. */
  133. public function testOutputClassOptionsForTable() {
  134. $this->io->expects($this->at(0))
  135. ->method('out')
  136. ->with($this->stringContains('You must provide'));
  137. $this->io->expects($this->at(1))
  138. ->method('out')
  139. ->with($this->stringContains('1. ArticlesTable'));
  140. $this->io->expects($this->at(2))
  141. ->method('out')
  142. ->with($this->stringContains('2. ArticlesTagsTable'));
  143. $this->io->expects($this->at(3))
  144. ->method('out')
  145. ->with($this->stringContains('3. AuthUsersTable'));
  146. $this->io->expects($this->at(4))
  147. ->method('out')
  148. ->with($this->stringContains('4. AuthorsTable'));
  149. $this->Task->outputClassChoices('Table');
  150. }
  151. /**
  152. * Test generating class options for table.
  153. *
  154. * @return void
  155. */
  156. public function testOutputClassOptionsForTablePlugin() {
  157. Plugin::load('TestPlugin');
  158. $this->Task->plugin = 'TestPlugin';
  159. $this->io->expects($this->at(0))
  160. ->method('out')
  161. ->with($this->stringContains('You must provide'));
  162. $this->io->expects($this->at(1))
  163. ->method('out')
  164. ->with($this->stringContains('1. TestPluginCommentsTable'));
  165. $this->Task->outputClassChoices('Table');
  166. }
  167. /**
  168. * Test that method introspection pulls all relevant non parent class
  169. * methods into the test case.
  170. *
  171. * @return void
  172. */
  173. public function testMethodIntrospection() {
  174. $result = $this->Task->getTestableMethods('TestApp\Model\Table\ArticlesTable');
  175. $expected = array('dosomething', 'dosomethingelse');
  176. $this->assertEquals($expected, array_map('strtolower', $result));
  177. }
  178. /**
  179. * test that the generation of fixtures works correctly.
  180. *
  181. * @return void
  182. */
  183. public function testFixtureArrayGenerationFromModel() {
  184. $subject = new ArticlesTable();
  185. $result = $this->Task->generateFixtureList($subject);
  186. $expected = [
  187. 'app.article',
  188. 'app.author',
  189. 'app.tag',
  190. 'app.articles_tag'
  191. ];
  192. $this->assertEquals($expected, $result);
  193. }
  194. /**
  195. * test that the generation of fixtures works correctly.
  196. *
  197. * @return void
  198. */
  199. public function testFixtureArrayGenerationFromController() {
  200. $subject = new PostsController();
  201. $result = $this->Task->generateFixtureList($subject);
  202. $expected = [
  203. 'app.post',
  204. ];
  205. $this->assertEquals($expected, $result);
  206. }
  207. /**
  208. * creating test subjects should clear the registry so the registry is always fresh
  209. *
  210. * @return void
  211. */
  212. public function testRegistryClearWhenBuildingTestObjects() {
  213. $articles = TableRegistry::get('Articles');
  214. $this->Task->buildTestSubject('Table', 'Posts');
  215. $this->assertFalse(TableRegistry::exists('Articles'));
  216. }
  217. /**
  218. * Test the user interaction for defining additional fixtures.
  219. *
  220. * @return void
  221. */
  222. public function testGetUserFixtures() {
  223. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  224. $this->Task->expects($this->at(1))->method('in')
  225. ->will($this->returnValue('app.pizza, app.topping, app.side_dish'));
  226. $result = $this->Task->getUserFixtures();
  227. $expected = array('app.pizza', 'app.topping', 'app.side_dish');
  228. $this->assertEquals($expected, $result);
  229. }
  230. /**
  231. * Dataprovider for class name generation.
  232. *
  233. * @return array
  234. */
  235. public static function realClassProvider() {
  236. return [
  237. ['Entity', 'Article', 'App\Model\Entity\Article'],
  238. ['entity', 'ArticleEntity', 'App\Model\Entity\ArticleEntity'],
  239. ['Table', 'Posts', 'App\Model\Table\PostsTable'],
  240. ['table', 'PostsTable', 'App\Model\Table\PostsTable'],
  241. ['Controller', 'Posts', 'App\Controller\PostsController'],
  242. ['controller', 'PostsController', 'App\Controller\PostsController'],
  243. ['Behavior', 'Timestamp', 'App\Model\Behavior\TimestampBehavior'],
  244. ['behavior', 'TimestampBehavior', 'App\Model\Behavior\TimestampBehavior'],
  245. ['Helper', 'Form', 'App\View\Helper\FormHelper'],
  246. ['helper', 'FormHelper', 'App\View\Helper\FormHelper'],
  247. ['Component', 'Auth', 'App\Controller\Component\AuthComponent'],
  248. ['component', 'AuthComponent', 'App\Controller\Component\AuthComponent'],
  249. ];
  250. }
  251. /**
  252. * test that resolving class names works
  253. *
  254. * @dataProvider realClassProvider
  255. * @return void
  256. */
  257. public function testGetRealClassname($type, $name, $expected) {
  258. $result = $this->Task->getRealClassname($type, $name);
  259. $this->assertEquals($expected, $result);
  260. }
  261. /**
  262. * test resolving class names with plugins
  263. *
  264. * @return void
  265. */
  266. public function testGetRealClassnamePlugin() {
  267. Plugin::load('TestPlugin');
  268. $this->Task->plugin = 'TestPlugin';
  269. $result = $this->Task->getRealClassname('Helper', 'Asset');
  270. $expected = 'TestPlugin\View\Helper\AssetHelper';
  271. $this->assertEquals($expected, $result);
  272. }
  273. /**
  274. * Test baking a test for a concrete model with fixtures arg
  275. *
  276. * @return void
  277. */
  278. public function testBakeFixturesParam() {
  279. $this->Task->expects($this->once())
  280. ->method('createFile')
  281. ->will($this->returnValue(true));
  282. $this->Task->params['fixtures'] = 'app.post, app.comments , app.user ,';
  283. $result = $this->Task->bake('Table', 'Articles');
  284. $this->assertContains('public $fixtures = [', $result);
  285. $this->assertContains('app.post', $result);
  286. $this->assertContains('app.comments', $result);
  287. $this->assertContains('app.user', $result);
  288. $this->assertNotContains("''", $result);
  289. }
  290. /**
  291. * Test baking a test for a concrete model.
  292. *
  293. * @return void
  294. */
  295. public function testBakeModelTest() {
  296. $this->Task->expects($this->once())
  297. ->method('createFile')
  298. ->will($this->returnValue(true));
  299. $result = $this->Task->bake('Table', 'Articles');
  300. $this->assertContains("use App\Model\Table\ArticlesTable", $result);
  301. $this->assertContains('class ArticlesTableTest extends TestCase', $result);
  302. $this->assertContains('function setUp()', $result);
  303. $this->assertContains("\$config = TableRegistry::exists('Articles') ?", $result);
  304. $this->assertContains("\$this->Articles = TableRegistry::get('Articles', \$config", $result);
  305. $this->assertContains('function tearDown()', $result);
  306. $this->assertContains('unset($this->Articles)', $result);
  307. }
  308. /**
  309. * test baking controller test files
  310. *
  311. * @return void
  312. */
  313. public function testBakeControllerTest() {
  314. Configure::write('App.namespace', 'TestApp');
  315. $this->Task->expects($this->once())
  316. ->method('createFile')
  317. ->will($this->returnValue(true));
  318. $result = $this->Task->bake('Controller', 'PostsController');
  319. $this->assertContains("use TestApp\Controller\PostsController", $result);
  320. $this->assertContains('class PostsControllerTest extends ControllerTestCase', $result);
  321. $this->assertNotContains('function setUp()', $result);
  322. $this->assertNotContains("\$this->Posts = new PostsController()", $result);
  323. $this->assertNotContains("\$this->Posts->constructClasses()", $result);
  324. $this->assertNotContains('function tearDown()', $result);
  325. $this->assertNotContains('unset($this->Posts)', $result);
  326. $this->assertContains("'app.post'", $result);
  327. }
  328. /**
  329. * test baking controller test files
  330. *
  331. * @return void
  332. */
  333. public function testBakePrefixControllerTest() {
  334. Configure::write('App.namespace', 'TestApp');
  335. $this->Task->expects($this->once())
  336. ->method('createFile')
  337. ->with($this->stringContains('Controller' . DS . 'Admin' . DS . 'PostsControllerTest.php'))
  338. ->will($this->returnValue(true));
  339. $result = $this->Task->bake('controller', 'Admin\Posts');
  340. $this->assertContains("use TestApp\Controller\Admin\PostsController", $result);
  341. $this->assertContains('class PostsControllerTest extends ControllerTestCase', $result);
  342. $this->assertNotContains('function setUp()', $result);
  343. $this->assertNotContains("\$this->Posts = new PostsController()", $result);
  344. $this->assertNotContains("\$this->Posts->constructClasses()", $result);
  345. $this->assertNotContains('function tearDown()', $result);
  346. $this->assertNotContains('unset($this->Posts)', $result);
  347. }
  348. /**
  349. * test baking component test files,
  350. *
  351. * @return void
  352. */
  353. public function testBakeComponentTest() {
  354. Configure::write('App.namespace', 'TestApp');
  355. $this->Task->expects($this->once())
  356. ->method('createFile')
  357. ->will($this->returnValue(true));
  358. $result = $this->Task->bake('Component', 'Apple');
  359. $this->assertContains('use Cake\Controller\ComponentRegistry', $result);
  360. $this->assertContains('use TestApp\Controller\Component\AppleComponent', $result);
  361. $this->assertContains('class AppleComponentTest extends TestCase', $result);
  362. $this->assertContains('function setUp()', $result);
  363. $this->assertContains("\$registry = new ComponentRegistry()", $result);
  364. $this->assertContains("\$this->Apple = new AppleComponent(\$registry)", $result);
  365. $this->assertContains('function testStartup()', $result);
  366. $this->assertContains('$this->markTestIncomplete(\'testStartup not implemented.\')', $result);
  367. $this->assertContains('function tearDown()', $result);
  368. $this->assertContains('unset($this->Apple)', $result);
  369. }
  370. /**
  371. * test baking behavior test files,
  372. *
  373. * @return void
  374. */
  375. public function testBakeBehaviorTest() {
  376. $this->Task->expects($this->once())
  377. ->method('createFile')
  378. ->will($this->returnValue(true));
  379. $result = $this->Task->bake('Behavior', 'Example');
  380. $this->assertContains("use App\Model\Behavior\ExampleBehavior;", $result);
  381. $this->assertContains('class ExampleBehaviorTest extends TestCase', $result);
  382. $this->assertContains('function setUp()', $result);
  383. $this->assertContains("\$this->Example = new ExampleBehavior()", $result);
  384. $this->assertContains('function tearDown()', $result);
  385. $this->assertContains('unset($this->Example)', $result);
  386. }
  387. /**
  388. * test baking helper test files,
  389. *
  390. * @return void
  391. */
  392. public function testBakeHelperTest() {
  393. $this->Task->expects($this->once())
  394. ->method('createFile')
  395. ->will($this->returnValue(true));
  396. $result = $this->Task->bake('Helper', 'Example');
  397. $this->assertContains("use Cake\View\View;", $result);
  398. $this->assertContains("use App\View\Helper\ExampleHelper;", $result);
  399. $this->assertContains('class ExampleHelperTest extends TestCase', $result);
  400. $this->assertContains('function setUp()', $result);
  401. $this->assertContains("\$view = new View()", $result);
  402. $this->assertContains("\$this->Example = new ExampleHelper(\$view)", $result);
  403. $this->assertContains('function tearDown()', $result);
  404. $this->assertContains('unset($this->Example)', $result);
  405. }
  406. /**
  407. * test Constructor generation ensure that constructClasses is called for controllers
  408. *
  409. * @return void
  410. */
  411. public function testGenerateConstructor() {
  412. $result = $this->Task->generateConstructor('controller', 'PostsController');
  413. $expected = ['', '', ''];
  414. $this->assertEquals($expected, $result);
  415. $result = $this->Task->generateConstructor('table', 'App\Model\\Table\PostsTable');
  416. $expected = [
  417. "\$config = TableRegistry::exists('Posts') ? [] : ['className' => 'App\Model\\Table\PostsTable'];\n",
  418. "TableRegistry::get('Posts', \$config);\n",
  419. ''
  420. ];
  421. $this->assertEquals($expected, $result);
  422. $result = $this->Task->generateConstructor('helper', 'FormHelper');
  423. $expected = ["\$view = new View();\n", "new FormHelper(\$view);\n", ''];
  424. $this->assertEquals($expected, $result);
  425. $result = $this->Task->generateConstructor('entity', 'TestPlugin\Model\Entity\Article');
  426. $expected = ["", "new Article();\n", ''];
  427. $this->assertEquals($expected, $result);
  428. }
  429. /**
  430. * Test generateUses()
  431. *
  432. * @return void
  433. */
  434. public function testGenerateUses() {
  435. $result = $this->Task->generateUses('table', 'App\Model\Table\PostsTable');
  436. $expected = array(
  437. 'Cake\ORM\TableRegistry',
  438. 'App\Model\Table\PostsTable',
  439. );
  440. $this->assertEquals($expected, $result);
  441. $result = $this->Task->generateUses('controller', 'App\Controller\PostsController');
  442. $expected = array(
  443. 'App\Controller\PostsController',
  444. );
  445. $this->assertEquals($expected, $result);
  446. $result = $this->Task->generateUses('helper', 'App\View\Helper\FormHelper');
  447. $expected = array(
  448. 'Cake\View\View',
  449. 'App\View\Helper\FormHelper',
  450. );
  451. $this->assertEquals($expected, $result);
  452. $result = $this->Task->generateUses('component', 'App\Controller\Component\AuthComponent');
  453. $expected = array(
  454. 'Cake\Controller\ComponentRegistry',
  455. 'App\Controller\Component\AuthComponent',
  456. );
  457. $this->assertEquals($expected, $result);
  458. }
  459. /**
  460. * Test that mock class generation works for the appropriate classes
  461. *
  462. * @return void
  463. */
  464. public function testMockClassGeneration() {
  465. $result = $this->Task->hasMockClass('controller');
  466. $this->assertTrue($result);
  467. }
  468. /**
  469. * test bake() with a -plugin param
  470. *
  471. * @return void
  472. */
  473. public function testBakeWithPlugin() {
  474. $this->Task->plugin = 'TestPlugin';
  475. Plugin::load('TestPlugin');
  476. $path = TEST_APP . 'Plugin/TestPlugin/Test/TestCase/View/Helper/FormHelperTest.php';
  477. $path = str_replace('/', DS, $path);
  478. $this->Task->expects($this->once())->method('createFile')
  479. ->with($path, $this->anything());
  480. $this->Task->bake('Helper', 'Form');
  481. }
  482. /**
  483. * Provider for test case file names.
  484. *
  485. * @return array
  486. */
  487. public static function caseFileNameProvider() {
  488. return array(
  489. array('Table', 'App\Model\Table\PostsTable', 'TestCase/Model/Table/PostsTableTest.php'),
  490. array('Entity', 'App\Model\Entity\Article', 'TestCase/Model/Entity/ArticleTest.php'),
  491. array('Helper', 'App\View\Helper\FormHelper', 'TestCase/View/Helper/FormHelperTest.php'),
  492. array('Controller', 'App\Controller\PostsController', 'TestCase/Controller/PostsControllerTest.php'),
  493. array('Controller', 'App\Controller\Admin\PostsController', 'TestCase/Controller/Admin/PostsControllerTest.php'),
  494. array('Behavior', 'App\Model\Behavior\TreeBehavior', 'TestCase/Model/Behavior/TreeBehaviorTest.php'),
  495. array('Component', 'App\Controller\Component\AuthComponent', 'TestCase/Controller/Component/AuthComponentTest.php'),
  496. array('entity', 'App\Model\Entity\Article', 'TestCase/Model/Entity/ArticleTest.php'),
  497. array('table', 'App\Model\Table\PostsTable', 'TestCase/Model/Table/PostsTableTest.php'),
  498. array('helper', 'App\View\Helper\FormHelper', 'TestCase/View/Helper/FormHelperTest.php'),
  499. array('controller', 'App\Controller\PostsController', 'TestCase/Controller/PostsControllerTest.php'),
  500. array('behavior', 'App\Model\Behavior\TreeBehavior', 'TestCase/Model/Behavior/TreeBehaviorTest.php'),
  501. array('component', 'App\Controller\Component\AuthComponent', 'TestCase/Controller/Component/AuthComponentTest.php'),
  502. );
  503. }
  504. /**
  505. * Test filename generation for each type + plugins
  506. *
  507. * @dataProvider caseFileNameProvider
  508. * @return void
  509. */
  510. public function testTestCaseFileName($type, $class, $expected) {
  511. $result = $this->Task->testCaseFileName($type, $class);
  512. $expected = ROOT . DS . 'Test/' . $expected;
  513. $this->assertPathEquals($expected, $result);
  514. }
  515. /**
  516. * Test filename generation for plugins.
  517. *
  518. * @return void
  519. */
  520. public function testTestCaseFileNamePlugin() {
  521. $this->Task->path = DS . 'my/path/tests/';
  522. Plugin::load('TestPlugin');
  523. $this->Task->plugin = 'TestPlugin';
  524. $class = 'TestPlugin\Model\Entity\Post';
  525. $result = $this->Task->testCaseFileName('entity', $class);
  526. $expected = TEST_APP . 'Plugin/TestPlugin/Test/TestCase/Model/Entity/PostTest.php';
  527. $this->assertPathEquals($expected, $result);
  528. }
  529. /**
  530. * Data provider for mapType() tests.
  531. *
  532. * @return array
  533. */
  534. public static function mapTypeProvider() {
  535. return array(
  536. array('controller', 'Controller'),
  537. array('Controller', 'Controller'),
  538. array('component', 'Controller\Component'),
  539. array('Component', 'Controller\Component'),
  540. array('table', 'Model\Table'),
  541. array('Table', 'Model\Table'),
  542. array('entity', 'Model\Entity'),
  543. array('Entity', 'Model\Entity'),
  544. array('behavior', 'Model\Behavior'),
  545. array('Behavior', 'Model\Behavior'),
  546. array('helper', 'View\Helper'),
  547. array('Helper', 'View\Helper'),
  548. array('Helper', 'View\Helper'),
  549. );
  550. }
  551. /**
  552. * Test that mapType returns the correct package names.
  553. *
  554. * @dataProvider mapTypeProvider
  555. * @return void
  556. */
  557. public function testMapType($original, $expected) {
  558. $this->assertEquals($expected, $this->Task->mapType($original));
  559. }
  560. }