PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Console/Command/Task/TestTaskTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 690 lines | 337 code | 92 blank | 261 comment | 0 complexity | 3918066b7ca2ea6d3e12bca2337acab9 MD5 | raw file
  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-2011, 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-2011, 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. * endTest 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($result), sort($expected));
  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. $object = $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($result, 'MyCustomClass');
  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('Helper', 'Form');
  356. $this->assertEquals('FormHelper', $result);
  357. $result = $this->Task->getRealClassname('Helper', 'FormHelper');
  358. $this->assertEquals('FormHelper', $result);
  359. $result = $this->Task->getRealClassname('Behavior', 'Containable');
  360. $this->assertEquals('ContainableBehavior', $result);
  361. $result = $this->Task->getRealClassname('Behavior', 'ContainableBehavior');
  362. $this->assertEquals('ContainableBehavior', $result);
  363. $result = $this->Task->getRealClassname('Component', 'Auth');
  364. $this->assertEquals('AuthComponent', $result);
  365. }
  366. /**
  367. * test baking files. The conditionally run tests are known to fail in PHP4
  368. * as PHP4 classnames are all lower case, breaking the plugin path inflection.
  369. *
  370. * @return void
  371. */
  372. public function testBakeModelTest() {
  373. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  374. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  375. $result = $this->Task->bake('Model', 'TestTaskArticle');
  376. $this->assertContains("App::uses('TestTaskArticle', 'Model')", $result);
  377. $this->assertContains('class TestTaskArticleTestCase extends CakeTestCase', $result);
  378. $this->assertContains('function setUp()', $result);
  379. $this->assertContains("\$this->TestTaskArticle = ClassRegistry::init('TestTaskArticle')", $result);
  380. $this->assertContains('function tearDown()', $result);
  381. $this->assertContains('unset($this->TestTaskArticle)', $result);
  382. $this->assertContains('function testDoSomething()', $result);
  383. $this->assertContains('function testDoSomethingElse()', $result);
  384. $this->assertContains("'app.test_task_article'", $result);
  385. $this->assertContains("'plugin.test_task.test_task_comment'", $result);
  386. $this->assertContains("'app.test_task_tag'", $result);
  387. $this->assertContains("'app.articles_tag'", $result);
  388. }
  389. /**
  390. * test baking controller test files, ensure that the stub class is generated.
  391. * Conditional assertion is known to fail on PHP4 as classnames are all lower case
  392. * causing issues with inflection of path name from classname.
  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 TestTaskCommentsControllerTestCase extends CakeTestCase', $result);
  402. $this->assertContains('class TestTestTaskCommentsController extends TestTaskCommentsController', $result);
  403. $this->assertContains('public $autoRender = false', $result);
  404. $this->assertContains('function redirect($url, $status = null, $exit = true)', $result);
  405. $this->assertContains('function setUp()', $result);
  406. $this->assertContains("\$this->TestTaskComments = new TestTestTaskCommentsController()", $result);
  407. $this->assertContains("\$this->TestTaskComments->constructClasses()", $result);
  408. $this->assertContains('function tearDown()', $result);
  409. $this->assertContains('unset($this->TestTaskComments)', $result);
  410. $this->assertContains("'app.test_task_article'", $result);
  411. $this->assertContains("'plugin.test_task.test_task_comment'", $result);
  412. $this->assertContains("'app.test_task_tag'", $result);
  413. $this->assertContains("'app.articles_tag'", $result);
  414. }
  415. /**
  416. * test Constructor generation ensure that constructClasses is called for controllers
  417. *
  418. * @return void
  419. */
  420. public function testGenerateConstructor() {
  421. $result = $this->Task->generateConstructor('controller', 'PostsController');
  422. $expected = "new TestPostsController();\n\t\t\$this->Posts->constructClasses();\n";
  423. $this->assertEquals($expected, $result);
  424. $result = $this->Task->generateConstructor('model', 'Post');
  425. $expected = "ClassRegistry::init('Post');\n";
  426. $this->assertEquals($expected, $result);
  427. $result = $this->Task->generateConstructor('helper', 'FormHelper');
  428. $expected = "new FormHelper();\n";
  429. $this->assertEquals($expected, $result);
  430. }
  431. /**
  432. * Test that mock class generation works for the appropriate classes
  433. *
  434. * @return void
  435. */
  436. public function testMockClassGeneration() {
  437. $result = $this->Task->hasMockClass('controller');
  438. $this->assertTrue($result);
  439. }
  440. /**
  441. * test bake() with a -plugin param
  442. *
  443. * @return void
  444. */
  445. public function testBakeWithPlugin() {
  446. $this->Task->plugin = 'TestTest';
  447. //fake plugin path
  448. CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
  449. $path = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS .'FormHelperTest.php';
  450. $this->Task->expects($this->once())->method('createFile')
  451. ->with($path, $this->anything());
  452. $this->Task->bake('Helper', 'Form');
  453. CakePlugin::unload();
  454. }
  455. /**
  456. * test interactive with plugins lists from the plugin
  457. *
  458. * @return void
  459. */
  460. public function testInteractiveWithPlugin() {
  461. $testApp = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
  462. App::build(array(
  463. 'plugins' => array($testApp)
  464. ), true);
  465. CakePlugin::load('TestPlugin');
  466. $this->Task->plugin = 'TestPlugin';
  467. $path = $testApp . 'TestPlugin' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'OtherHelperTest.php';
  468. $this->Task->expects($this->any())
  469. ->method('in')
  470. ->will($this->onConsecutiveCalls(
  471. 5, //helper
  472. 1 //OtherHelper
  473. ));
  474. $this->Task->expects($this->once())
  475. ->method('createFile')
  476. ->with($path, $this->anything());
  477. $this->Task->stdout->expects($this->at(21))
  478. ->method('write')
  479. ->with('1. OtherHelperHelper');
  480. $this->Task->execute();
  481. }
  482. public static function caseFileNameProvider() {
  483. return array(
  484. array('Model', 'Post', 'Case' . DS . 'Model' . DS . 'PostTest.php'),
  485. array('Helper', 'Form', 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php'),
  486. array('Controller', 'Posts', 'Case' . DS . 'Controller' . DS . 'PostsControllerTest.php'),
  487. array('Behavior', 'Containable', 'Case' . DS . 'Model' . DS . 'Behavior' . DS . 'ContainableBehaviorTest.php'),
  488. array('Component', 'Auth', 'Case' . DS . 'Controller' . DS . 'Component' . DS . 'AuthComponentTest.php'),
  489. array('model', 'Post', 'Case' . DS . 'Model' . DS . 'PostTest.php'),
  490. array('helper', 'Form', 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php'),
  491. array('controller', 'Posts', 'Case' . DS . 'Controller' . DS . 'PostsControllerTest.php'),
  492. array('behavior', 'Containable', 'Case' . DS . 'Model' . DS . 'Behavior' . DS . 'ContainableBehaviorTest.php'),
  493. array('component', 'Auth', 'Case' . DS . 'Controller' . DS . 'Component' . DS . 'AuthComponentTest.php'),
  494. );
  495. }
  496. /**
  497. * Test filename generation for each type + plugins
  498. *
  499. * @dataProvider caseFileNameProvider
  500. * @return void
  501. */
  502. public function testTestCaseFileName($type, $class, $expected) {
  503. $this->Task->path = DS . 'my' . DS . 'path' . DS . 'tests' . DS;
  504. $result = $this->Task->testCaseFileName($type, $class);
  505. $expected = $this->Task->path . $expected;
  506. $this->assertEquals($expected, $result);
  507. }
  508. /**
  509. * Test filename generation for plugins.
  510. *
  511. * @return void
  512. */
  513. public function testTestCaseFileNamePlugin() {
  514. $this->Task->path = DS . 'my' . DS . 'path' . DS . 'tests' . DS;
  515. CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS ));
  516. $this->Task->plugin = 'TestTest';
  517. $result = $this->Task->testCaseFileName('Model', 'Post');
  518. $expected = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'PostTest.php';
  519. $this->assertEquals($expected, $result);
  520. }
  521. /**
  522. * test execute with a type defined
  523. *
  524. * @return void
  525. */
  526. public function testExecuteWithOneArg() {
  527. $this->Task->args[0] = 'Model';
  528. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  529. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  530. $this->Task->expects($this->once())->method('createFile')
  531. ->with(
  532. $this->anything(),
  533. $this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
  534. );
  535. $this->Task->execute();
  536. }
  537. /**
  538. * test execute with type and class name defined
  539. *
  540. * @return void
  541. */
  542. public function testExecuteWithTwoArgs() {
  543. $this->Task->args = array('Model', 'TestTaskTag');
  544. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  545. $this->Task->expects($this->once())->method('createFile')
  546. ->with(
  547. $this->anything(),
  548. $this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
  549. );
  550. $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
  551. $this->Task->execute();
  552. }
  553. /**
  554. * test execute with type and class name defined and lower case.
  555. *
  556. * @return void
  557. */
  558. public function testExecuteWithTwoArgsLowerCase() {
  559. $this->Task->args = array('model', 'TestTaskTag');
  560. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  561. $this->Task->expects($this->once())->method('createFile')
  562. ->with(
  563. $this->anything(),
  564. $this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
  565. );
  566. $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
  567. $this->Task->execute();
  568. }
  569. /**
  570. * Data provider for mapType() tests.
  571. *
  572. * @return array
  573. */
  574. public static function mapTypeProvider() {
  575. return array(
  576. array('controller', null, 'Controller'),
  577. array('Controller', null, 'Controller'),
  578. array('component', null, 'Controller/Component'),
  579. array('Component', null, 'Controller/Component'),
  580. array('model', null, 'Model'),
  581. array('Model', null, 'Model'),
  582. array('behavior', null, 'Model/Behavior'),
  583. array('Behavior', null, 'Model/Behavior'),
  584. array('helper', null, 'View/Helper'),
  585. array('Helper', null, 'View/Helper'),
  586. array('Helper', 'DebugKit', 'DebugKit.View/Helper'),
  587. );
  588. }
  589. /**
  590. * Test that mapType returns the correct package names.
  591. *
  592. * @dataProvider mapTypeProvider
  593. * @return void
  594. */
  595. public function testMapType($original, $plugin, $expected) {
  596. $this->assertEquals($expected, $this->Task->mapType($original, $plugin));
  597. }
  598. }