TestTaskTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. /**
  3. * TestTaskTest file
  4. *
  5. * Test Case for test generation shell task
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP Project
  18. * @package Cake.Test.Case.Console.Command.Task
  19. * @since CakePHP v 1.2.0.7726
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('ShellDispatcher', 'Console');
  23. App::uses('ConsoleOutput', 'Console');
  24. App::uses('ConsoleInput', 'Console');
  25. App::uses('Shell', 'Console');
  26. App::uses('TestTask', 'Console/Command/Task');
  27. App::uses('TemplateTask', 'Console/Command/Task');
  28. App::uses('Controller', 'Controller');
  29. App::uses('Model', 'Model');
  30. /**
  31. * Test Article model
  32. *
  33. * @package Cake.Test.Case.Console.Command.Task
  34. * @package Cake.Test.Case.Console.Command.Task
  35. */
  36. class TestTaskArticle extends Model {
  37. /**
  38. * Model name
  39. *
  40. * @var string
  41. */
  42. public $name = 'TestTaskArticle';
  43. /**
  44. * Table name to use
  45. *
  46. * @var string
  47. */
  48. public $useTable = 'articles';
  49. /**
  50. * HasMany Associations
  51. *
  52. * @var array
  53. */
  54. public $hasMany = array(
  55. 'Comment' => array(
  56. 'className' => 'TestTask.TestTaskComment',
  57. 'foreignKey' => 'article_id',
  58. )
  59. );
  60. /**
  61. * Has and Belongs To Many Associations
  62. *
  63. * @var array
  64. */
  65. public $hasAndBelongsToMany = array(
  66. 'Tag' => array(
  67. 'className' => 'TestTaskTag',
  68. 'joinTable' => 'articles_tags',
  69. 'foreignKey' => 'article_id',
  70. 'associationForeignKey' => 'tag_id'
  71. )
  72. );
  73. /**
  74. * Example public method
  75. *
  76. * @return void
  77. */
  78. public function doSomething() {
  79. }
  80. /**
  81. * Example Secondary public method
  82. *
  83. * @return void
  84. */
  85. public function doSomethingElse() {
  86. }
  87. /**
  88. * Example protected method
  89. *
  90. * @return void
  91. */
  92. protected function _innerMethod() {
  93. }
  94. }
  95. /**
  96. * Tag Testing Model
  97. *
  98. * @package Cake.Test.Case.Console.Command.Task
  99. * @package Cake.Test.Case.Console.Command.Task
  100. */
  101. class TestTaskTag extends Model {
  102. /**
  103. * Model name
  104. *
  105. * @var string
  106. */
  107. public $name = 'TestTaskTag';
  108. /**
  109. * Table name
  110. *
  111. * @var string
  112. */
  113. public $useTable = 'tags';
  114. /**
  115. * Has and Belongs To Many Associations
  116. *
  117. * @var array
  118. */
  119. public $hasAndBelongsToMany = array(
  120. 'Article' => array(
  121. 'className' => 'TestTaskArticle',
  122. 'joinTable' => 'articles_tags',
  123. 'foreignKey' => 'tag_id',
  124. 'associationForeignKey' => 'article_id'
  125. )
  126. );
  127. }
  128. /**
  129. * Simulated plugin
  130. *
  131. * @package Cake.Test.Case.Console.Command.Task
  132. * @package Cake.Test.Case.Console.Command.Task
  133. */
  134. class TestTaskAppModel extends Model {
  135. }
  136. /**
  137. * Testing AppMode (TaskComment)
  138. *
  139. * @package Cake.Test.Case.Console.Command.Task
  140. * @package Cake.Test.Case.Console.Command.Task
  141. */
  142. class TestTaskComment extends TestTaskAppModel {
  143. /**
  144. * Model name
  145. *
  146. * @var string
  147. */
  148. public $name = 'TestTaskComment';
  149. /**
  150. * Table name
  151. *
  152. * @var string
  153. */
  154. public $useTable = 'comments';
  155. /**
  156. * Belongs To Associations
  157. *
  158. * @var array
  159. */
  160. public $belongsTo = array(
  161. 'Article' => array(
  162. 'className' => 'TestTaskArticle',
  163. 'foreignKey' => 'article_id',
  164. )
  165. );
  166. }
  167. /**
  168. * Test Task Comments Controller
  169. *
  170. * @package Cake.Test.Case.Console.Command.Task
  171. * @package Cake.Test.Case.Console.Command.Task
  172. */
  173. class TestTaskCommentsController extends Controller {
  174. /**
  175. * Controller Name
  176. *
  177. * @var string
  178. */
  179. public $name = 'TestTaskComments';
  180. /**
  181. * Models to use
  182. *
  183. * @var array
  184. */
  185. public $uses = array('TestTaskComment', 'TestTaskTag');
  186. }
  187. /**
  188. * TestTaskTest class
  189. *
  190. * @package Cake.Test.Case.Console.Command.Task
  191. */
  192. class TestTaskTest extends CakeTestCase {
  193. /**
  194. * Fixtures
  195. *
  196. * @var string
  197. */
  198. public $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
  199. /**
  200. * setUp method
  201. *
  202. * @return void
  203. */
  204. public function setUp() {
  205. parent::setUp();
  206. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  207. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  208. $this->Task = $this->getMock('TestTask',
  209. array('in', 'err', 'createFile', '_stop', 'isLoadableClass'),
  210. array($out, $out, $in)
  211. );
  212. $this->Task->name = 'Test';
  213. $this->Task->Template = new TemplateTask($out, $out, $in);
  214. }
  215. /**
  216. * tearDown method
  217. *
  218. * @return void
  219. */
  220. public function tearDown() {
  221. parent::tearDown();
  222. unset($this->Task);
  223. CakePlugin::unload();
  224. }
  225. /**
  226. * Test that file path generation doesn't continuously append paths.
  227. *
  228. * @return void
  229. */
  230. public function testFilePathGenerationModelRepeated() {
  231. $this->Task->expects($this->never())->method('err');
  232. $this->Task->expects($this->never())->method('_stop');
  233. $file = TESTS . 'Case' . DS . 'Model' . DS . 'MyClassTest.php';
  234. $this->Task->expects($this->at(1))->method('createFile')
  235. ->with($file, $this->anything());
  236. $this->Task->expects($this->at(3))->method('createFile')
  237. ->with($file, $this->anything());
  238. $file = TESTS . 'Case' . DS . 'Controller' . DS . 'CommentsControllerTest.php';
  239. $this->Task->expects($this->at(5))->method('createFile')
  240. ->with($file, $this->anything());
  241. $this->Task->bake('Model', 'MyClass');
  242. $this->Task->bake('Model', 'MyClass');
  243. $this->Task->bake('Controller', 'Comments');
  244. }
  245. /**
  246. * Test that method introspection pulls all relevant non parent class
  247. * methods into the test case.
  248. *
  249. * @return void
  250. */
  251. public function testMethodIntrospection() {
  252. $result = $this->Task->getTestableMethods('TestTaskArticle');
  253. $expected = array('dosomething', 'dosomethingelse');
  254. $this->assertEquals($expected, array_map('strtolower', $result));
  255. }
  256. /**
  257. * test that the generation of fixtures works correctly.
  258. *
  259. * @return void
  260. */
  261. public function testFixtureArrayGenerationFromModel() {
  262. $subject = ClassRegistry::init('TestTaskArticle');
  263. $result = $this->Task->generateFixtureList($subject);
  264. $expected = array('plugin.test_task.test_task_comment', 'app.articles_tags',
  265. 'app.test_task_article', 'app.test_task_tag');
  266. $this->assertEquals(sort($expected), sort($result));
  267. }
  268. /**
  269. * test that the generation of fixtures works correctly.
  270. *
  271. * @return void
  272. */
  273. public function testFixtureArrayGenerationFromController() {
  274. $subject = new TestTaskCommentsController();
  275. $result = $this->Task->generateFixtureList($subject);
  276. $expected = array('plugin.test_task.test_task_comment', 'app.articles_tags',
  277. 'app.test_task_article', 'app.test_task_tag');
  278. $this->assertEquals(sort($expected), sort($result));
  279. }
  280. /**
  281. * test user interaction to get object type
  282. *
  283. * @return void
  284. */
  285. public function testGetObjectType() {
  286. $this->Task->expects($this->once())->method('_stop');
  287. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('q'));
  288. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue(2));
  289. $this->Task->getObjectType();
  290. $result = $this->Task->getObjectType();
  291. $this->assertEquals($this->Task->classTypes['Controller'], $result);
  292. }
  293. /**
  294. * creating test subjects should clear the registry so the registry is always fresh
  295. *
  296. * @return void
  297. */
  298. public function testRegistryClearWhenBuildingTestObjects() {
  299. ClassRegistry::flush();
  300. $model = ClassRegistry::init('TestTaskComment');
  301. $model->bindModel(array(
  302. 'belongsTo' => array(
  303. 'Random' => array(
  304. 'className' => 'TestTaskArticle',
  305. 'foreignKey' => 'article_id',
  306. )
  307. )
  308. ));
  309. $keys = ClassRegistry::keys();
  310. $this->assertTrue(in_array('test_task_comment', $keys));
  311. $this->Task->buildTestSubject('Model', 'TestTaskComment');
  312. $keys = ClassRegistry::keys();
  313. $this->assertFalse(in_array('random', $keys));
  314. }
  315. /**
  316. * test that getClassName returns the user choice as a classname.
  317. *
  318. * @return void
  319. */
  320. public function testGetClassName() {
  321. $objects = App::objects('model');
  322. $this->skipIf(empty($objects), 'No models in app.');
  323. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('MyCustomClass'));
  324. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(1));
  325. $result = $this->Task->getClassName('Model');
  326. $this->assertEquals('MyCustomClass', $result);
  327. $result = $this->Task->getClassName('Model');
  328. $options = App::objects('model');
  329. $this->assertEquals($options[0], $result);
  330. }
  331. /**
  332. * Test the user interaction for defining additional fixtures.
  333. *
  334. * @return void
  335. */
  336. public function testGetUserFixtures() {
  337. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  338. $this->Task->expects($this->at(1))->method('in')
  339. ->will($this->returnValue('app.pizza, app.topping, app.side_dish'));
  340. $result = $this->Task->getUserFixtures();
  341. $expected = array('app.pizza', 'app.topping', 'app.side_dish');
  342. $this->assertEquals($expected, $result);
  343. }
  344. /**
  345. * test that resolving classnames works
  346. *
  347. * @return void
  348. */
  349. public function testGetRealClassname() {
  350. $result = $this->Task->getRealClassname('Model', 'Post');
  351. $this->assertEquals('Post', $result);
  352. $result = $this->Task->getRealClassname('Controller', 'Posts');
  353. $this->assertEquals('PostsController', $result);
  354. $result = $this->Task->getRealClassname('Controller', 'PostsController');
  355. $this->assertEquals('PostsController', $result);
  356. $result = $this->Task->getRealClassname('Controller', 'AlertTypes');
  357. $this->assertEquals('AlertTypesController', $result);
  358. $result = $this->Task->getRealClassname('Helper', 'Form');
  359. $this->assertEquals('FormHelper', $result);
  360. $result = $this->Task->getRealClassname('Helper', 'FormHelper');
  361. $this->assertEquals('FormHelper', $result);
  362. $result = $this->Task->getRealClassname('Behavior', 'Containable');
  363. $this->assertEquals('ContainableBehavior', $result);
  364. $result = $this->Task->getRealClassname('Behavior', 'ContainableBehavior');
  365. $this->assertEquals('ContainableBehavior', $result);
  366. $result = $this->Task->getRealClassname('Component', 'Auth');
  367. $this->assertEquals('AuthComponent', $result);
  368. }
  369. /**
  370. * test baking files. The conditionally run tests are known to fail in PHP4
  371. * as PHP4 classnames are all lower case, breaking the plugin path inflection.
  372. *
  373. * @return void
  374. */
  375. public function testBakeModelTest() {
  376. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  377. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  378. $result = $this->Task->bake('Model', 'TestTaskArticle');
  379. $this->assertContains("App::uses('TestTaskArticle', 'Model')", $result);
  380. $this->assertContains('class TestTaskArticleTest extends CakeTestCase', $result);
  381. $this->assertContains('function setUp()', $result);
  382. $this->assertContains("\$this->TestTaskArticle = ClassRegistry::init('TestTaskArticle')", $result);
  383. $this->assertContains('function tearDown()', $result);
  384. $this->assertContains('unset($this->TestTaskArticle)', $result);
  385. $this->assertContains('function testDoSomething()', $result);
  386. $this->assertContains('function testDoSomethingElse()', $result);
  387. $this->assertContains("'app.test_task_article'", $result);
  388. $this->assertContains("'app.test_task_comment'", $result);
  389. $this->assertContains("'app.test_task_tag'", $result);
  390. $this->assertContains("'app.articles_tag'", $result);
  391. }
  392. /**
  393. * test baking controller test files
  394. *
  395. * @return void
  396. */
  397. public function testBakeControllerTest() {
  398. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  399. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  400. $result = $this->Task->bake('Controller', 'TestTaskComments');
  401. $this->assertContains("App::uses('TestTaskCommentsController', 'Controller')", $result);
  402. $this->assertContains('class TestTaskCommentsControllerTest extends ControllerTestCase', $result);
  403. $this->assertNotContains('function setUp()', $result);
  404. $this->assertNotContains("\$this->TestTaskComments = new TestTaskCommentsController()", $result);
  405. $this->assertNotContains("\$this->TestTaskComments->constructClasses()", $result);
  406. $this->assertNotContains('function tearDown()', $result);
  407. $this->assertNotContains('unset($this->TestTaskComments)', $result);
  408. $this->assertContains("'app.test_task_article'", $result);
  409. $this->assertContains("'app.test_task_comment'", $result);
  410. $this->assertContains("'app.test_task_tag'", $result);
  411. $this->assertContains("'app.articles_tag'", $result);
  412. }
  413. /**
  414. * test baking component test files,
  415. *
  416. * @return void
  417. */
  418. public function testBakeComponentTest() {
  419. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  420. $result = $this->Task->bake('Component', 'Example');
  421. $this->assertContains("App::uses('Component', 'Controller')", $result);
  422. $this->assertContains("App::uses('ComponentCollection', 'Controller')", $result);
  423. $this->assertContains("App::uses('ExampleComponent', 'Controller/Component')", $result);
  424. $this->assertContains('class ExampleComponentTest extends CakeTestCase', $result);
  425. $this->assertContains('function setUp()', $result);
  426. $this->assertContains("\$Collection = new ComponentCollection()", $result);
  427. $this->assertContains("\$this->Example = new ExampleComponent(\$Collection)", $result);
  428. $this->assertContains('function tearDown()', $result);
  429. $this->assertContains('unset($this->Example)', $result);
  430. }
  431. /**
  432. * test baking behavior test files,
  433. *
  434. * @return void
  435. */
  436. public function testBakeBehaviorTest() {
  437. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  438. $result = $this->Task->bake('Behavior', 'Example');
  439. $this->assertContains("App::uses('ExampleBehavior', 'Model/Behavior')", $result);
  440. $this->assertContains('class ExampleBehaviorTest extends CakeTestCase', $result);
  441. $this->assertContains('function setUp()', $result);
  442. $this->assertContains("\$this->Example = new ExampleBehavior()", $result);
  443. $this->assertContains('function tearDown()', $result);
  444. $this->assertContains('unset($this->Example)', $result);
  445. }
  446. /**
  447. * test baking helper test files,
  448. *
  449. * @return void
  450. */
  451. public function testBakeHelperTest() {
  452. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  453. $result = $this->Task->bake('Helper', 'Example');
  454. $this->assertContains("App::uses('ExampleHelper', 'View/Helper')", $result);
  455. $this->assertContains('class ExampleHelperTest extends CakeTestCase', $result);
  456. $this->assertContains('function setUp()', $result);
  457. $this->assertContains("\$View = new View()", $result);
  458. $this->assertContains("\$this->Example = new ExampleHelper(\$View)", $result);
  459. $this->assertContains('function tearDown()', $result);
  460. $this->assertContains('unset($this->Example)', $result);
  461. }
  462. /**
  463. * test Constructor generation ensure that constructClasses is called for controllers
  464. *
  465. * @return void
  466. */
  467. public function testGenerateConstructor() {
  468. $result = $this->Task->generateConstructor('controller', 'PostsController', null);
  469. $expected = array('', '', '');
  470. $this->assertEquals($expected, $result);
  471. $result = $this->Task->generateConstructor('model', 'Post', null);
  472. $expected = array('', "ClassRegistry::init('Post');\n", '');
  473. $this->assertEquals($expected, $result);
  474. $result = $this->Task->generateConstructor('helper', 'FormHelper', null);
  475. $expected = array("\$View = new View();\n", "new FormHelper(\$View);\n", '');
  476. $this->assertEquals($expected, $result);
  477. }
  478. /**
  479. * Test generateUses()
  480. */
  481. public function testGenerateUses() {
  482. $result = $this->Task->generateUses('model', 'Model', 'Post');
  483. $expected = array(
  484. array('Post', 'Model')
  485. );
  486. $this->assertEquals($expected, $result);
  487. $result = $this->Task->generateUses('controller', 'Controller', 'PostsController');
  488. $expected = array(
  489. array('PostsController', 'Controller')
  490. );
  491. $this->assertEquals($expected, $result);
  492. $result = $this->Task->generateUses('helper', 'View/Helper', 'FormHelper');
  493. $expected = array(
  494. array('View', 'View'),
  495. array('Helper', 'View'),
  496. array('FormHelper', 'View/Helper'),
  497. );
  498. $this->assertEquals($expected, $result);
  499. $result = $this->Task->generateUses('component', 'Controller/Component', 'AuthComponent');
  500. $expected = array(
  501. array('ComponentCollection', 'Controller'),
  502. array('Component', 'Controller'),
  503. array('AuthComponent', 'Controller/Component')
  504. );
  505. $this->assertEquals($expected, $result);
  506. }
  507. /**
  508. * Test that mock class generation works for the appropriate classes
  509. *
  510. * @return void
  511. */
  512. public function testMockClassGeneration() {
  513. $result = $this->Task->hasMockClass('controller');
  514. $this->assertTrue($result);
  515. }
  516. /**
  517. * test bake() with a -plugin param
  518. *
  519. * @return void
  520. */
  521. public function testBakeWithPlugin() {
  522. $this->Task->plugin = 'TestTest';
  523. //fake plugin path
  524. CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
  525. $path = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php';
  526. $this->Task->expects($this->once())->method('createFile')
  527. ->with($path, $this->anything());
  528. $this->Task->bake('Helper', 'Form');
  529. CakePlugin::unload();
  530. }
  531. /**
  532. * test interactive with plugins lists from the plugin
  533. *
  534. * @return void
  535. */
  536. public function testInteractiveWithPlugin() {
  537. $testApp = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
  538. App::build(array(
  539. 'Plugin' => array($testApp)
  540. ), App::RESET);
  541. CakePlugin::load('TestPlugin');
  542. $this->Task->plugin = 'TestPlugin';
  543. $path = $testApp . 'TestPlugin' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'OtherHelperTest.php';
  544. $this->Task->expects($this->any())
  545. ->method('in')
  546. ->will($this->onConsecutiveCalls(
  547. 5, //helper
  548. 1 //OtherHelper
  549. ));
  550. $this->Task->expects($this->once())
  551. ->method('createFile')
  552. ->with($path, $this->anything());
  553. $this->Task->stdout->expects($this->at(21))
  554. ->method('write')
  555. ->with('1. OtherHelperHelper');
  556. $this->Task->execute();
  557. }
  558. public static function caseFileNameProvider() {
  559. return array(
  560. array('Model', 'Post', 'Case' . DS . 'Model' . DS . 'PostTest.php'),
  561. array('Helper', 'Form', 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php'),
  562. array('Controller', 'Posts', 'Case' . DS . 'Controller' . DS . 'PostsControllerTest.php'),
  563. array('Behavior', 'Containable', 'Case' . DS . 'Model' . DS . 'Behavior' . DS . 'ContainableBehaviorTest.php'),
  564. array('Component', 'Auth', 'Case' . DS . 'Controller' . DS . 'Component' . DS . 'AuthComponentTest.php'),
  565. array('model', 'Post', 'Case' . DS . 'Model' . DS . 'PostTest.php'),
  566. array('helper', 'Form', 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php'),
  567. array('controller', 'Posts', 'Case' . DS . 'Controller' . DS . 'PostsControllerTest.php'),
  568. array('behavior', 'Containable', 'Case' . DS . 'Model' . DS . 'Behavior' . DS . 'ContainableBehaviorTest.php'),
  569. array('component', 'Auth', 'Case' . DS . 'Controller' . DS . 'Component' . DS . 'AuthComponentTest.php'),
  570. );
  571. }
  572. /**
  573. * Test filename generation for each type + plugins
  574. *
  575. * @dataProvider caseFileNameProvider
  576. * @return void
  577. */
  578. public function testTestCaseFileName($type, $class, $expected) {
  579. $this->Task->path = DS . 'my' . DS . 'path' . DS . 'tests' . DS;
  580. $result = $this->Task->testCaseFileName($type, $class);
  581. $expected = $this->Task->path . $expected;
  582. $this->assertEquals($expected, $result);
  583. }
  584. /**
  585. * Test filename generation for plugins.
  586. *
  587. * @return void
  588. */
  589. public function testTestCaseFileNamePlugin() {
  590. $this->Task->path = DS . 'my' . DS . 'path' . DS . 'tests' . DS;
  591. CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
  592. $this->Task->plugin = 'TestTest';
  593. $result = $this->Task->testCaseFileName('Model', 'Post');
  594. $expected = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'PostTest.php';
  595. $this->assertEquals($expected, $result);
  596. }
  597. /**
  598. * test execute with a type defined
  599. *
  600. * @return void
  601. */
  602. public function testExecuteWithOneArg() {
  603. $this->Task->args[0] = 'Model';
  604. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  605. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  606. $this->Task->expects($this->once())->method('createFile')
  607. ->with(
  608. $this->anything(),
  609. $this->stringContains('class TestTaskTagTest extends CakeTestCase')
  610. );
  611. $this->Task->execute();
  612. }
  613. /**
  614. * test execute with type and class name defined
  615. *
  616. * @return void
  617. */
  618. public function testExecuteWithTwoArgs() {
  619. $this->Task->args = array('Model', 'TestTaskTag');
  620. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  621. $this->Task->expects($this->once())->method('createFile')
  622. ->with(
  623. $this->anything(),
  624. $this->stringContains('class TestTaskTagTest extends CakeTestCase')
  625. );
  626. $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
  627. $this->Task->execute();
  628. }
  629. /**
  630. * test execute with type and class name defined and lower case.
  631. *
  632. * @return void
  633. */
  634. public function testExecuteWithTwoArgsLowerCase() {
  635. $this->Task->args = array('model', 'TestTaskTag');
  636. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  637. $this->Task->expects($this->once())->method('createFile')
  638. ->with(
  639. $this->anything(),
  640. $this->stringContains('class TestTaskTagTest extends CakeTestCase')
  641. );
  642. $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
  643. $this->Task->execute();
  644. }
  645. /**
  646. * Data provider for mapType() tests.
  647. *
  648. * @return array
  649. */
  650. public static function mapTypeProvider() {
  651. return array(
  652. array('controller', null, 'Controller'),
  653. array('Controller', null, 'Controller'),
  654. array('component', null, 'Controller/Component'),
  655. array('Component', null, 'Controller/Component'),
  656. array('model', null, 'Model'),
  657. array('Model', null, 'Model'),
  658. array('behavior', null, 'Model/Behavior'),
  659. array('Behavior', null, 'Model/Behavior'),
  660. array('helper', null, 'View/Helper'),
  661. array('Helper', null, 'View/Helper'),
  662. array('Helper', 'DebugKit', 'DebugKit.View/Helper'),
  663. );
  664. }
  665. /**
  666. * Test that mapType returns the correct package names.
  667. *
  668. * @dataProvider mapTypeProvider
  669. * @return void
  670. */
  671. public function testMapType($original, $plugin, $expected) {
  672. $this->assertEquals($expected, $this->Task->mapType($original, $plugin));
  673. }
  674. }