TestTaskTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 2006-2010, 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 2006-2010, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package cake.tests.cases.console.libs.tasks
  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
  33. * @package cake.tests.cases.console.libs.tasks
  34. */
  35. class TestTaskArticle extends Model {
  36. /**
  37. * Model name
  38. *
  39. * @var string
  40. * @access public
  41. */
  42. public $name = 'TestTaskArticle';
  43. /**
  44. * Table name to use
  45. *
  46. * @var string
  47. * @access public
  48. */
  49. public $useTable = 'articles';
  50. /**
  51. * HasMany Associations
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. public $hasMany = array(
  57. 'Comment' => array(
  58. 'className' => 'TestTask.TestTaskComment',
  59. 'foreignKey' => 'article_id',
  60. )
  61. );
  62. /**
  63. * Has and Belongs To Many Associations
  64. *
  65. * @var array
  66. * @access public
  67. */
  68. public $hasAndBelongsToMany = array(
  69. 'Tag' => array(
  70. 'className' => 'TestTaskTag',
  71. 'joinTable' => 'articles_tags',
  72. 'foreignKey' => 'article_id',
  73. 'associationForeignKey' => 'tag_id'
  74. )
  75. );
  76. /**
  77. * Example public method
  78. *
  79. * @return void
  80. */
  81. public function doSomething() {
  82. }
  83. /**
  84. * Example Secondary public method
  85. *
  86. * @return void
  87. */
  88. public function doSomethingElse() {
  89. }
  90. /**
  91. * Example protected method
  92. *
  93. * @return void
  94. */
  95. protected function _innerMethod() {
  96. }
  97. }
  98. /**
  99. * Tag Testing Model
  100. *
  101. * @package cake
  102. * @package cake.tests.cases.console.libs.tasks
  103. */
  104. class TestTaskTag extends Model {
  105. /**
  106. * Model name
  107. *
  108. * @var string
  109. * @access public
  110. */
  111. public $name = 'TestTaskTag';
  112. /**
  113. * Table name
  114. *
  115. * @var string
  116. * @access public
  117. */
  118. public $useTable = 'tags';
  119. /**
  120. * Has and Belongs To Many Associations
  121. *
  122. * @var array
  123. * @access public
  124. */
  125. public $hasAndBelongsToMany = array(
  126. 'Article' => array(
  127. 'className' => 'TestTaskArticle',
  128. 'joinTable' => 'articles_tags',
  129. 'foreignKey' => 'tag_id',
  130. 'associationForeignKey' => 'article_id'
  131. )
  132. );
  133. }
  134. /**
  135. * Simulated plugin
  136. *
  137. * @package cake
  138. * @package cake.tests.cases.console.libs.tasks
  139. */
  140. class TestTaskAppModel extends Model {
  141. }
  142. /**
  143. * Testing AppMode (TaskComment)
  144. *
  145. * @package cake
  146. * @package cake.tests.cases.console.libs.tasks
  147. */
  148. class TestTaskComment extends TestTaskAppModel {
  149. /**
  150. * Model name
  151. *
  152. * @var string
  153. * @access public
  154. */
  155. public $name = 'TestTaskComment';
  156. /**
  157. * Table name
  158. *
  159. * @var string
  160. * @access public
  161. */
  162. public $useTable = 'comments';
  163. /**
  164. * Belongs To Associations
  165. *
  166. * @var array
  167. * @access public
  168. */
  169. public $belongsTo = array(
  170. 'Article' => array(
  171. 'className' => 'TestTaskArticle',
  172. 'foreignKey' => 'article_id',
  173. )
  174. );
  175. }
  176. /**
  177. * Test Task Comments Controller
  178. *
  179. * @package cake
  180. * @package cake.tests.cases.console.libs.tasks
  181. */
  182. class TestTaskCommentsController extends Controller {
  183. /**
  184. * Controller Name
  185. *
  186. * @var string
  187. * @access public
  188. */
  189. public $name = 'TestTaskComments';
  190. /**
  191. * Models to use
  192. *
  193. * @var array
  194. * @access public
  195. */
  196. public $uses = array('TestTaskComment', 'TestTaskTag');
  197. }
  198. /**
  199. * TestTaskTest class
  200. *
  201. * @package cake.tests.cases.console.libs.tasks
  202. */
  203. class TestTaskTest extends CakeTestCase {
  204. /**
  205. * Fixtures
  206. *
  207. * @var string
  208. * @access public
  209. */
  210. public $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
  211. /**
  212. * setup method
  213. *
  214. * @return void
  215. */
  216. public function setup() {
  217. parent::setup();
  218. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  219. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  220. $this->Task = $this->getMock('TestTask',
  221. array('in', 'err', 'createFile', '_stop', 'isLoadableClass'),
  222. array($out, $out, $in)
  223. );
  224. $this->Task->name = 'Test';
  225. $this->Task->Template = new TemplateTask($out, $out, $in);
  226. }
  227. /**
  228. * endTest method
  229. *
  230. * @return void
  231. */
  232. public function tearDown() {
  233. parent::tearDown();
  234. unset($this->Task);
  235. }
  236. /**
  237. * Test that file path generation doesn't continuously append paths.
  238. *
  239. * @return void
  240. */
  241. public function testFilePathGenerationModelRepeated() {
  242. $this->Task->expects($this->never())->method('err');
  243. $this->Task->expects($this->never())->method('_stop');
  244. $file = TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php';
  245. $this->Task->expects($this->at(1))->method('createFile')
  246. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  247. $this->Task->expects($this->at(3))->method('createFile')
  248. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  249. $file = TESTS . 'cases' . DS . 'controllers' . DS . 'comments_controller.test.php';
  250. $this->Task->expects($this->at(5))->method('createFile')
  251. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  252. $this->Task->bake('Model', 'MyClass');
  253. $this->Task->bake('Model', 'MyClass');
  254. $this->Task->bake('Controller', 'Comments');
  255. }
  256. /**
  257. * Test that method introspection pulls all relevant non parent class
  258. * methods into the test case.
  259. *
  260. * @return void
  261. */
  262. function testMethodIntrospection() {
  263. $result = $this->Task->getTestableMethods('TestTaskArticle');
  264. $expected = array('dosomething', 'dosomethingelse');
  265. $this->assertEqual(array_map('strtolower', $result), $expected);
  266. }
  267. /**
  268. * test that the generation of fixtures works correctly.
  269. *
  270. * @return void
  271. */
  272. public function testFixtureArrayGenerationFromModel() {
  273. $subject = ClassRegistry::init('TestTaskArticle');
  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->assertEqual(sort($result), sort($expected));
  278. }
  279. /**
  280. * test that the generation of fixtures works correctly.
  281. *
  282. * @return void
  283. */
  284. public function testFixtureArrayGenerationFromController() {
  285. $subject = new TestTaskCommentsController();
  286. $result = $this->Task->generateFixtureList($subject);
  287. $expected = array('plugin.test_task.test_task_comment', 'app.articles_tags',
  288. 'app.test_task_article', 'app.test_task_tag');
  289. $this->assertEqual(sort($result), sort($expected));
  290. }
  291. /**
  292. * test user interaction to get object type
  293. *
  294. * @return void
  295. */
  296. public function testGetObjectType() {
  297. $this->Task->expects($this->once())->method('_stop');
  298. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('q'));
  299. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue(2));
  300. $this->Task->getObjectType();
  301. $result = $this->Task->getObjectType();
  302. $this->assertEqual($result, $this->Task->classTypes[1]);
  303. }
  304. /**
  305. * creating test subjects should clear the registry so the registry is always fresh
  306. *
  307. * @return void
  308. */
  309. public function testRegistryClearWhenBuildingTestObjects() {
  310. ClassRegistry::flush();
  311. $model = ClassRegistry::init('TestTaskComment');
  312. $model->bindModel(array(
  313. 'belongsTo' => array(
  314. 'Random' => array(
  315. 'className' => 'TestTaskArticle',
  316. 'foreignKey' => 'article_id',
  317. )
  318. )
  319. ));
  320. $keys = ClassRegistry::keys();
  321. $this->assertTrue(in_array('test_task_comment', $keys));
  322. $object = $this->Task->buildTestSubject('Model', 'TestTaskComment');
  323. $keys = ClassRegistry::keys();
  324. $this->assertFalse(in_array('random', $keys));
  325. }
  326. /**
  327. * test that getClassName returns the user choice as a classname.
  328. *
  329. * @return void
  330. */
  331. public function testGetClassName() {
  332. $objects = App::objects('model');
  333. $skip = $this->skipIf(empty($objects), 'No models in app, this test will fail.');
  334. if ($skip) {
  335. return;
  336. }
  337. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('MyCustomClass'));
  338. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(1));
  339. $result = $this->Task->getClassName('Model');
  340. $this->assertEqual($result, 'MyCustomClass');
  341. $result = $this->Task->getClassName('Model');
  342. $options = App::objects('model');
  343. $this->assertEqual($result, $options[0]);
  344. }
  345. /**
  346. * Test the user interaction for defining additional fixtures.
  347. *
  348. * @return void
  349. */
  350. public function testGetUserFixtures() {
  351. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  352. $this->Task->expects($this->at(1))->method('in')
  353. ->will($this->returnValue('app.pizza, app.topping, app.side_dish'));
  354. $result = $this->Task->getUserFixtures();
  355. $expected = array('app.pizza', 'app.topping', 'app.side_dish');
  356. $this->assertEqual($result, $expected);
  357. }
  358. /**
  359. * test that resolving classnames works
  360. *
  361. * @return void
  362. */
  363. public function testGetRealClassname() {
  364. $result = $this->Task->getRealClassname('Model', 'Post');
  365. $this->assertEqual($result, 'Post');
  366. $result = $this->Task->getRealClassname('Controller', 'Posts');
  367. $this->assertEqual($result, 'PostsController');
  368. $result = $this->Task->getRealClassname('Helper', 'Form');
  369. $this->assertEqual($result, 'FormHelper');
  370. $result = $this->Task->getRealClassname('Behavior', 'Containable');
  371. $this->assertEqual($result, 'ContainableBehavior');
  372. $result = $this->Task->getRealClassname('Component', 'Auth');
  373. $this->assertEqual($result, 'AuthComponent');
  374. }
  375. /**
  376. * test baking files. The conditionally run tests are known to fail in PHP4
  377. * as PHP4 classnames are all lower case, breaking the plugin path inflection.
  378. *
  379. * @return void
  380. */
  381. public function testBakeModelTest() {
  382. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  383. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  384. $result = $this->Task->bake('Model', 'TestTaskArticle');
  385. $this->assertContains("App::uses('TestTaskArticle', 'Model')", $result);
  386. $this->assertContains('class TestTaskArticleTestCase extends CakeTestCase', $result);
  387. $this->assertContains('function setUp()', $result);
  388. $this->assertContains("\$this->TestTaskArticle = ClassRegistry::init('TestTaskArticle')", $result);
  389. $this->assertContains('function tearDown()', $result);
  390. $this->assertContains('unset($this->TestTaskArticle)', $result);
  391. $this->assertContains('function testDoSomething()', $result);
  392. $this->assertContains('function testDoSomethingElse()', $result);
  393. $this->assertContains("'app.test_task_article'", $result);
  394. $this->assertContains("'plugin.test_task.test_task_comment'", $result);
  395. $this->assertContains("'app.test_task_tag'", $result);
  396. $this->assertContains("'app.articles_tag'", $result);
  397. }
  398. /**
  399. * test baking controller test files, ensure that the stub class is generated.
  400. * Conditional assertion is known to fail on PHP4 as classnames are all lower case
  401. * causing issues with inflection of path name from classname.
  402. *
  403. * @return void
  404. */
  405. public function testBakeControllerTest() {
  406. $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
  407. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  408. $result = $this->Task->bake('Controller', 'TestTaskComments');
  409. $this->assertContains("App::uses('TestTaskCommentsController', 'Controller')", $result);
  410. $this->assertContains('class TestTaskCommentsControllerTestCase extends CakeTestCase', $result);
  411. $this->assertContains('class TestTestTaskCommentsController extends TestTaskCommentsController', $result);
  412. $this->assertContains('public $autoRender = false', $result);
  413. $this->assertContains('function redirect($url, $status = null, $exit = true)', $result);
  414. $this->assertContains('function setUp()', $result);
  415. $this->assertContains("\$this->TestTaskComments = new TestTestTaskCommentsController()", $result);
  416. $this->assertContains("\$this->TestTaskComments->constructClasses()", $result);
  417. $this->assertContains('function tearDown()', $result);
  418. $this->assertContains('unset($this->TestTaskComments)', $result);
  419. $this->assertContains("'app.test_task_article'", $result);
  420. $this->assertContains("'plugin.test_task.test_task_comment'", $result);
  421. $this->assertContains("'app.test_task_tag'", $result);
  422. $this->assertContains("'app.articles_tag'", $result);
  423. }
  424. /**
  425. * test Constructor generation ensure that constructClasses is called for controllers
  426. *
  427. * @return void
  428. */
  429. public function testGenerateConstructor() {
  430. $result = $this->Task->generateConstructor('controller', 'PostsController');
  431. $expected = "new TestPostsController();\n\t\t\$this->Posts->constructClasses();\n";
  432. $this->assertEqual($result, $expected);
  433. $result = $this->Task->generateConstructor('model', 'Post');
  434. $expected = "ClassRegistry::init('Post');\n";
  435. $this->assertEqual($result, $expected);
  436. $result = $this->Task->generateConstructor('helper', 'FormHelper');
  437. $expected = "new FormHelper();\n";
  438. $this->assertEqual($result, $expected);
  439. }
  440. /**
  441. * Test that mock class generation works for the appropriate classes
  442. *
  443. * @return void
  444. */
  445. public function testMockClassGeneration() {
  446. $result = $this->Task->hasMockClass('controller');
  447. $this->assertTrue($result);
  448. }
  449. /**
  450. * test bake() with a -plugin param
  451. *
  452. * @return void
  453. */
  454. public function testBakeWithPlugin() {
  455. $this->Task->plugin = 'TestTest';
  456. $path = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'form.test.php';
  457. $this->Task->expects($this->once())->method('createFile')
  458. ->with($path, new PHPUnit_Framework_Constraint_IsAnything());
  459. $this->Task->bake('Helper', 'Form');
  460. }
  461. /**
  462. * test interactive with plugins lists from the plugin
  463. *
  464. * @return void
  465. */
  466. function testInteractiveWithPlugin() {
  467. $testApp = LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS;
  468. App::build(array(
  469. 'plugins' => array($testApp)
  470. ), true);
  471. $this->Task->plugin = 'TestPlugin';
  472. $path = $testApp . 'test_plugin' . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'other_helper_helper.test.php';
  473. $this->Task->expects($this->any())
  474. ->method('in')
  475. ->will($this->onConsecutiveCalls(
  476. 5, //helper
  477. 1 //OtherHelper
  478. ));
  479. $this->Task->expects($this->once())
  480. ->method('createFile')
  481. ->with($path, $this->anything());
  482. $this->Task->stdout->expects($this->at(21))
  483. ->method('write')
  484. ->with('1. OtherHelperHelper');
  485. $this->Task->execute();
  486. }
  487. /**
  488. * Test filename generation for each type + plugins
  489. *
  490. * @return void
  491. */
  492. public function testTestCaseFileName() {
  493. $this->Task->path = '/my/path/tests/';
  494. $result = $this->Task->testCaseFileName('Model', 'Post');
  495. $expected = $this->Task->path . 'cases' . DS . 'models' . DS . 'post.test.php';
  496. $this->assertEqual($result, $expected);
  497. $result = $this->Task->testCaseFileName('Helper', 'Form');
  498. $expected = $this->Task->path . 'cases' . DS . 'helpers' . DS . 'form.test.php';
  499. $this->assertEqual($result, $expected);
  500. $result = $this->Task->testCaseFileName('Controller', 'Posts');
  501. $expected = $this->Task->path . 'cases' . DS . 'controllers' . DS . 'posts_controller.test.php';
  502. $this->assertEqual($result, $expected);
  503. $result = $this->Task->testCaseFileName('Behavior', 'Containable');
  504. $expected = $this->Task->path . 'cases' . DS . 'behaviors' . DS . 'containable.test.php';
  505. $this->assertEqual($result, $expected);
  506. $result = $this->Task->testCaseFileName('Component', 'Auth');
  507. $expected = $this->Task->path . 'cases' . DS . 'components' . DS . 'auth.test.php';
  508. $this->assertEqual($result, $expected);
  509. $this->Task->plugin = 'TestTest';
  510. $result = $this->Task->testCaseFileName('Model', 'Post');
  511. $expected = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'cases' . DS . 'models' . DS . 'post.test.php';
  512. $this->assertEqual($result, $expected);
  513. }
  514. /**
  515. * test execute with a type defined
  516. *
  517. * @return void
  518. */
  519. public function testExecuteWithOneArg() {
  520. $this->Task->args[0] = 'Model';
  521. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  522. $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
  523. $this->Task->expects($this->once())->method('createFile')
  524. ->with(
  525. new PHPUnit_Framework_Constraint_IsAnything(),
  526. new PHPUnit_Framework_Constraint_PCREMatch('/class TestTaskTagTestCase extends CakeTestCase/')
  527. );
  528. $this->Task->execute();
  529. }
  530. /**
  531. * test execute with type and class name defined
  532. *
  533. * @return void
  534. */
  535. public function testExecuteWithTwoArgs() {
  536. $this->Task->args = array('Model', 'TestTaskTag');
  537. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
  538. $this->Task->expects($this->once())->method('createFile')
  539. ->with(
  540. new PHPUnit_Framework_Constraint_IsAnything(),
  541. new PHPUnit_Framework_Constraint_PCREMatch('/class TestTaskTagTestCase extends CakeTestCase/')
  542. );
  543. $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
  544. $this->Task->execute();
  545. }
  546. }