TestTaskTest.php 22 KB

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