PageRenderTime 107ms CodeModel.GetById 41ms RepoModel.GetById 2ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/tests/cases/console/libs/tasks/model.test.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 919 lines | 603 code | 94 blank | 222 comment | 2 complexity | d32f7377fa1966eb4a8d600007c368f0 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ModelTaskTest file
  4. *
  5. * Test Case for test generation shell task
  6. *
  7. * PHP versions 4 and 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
  18. * @subpackage cake.tests.cases.console.libs.tasks
  19. * @since CakePHP v 1.2.6
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::import('Shell', 'Shell', false);
  23. if (!defined('DISABLE_AUTO_DISPATCH')) {
  24. define('DISABLE_AUTO_DISPATCH', true);
  25. }
  26. if (!class_exists('ShellDispatcher')) {
  27. ob_start();
  28. $argv = false;
  29. require CAKE . 'console' . DS . 'cake.php';
  30. ob_end_clean();
  31. }
  32. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php';
  33. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
  34. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
  35. Mock::generatePartial(
  36. 'ShellDispatcher', 'TestModelTaskMockShellDispatcher',
  37. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  38. );
  39. Mock::generatePartial(
  40. 'ModelTask', 'MockModelTask',
  41. array('in', 'out', 'hr', 'err', 'createFile', '_stop', '_checkUnitTest')
  42. );
  43. Mock::generate(
  44. 'Model', 'MockModelTaskModel'
  45. );
  46. Mock::generate(
  47. 'FixtureTask', 'MockModelTaskFixtureTask'
  48. );
  49. /**
  50. * ModelTaskTest class
  51. *
  52. * @package cake
  53. * @subpackage cake.tests.cases.console.libs.tasks
  54. */
  55. class ModelTaskTest extends CakeTestCase {
  56. /**
  57. * fixtures
  58. *
  59. * @var array
  60. * @access public
  61. */
  62. var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag', 'core.category_thread');
  63. /**
  64. * starTest method
  65. *
  66. * @return void
  67. * @access public
  68. */
  69. function startTest() {
  70. $this->Dispatcher =& new TestModelTaskMockShellDispatcher();
  71. $this->Task =& new MockModelTask($this->Dispatcher);
  72. $this->Task->name = 'ModelTask';
  73. $this->Task->interactive = true;
  74. $this->Task->Dispatch =& $this->Dispatcher;
  75. $this->Task->Dispatch->shellPaths = App::path('shells');
  76. $this->Task->Template =& new TemplateTask($this->Task->Dispatch);
  77. $this->Task->Fixture =& new MockModelTaskFixtureTask();
  78. $this->Task->Test =& new MockModelTaskFixtureTask();
  79. }
  80. /**
  81. * endTest method
  82. *
  83. * @return void
  84. * @access public
  85. */
  86. function endTest() {
  87. unset($this->Task, $this->Dispatcher);
  88. ClassRegistry::flush();
  89. }
  90. /**
  91. * Test that listAll scans the database connection and lists all the tables in it.s
  92. *
  93. * @return void
  94. * @access public
  95. */
  96. function testListAll() {
  97. $this->Task->expectAt(1, 'out', array('1. Article'));
  98. $this->Task->expectAt(2, 'out', array('2. ArticlesTag'));
  99. $this->Task->expectAt(3, 'out', array('3. CategoryThread'));
  100. $this->Task->expectAt(4, 'out', array('4. Comment'));
  101. $this->Task->expectAt(5, 'out', array('5. Tag'));
  102. $result = $this->Task->listAll('test_suite');
  103. $expected = array('articles', 'articles_tags', 'category_threads', 'comments', 'tags');
  104. $this->assertEqual($result, $expected);
  105. $this->Task->expectAt(7, 'out', array('1. Article'));
  106. $this->Task->expectAt(8, 'out', array('2. ArticlesTag'));
  107. $this->Task->expectAt(9, 'out', array('3. CategoryThread'));
  108. $this->Task->expectAt(10, 'out', array('4. Comment'));
  109. $this->Task->expectAt(11, 'out', array('5. Tag'));
  110. $this->Task->connection = 'test_suite';
  111. $result = $this->Task->listAll();
  112. $expected = array('articles', 'articles_tags', 'category_threads', 'comments', 'tags');
  113. $this->assertEqual($result, $expected);
  114. }
  115. /**
  116. * Test that getName interacts with the user and returns the model name.
  117. *
  118. * @return void
  119. * @access public
  120. */
  121. function testGetName() {
  122. $this->Task->setReturnValue('in', 1);
  123. $this->Task->setReturnValueAt(0, 'in', 'q');
  124. $this->Task->expectOnce('_stop');
  125. $this->Task->getName('test_suite');
  126. $this->Task->setReturnValueAt(1, 'in', 1);
  127. $result = $this->Task->getName('test_suite');
  128. $expected = 'Article';
  129. $this->assertEqual($result, $expected);
  130. $this->Task->setReturnValueAt(2, 'in', 4);
  131. $result = $this->Task->getName('test_suite');
  132. $expected = 'Comment';
  133. $this->assertEqual($result, $expected);
  134. $this->Task->setReturnValueAt(3, 'in', 10);
  135. $result = $this->Task->getName('test_suite');
  136. $this->Task->expectOnce('err');
  137. }
  138. /**
  139. * Test table name interactions
  140. *
  141. * @return void
  142. * @access public
  143. */
  144. function testGetTableName() {
  145. $this->Task->setReturnValueAt(0, 'in', 'y');
  146. $result = $this->Task->getTable('Article', 'test_suite');
  147. $expected = 'articles';
  148. $this->assertEqual($result, $expected);
  149. $this->Task->setReturnValueAt(1, 'in', 'n');
  150. $this->Task->setReturnValueAt(2, 'in', 'my_table');
  151. $result = $this->Task->getTable('Article', 'test_suite');
  152. $expected = 'my_table';
  153. $this->assertEqual($result, $expected);
  154. }
  155. /**
  156. * test that initializing the validations works.
  157. *
  158. * @return void
  159. * @access public
  160. */
  161. function testInitValidations() {
  162. $result = $this->Task->initValidations();
  163. $this->assertTrue(in_array('notempty', $result));
  164. }
  165. /**
  166. * test that individual field validation works, with interactive = false
  167. * tests the guessing features of validation
  168. *
  169. * @return void
  170. * @access public
  171. */
  172. function testFieldValidationGuessing() {
  173. $this->Task->interactive = false;
  174. $this->Task->initValidations();
  175. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  176. $expected = array('notempty' => 'notempty');
  177. $this->assertEqual($expected, $result);
  178. $result = $this->Task->fieldValidation('text', array('type' => 'date', 'length' => 10, 'null' => false));
  179. $expected = array('date' => 'date');
  180. $this->assertEqual($expected, $result);
  181. $result = $this->Task->fieldValidation('text', array('type' => 'time', 'length' => 10, 'null' => false));
  182. $expected = array('time' => 'time');
  183. $this->assertEqual($expected, $result);
  184. $result = $this->Task->fieldValidation('email', array('type' => 'string', 'length' => 10, 'null' => false));
  185. $expected = array('email' => 'email');
  186. $this->assertEqual($expected, $result);
  187. $result = $this->Task->fieldValidation('test', array('type' => 'integer', 'length' => 10, 'null' => false));
  188. $expected = array('numeric' => 'numeric');
  189. $this->assertEqual($expected, $result);
  190. $result = $this->Task->fieldValidation('test', array('type' => 'boolean', 'length' => 10, 'null' => false));
  191. $expected = array('boolean' => 'boolean');
  192. $this->assertEqual($expected, $result);
  193. $result = $this->Task->fieldValidation('test', array('type' => 'string', 'length' => 36, 'null' => false));
  194. $expected = array('uuid' => 'uuid');
  195. $this->assertEqual($expected, $result);
  196. }
  197. /**
  198. * test that interactive field validation works and returns multiple validators.
  199. *
  200. * @return void
  201. * @access public
  202. */
  203. function testInteractiveFieldValidation() {
  204. $this->Task->initValidations();
  205. $this->Task->interactive = true;
  206. $this->Task->setReturnValueAt(0, 'in', '19');
  207. $this->Task->setReturnValueAt(1, 'in', 'y');
  208. $this->Task->setReturnValueAt(2, 'in', '15');
  209. $this->Task->setReturnValueAt(3, 'in', 'n');
  210. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  211. $expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
  212. $this->assertEqual($result, $expected);
  213. }
  214. /**
  215. * test that a bogus response doesn't cause errors to bubble up.
  216. *
  217. * @return void
  218. */
  219. function testInteractiveFieldValidationWithBogusResponse() {
  220. $this->Task->initValidations();
  221. $this->Task->interactive = true;
  222. $this->Task->setReturnValueAt(0, 'in', '999999');
  223. $this->Task->setReturnValueAt(1, 'in', '19');
  224. $this->Task->setReturnValueAt(2, 'in', 'n');
  225. $this->Task->expectAt(4, 'out', array(new PatternExpectation('/make a valid/')));
  226. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  227. $expected = array('notempty' => 'notempty');
  228. $this->assertEqual($result, $expected);
  229. }
  230. /**
  231. * test that a regular expression can be used for validation.
  232. *
  233. * @return void
  234. */
  235. function testInteractiveFieldValidationWithRegexp() {
  236. $this->Task->initValidations();
  237. $this->Task->interactive = true;
  238. $this->Task->setReturnValueAt(0, 'in', '/^[a-z]{0,9}$/');
  239. $this->Task->setReturnValueAt(1, 'in', 'n');
  240. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  241. $expected = array('a_z_0_9' => '/^[a-z]{0,9}$/');
  242. $this->assertEqual($result, $expected);
  243. }
  244. /**
  245. * test the validation Generation routine
  246. *
  247. * @return void
  248. * @access public
  249. */
  250. function testNonInteractiveDoValidation() {
  251. $Model =& new MockModelTaskModel();
  252. $Model->primaryKey = 'id';
  253. $Model->setReturnValue('schema', array(
  254. 'id' => array(
  255. 'type' => 'integer',
  256. 'length' => 11,
  257. 'null' => false,
  258. 'key' => 'primary',
  259. ),
  260. 'name' => array(
  261. 'type' => 'string',
  262. 'length' => 20,
  263. 'null' => false,
  264. ),
  265. 'email' => array(
  266. 'type' => 'string',
  267. 'length' => 255,
  268. 'null' => false,
  269. ),
  270. 'some_date' => array(
  271. 'type' => 'date',
  272. 'length' => '',
  273. 'null' => false,
  274. ),
  275. 'some_time' => array(
  276. 'type' => 'time',
  277. 'length' => '',
  278. 'null' => false,
  279. ),
  280. 'created' => array(
  281. 'type' => 'datetime',
  282. 'length' => '',
  283. 'null' => false,
  284. )
  285. ));
  286. $this->Task->interactive = false;
  287. $result = $this->Task->doValidation($Model);
  288. $expected = array(
  289. 'name' => array(
  290. 'notempty' => 'notempty'
  291. ),
  292. 'email' => array(
  293. 'email' => 'email',
  294. ),
  295. 'some_date' => array(
  296. 'date' => 'date'
  297. ),
  298. 'some_time' => array(
  299. 'time' => 'time'
  300. ),
  301. );
  302. $this->assertEqual($result, $expected);
  303. }
  304. /**
  305. * test that finding primary key works
  306. *
  307. * @return void
  308. * @access public
  309. */
  310. function testFindPrimaryKey() {
  311. $fields = array(
  312. 'one' => array(),
  313. 'two' => array(),
  314. 'key' => array('key' => 'primary')
  315. );
  316. $this->Task->expectAt(0, 'in', array('*', null, 'key'));
  317. $this->Task->setReturnValue('in', 'my_field');
  318. $result = $this->Task->findPrimaryKey($fields);
  319. $expected = 'my_field';
  320. $this->assertEqual($result, $expected);
  321. }
  322. /**
  323. * test finding Display field
  324. *
  325. * @return void
  326. * @access public
  327. */
  328. function testFindDisplayField() {
  329. $fields = array('id' => array(), 'tagname' => array(), 'body' => array(),
  330. 'created' => array(), 'modified' => array());
  331. $this->Task->setReturnValue('in', 'n');
  332. $this->Task->setReturnValueAt(0, 'in', 'n');
  333. $result = $this->Task->findDisplayField($fields);
  334. $this->assertFalse($result);
  335. $this->Task->setReturnValueAt(1, 'in', 'y');
  336. $this->Task->setReturnValueAt(2, 'in', 2);
  337. $result = $this->Task->findDisplayField($fields);
  338. $this->assertEqual($result, 'tagname');
  339. }
  340. /**
  341. * test that belongsTo generation works.
  342. *
  343. * @return void
  344. * @access public
  345. */
  346. function testBelongsToGeneration() {
  347. $model = new Model(array('ds' => 'test_suite', 'name' => 'Comment'));
  348. $result = $this->Task->findBelongsTo($model, array());
  349. $expected = array(
  350. 'belongsTo' => array(
  351. array(
  352. 'alias' => 'Article',
  353. 'className' => 'Article',
  354. 'foreignKey' => 'article_id',
  355. ),
  356. array(
  357. 'alias' => 'User',
  358. 'className' => 'User',
  359. 'foreignKey' => 'user_id',
  360. ),
  361. )
  362. );
  363. $this->assertEqual($result, $expected);
  364. $model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread'));
  365. $result = $this->Task->findBelongsTo($model, array());
  366. $expected = array(
  367. 'belongsTo' => array(
  368. array(
  369. 'alias' => 'ParentCategoryThread',
  370. 'className' => 'CategoryThread',
  371. 'foreignKey' => 'parent_id',
  372. ),
  373. )
  374. );
  375. $this->assertEqual($result, $expected);
  376. }
  377. /**
  378. * test that hasOne and/or hasMany relations are generated properly.
  379. *
  380. * @return void
  381. * @access public
  382. */
  383. function testHasManyHasOneGeneration() {
  384. $model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
  385. $this->Task->connection = 'test_suite';
  386. $this->Task->listAll();
  387. $result = $this->Task->findHasOneAndMany($model, array());
  388. $expected = array(
  389. 'hasMany' => array(
  390. array(
  391. 'alias' => 'Comment',
  392. 'className' => 'Comment',
  393. 'foreignKey' => 'article_id',
  394. ),
  395. ),
  396. 'hasOne' => array(
  397. array(
  398. 'alias' => 'Comment',
  399. 'className' => 'Comment',
  400. 'foreignKey' => 'article_id',
  401. ),
  402. ),
  403. );
  404. $this->assertEqual($result, $expected);
  405. $model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread'));
  406. $result = $this->Task->findHasOneAndMany($model, array());
  407. $expected = array(
  408. 'hasOne' => array(
  409. array(
  410. 'alias' => 'ChildCategoryThread',
  411. 'className' => 'CategoryThread',
  412. 'foreignKey' => 'parent_id',
  413. ),
  414. ),
  415. 'hasMany' => array(
  416. array(
  417. 'alias' => 'ChildCategoryThread',
  418. 'className' => 'CategoryThread',
  419. 'foreignKey' => 'parent_id',
  420. ),
  421. )
  422. );
  423. $this->assertEqual($result, $expected);
  424. }
  425. /**
  426. * Test that HABTM generation works
  427. *
  428. * @return void
  429. * @access public
  430. */
  431. function testHasAndBelongsToManyGeneration() {
  432. $model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
  433. $this->Task->connection = 'test_suite';
  434. $this->Task->listAll();
  435. $result = $this->Task->findHasAndBelongsToMany($model, array());
  436. $expected = array(
  437. 'hasAndBelongsToMany' => array(
  438. array(
  439. 'alias' => 'Tag',
  440. 'className' => 'Tag',
  441. 'foreignKey' => 'article_id',
  442. 'joinTable' => 'articles_tags',
  443. 'associationForeignKey' => 'tag_id',
  444. ),
  445. ),
  446. );
  447. $this->assertEqual($result, $expected);
  448. }
  449. /**
  450. * test non interactive doAssociations
  451. *
  452. * @return void
  453. * @access public
  454. */
  455. function testDoAssociationsNonInteractive() {
  456. $this->Task->connection = 'test_suite';
  457. $this->Task->interactive = false;
  458. $model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
  459. $result = $this->Task->doAssociations($model);
  460. $expected = array(
  461. 'hasMany' => array(
  462. array(
  463. 'alias' => 'Comment',
  464. 'className' => 'Comment',
  465. 'foreignKey' => 'article_id',
  466. ),
  467. ),
  468. 'hasAndBelongsToMany' => array(
  469. array(
  470. 'alias' => 'Tag',
  471. 'className' => 'Tag',
  472. 'foreignKey' => 'article_id',
  473. 'joinTable' => 'articles_tags',
  474. 'associationForeignKey' => 'tag_id',
  475. ),
  476. ),
  477. );
  478. }
  479. /**
  480. * Ensure that the fixutre object is correctly called.
  481. *
  482. * @return void
  483. * @access public
  484. */
  485. function testBakeFixture() {
  486. $this->Task->plugin = 'test_plugin';
  487. $this->Task->interactive = true;
  488. $this->Task->Fixture->expectAt(0, 'bake', array('Article', 'articles'));
  489. $this->Task->bakeFixture('Article', 'articles');
  490. $this->assertEqual($this->Task->plugin, $this->Task->Fixture->plugin);
  491. $this->assertEqual($this->Task->connection, $this->Task->Fixture->connection);
  492. $this->assertEqual($this->Task->interactive, $this->Task->Fixture->interactive);
  493. }
  494. /**
  495. * Ensure that the test object is correctly called.
  496. *
  497. * @return void
  498. * @access public
  499. */
  500. function testBakeTest() {
  501. $this->Task->plugin = 'test_plugin';
  502. $this->Task->interactive = true;
  503. $this->Task->Test->expectAt(0, 'bake', array('Model', 'Article'));
  504. $this->Task->bakeTest('Article');
  505. $this->assertEqual($this->Task->plugin, $this->Task->Test->plugin);
  506. $this->assertEqual($this->Task->connection, $this->Task->Test->connection);
  507. $this->assertEqual($this->Task->interactive, $this->Task->Test->interactive);
  508. }
  509. /**
  510. * test confirming of associations, and that when an association is hasMany
  511. * a question for the hasOne is also not asked.
  512. *
  513. * @return void
  514. * @access public
  515. */
  516. function testConfirmAssociations() {
  517. $associations = array(
  518. 'hasOne' => array(
  519. array(
  520. 'alias' => 'ChildCategoryThread',
  521. 'className' => 'CategoryThread',
  522. 'foreignKey' => 'parent_id',
  523. ),
  524. ),
  525. 'hasMany' => array(
  526. array(
  527. 'alias' => 'ChildCategoryThread',
  528. 'className' => 'CategoryThread',
  529. 'foreignKey' => 'parent_id',
  530. ),
  531. ),
  532. 'belongsTo' => array(
  533. array(
  534. 'alias' => 'User',
  535. 'className' => 'User',
  536. 'foreignKey' => 'user_id',
  537. ),
  538. )
  539. );
  540. $model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread'));
  541. $this->Task->setReturnValueAt(0, 'in', 'y');
  542. $result = $this->Task->confirmAssociations($model, $associations);
  543. $this->assertTrue(empty($result['hasOne']));
  544. $this->Task->setReturnValue('in', 'n');
  545. $result = $this->Task->confirmAssociations($model, $associations);
  546. $this->assertTrue(empty($result['hasMany']));
  547. $this->assertTrue(empty($result['hasOne']));
  548. }
  549. /**
  550. * test that inOptions generates questions and only accepts a valid answer
  551. *
  552. * @return void
  553. * @access public
  554. */
  555. function testInOptions() {
  556. $options = array('one', 'two', 'three');
  557. $this->Task->expectAt(0, 'out', array('1. one'));
  558. $this->Task->expectAt(1, 'out', array('2. two'));
  559. $this->Task->expectAt(2, 'out', array('3. three'));
  560. $this->Task->setReturnValueAt(0, 'in', 10);
  561. $this->Task->expectAt(3, 'out', array('1. one'));
  562. $this->Task->expectAt(4, 'out', array('2. two'));
  563. $this->Task->expectAt(5, 'out', array('3. three'));
  564. $this->Task->setReturnValueAt(1, 'in', 2);
  565. $result = $this->Task->inOptions($options, 'Pick a number');
  566. $this->assertEqual($result, 1);
  567. }
  568. /**
  569. * test baking validation
  570. *
  571. * @return void
  572. * @access public
  573. */
  574. function testBakeValidation() {
  575. $validate = array(
  576. 'name' => array(
  577. 'notempty' => 'notempty'
  578. ),
  579. 'email' => array(
  580. 'email' => 'email',
  581. ),
  582. 'some_date' => array(
  583. 'date' => 'date'
  584. ),
  585. 'some_time' => array(
  586. 'time' => 'time'
  587. )
  588. );
  589. $result = $this->Task->bake('Article', compact('validate'));
  590. $this->assertPattern('/class Article extends AppModel \{/', $result);
  591. $this->assertPattern('/\$name \= \'Article\'/', $result);
  592. $this->assertPattern('/\$validate \= array\(/', $result);
  593. $expected = <<< STRINGEND
  594. array(
  595. 'notempty' => array(
  596. 'rule' => array('notempty'),
  597. //'message' => 'Your custom message here',
  598. //'allowEmpty' => false,
  599. //'required' => false,
  600. //'last' => false, // Stop validation after this rule
  601. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  602. ),
  603. STRINGEND;
  604. $this->assertPattern('/' . preg_quote(str_replace("\r\n", "\n", $expected), '/') . '/', $result);
  605. }
  606. /**
  607. * test baking relations
  608. *
  609. * @return void
  610. * @access public
  611. */
  612. function testBakeRelations() {
  613. $associations = array(
  614. 'belongsTo' => array(
  615. array(
  616. 'alias' => 'SomethingElse',
  617. 'className' => 'SomethingElse',
  618. 'foreignKey' => 'something_else_id',
  619. ),
  620. array(
  621. 'alias' => 'User',
  622. 'className' => 'User',
  623. 'foreignKey' => 'user_id',
  624. ),
  625. ),
  626. 'hasOne' => array(
  627. array(
  628. 'alias' => 'OtherModel',
  629. 'className' => 'OtherModel',
  630. 'foreignKey' => 'other_model_id',
  631. ),
  632. ),
  633. 'hasMany' => array(
  634. array(
  635. 'alias' => 'Comment',
  636. 'className' => 'Comment',
  637. 'foreignKey' => 'parent_id',
  638. ),
  639. ),
  640. 'hasAndBelongsToMany' => array(
  641. array(
  642. 'alias' => 'Tag',
  643. 'className' => 'Tag',
  644. 'foreignKey' => 'article_id',
  645. 'joinTable' => 'articles_tags',
  646. 'associationForeignKey' => 'tag_id',
  647. ),
  648. )
  649. );
  650. $result = $this->Task->bake('Article', compact('associations'));
  651. $this->assertPattern('/\$hasAndBelongsToMany \= array\(/', $result);
  652. $this->assertPattern('/\$hasMany \= array\(/', $result);
  653. $this->assertPattern('/\$belongsTo \= array\(/', $result);
  654. $this->assertPattern('/\$hasOne \= array\(/', $result);
  655. $this->assertPattern('/Tag/', $result);
  656. $this->assertPattern('/OtherModel/', $result);
  657. $this->assertPattern('/SomethingElse/', $result);
  658. $this->assertPattern('/Comment/', $result);
  659. }
  660. /**
  661. * test bake() with a -plugin param
  662. *
  663. * @return void
  664. * @access public
  665. */
  666. function testBakeWithPlugin() {
  667. $this->Task->plugin = 'ControllerTest';
  668. $path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php';
  669. $this->Task->expectAt(0, 'createFile', array($path, '*'));
  670. $this->Task->bake('Article', array(), array());
  671. $this->Task->plugin = 'controllerTest';
  672. $path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php';
  673. $this->Task->expectAt(1, 'createFile', array(
  674. $path, new PatternExpectation('/Article extends ControllerTestAppModel/')));
  675. $this->Task->bake('Article', array(), array());
  676. $this->assertEqual(count(ClassRegistry::keys()), 0);
  677. $this->assertEqual(count(ClassRegistry::mapKeys()), 0);
  678. }
  679. /**
  680. * test that execute passes runs bake depending with named model.
  681. *
  682. * @return void
  683. * @access public
  684. */
  685. function testExecuteWithNamedModel() {
  686. $this->Task->connection = 'test_suite';
  687. $this->Task->path = '/my/path/';
  688. $this->Task->args = array('article');
  689. $filename = '/my/path/article.php';
  690. $this->Task->setReturnValue('_checkUnitTest', 1);
  691. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
  692. $this->Task->execute();
  693. $this->assertEqual(count(ClassRegistry::keys()), 0);
  694. $this->assertEqual(count(ClassRegistry::mapKeys()), 0);
  695. }
  696. /**
  697. * test that execute passes with different inflections of the same name.
  698. *
  699. * @return void
  700. * @access public
  701. */
  702. function testExecuteWithNamedModelVariations() {
  703. $this->Task->connection = 'test_suite';
  704. $this->Task->path = '/my/path/';
  705. $this->Task->setReturnValue('_checkUnitTest', 1);
  706. $this->Task->args = array('article');
  707. $filename = '/my/path/article.php';
  708. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
  709. $this->Task->execute();
  710. $this->Task->args = array('Articles');
  711. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
  712. $this->Task->execute();
  713. $this->Task->args = array('articles');
  714. $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
  715. $this->Task->execute();
  716. }
  717. /**
  718. * test that execute with a model name picks up hasMany associations.
  719. *
  720. * @return void
  721. * @access public
  722. */
  723. function testExecuteWithNamedModelHasManyCreated() {
  724. $this->Task->connection = 'test_suite';
  725. $this->Task->path = '/my/path/';
  726. $this->Task->args = array('article');
  727. $filename = '/my/path/article.php';
  728. $this->Task->setReturnValue('_checkUnitTest', 1);
  729. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation("/'Comment' \=\> array\(/")));
  730. $this->Task->execute();
  731. }
  732. /**
  733. * test that execute runs all() when args[0] = all
  734. *
  735. * @return void
  736. * @access public
  737. */
  738. function testExecuteIntoAll() {
  739. $this->Task->connection = 'test_suite';
  740. $this->Task->path = '/my/path/';
  741. $this->Task->args = array('all');
  742. $this->Task->setReturnValue('_checkUnitTest', true);
  743. $this->Task->Fixture->expectCallCount('bake', 5);
  744. $this->Task->Test->expectCallCount('bake', 5);
  745. $filename = '/my/path/article.php';
  746. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/')));
  747. $filename = '/my/path/articles_tag.php';
  748. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticlesTag/')));
  749. $filename = '/my/path/category_thread.php';
  750. $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class CategoryThread/')));
  751. $filename = '/my/path/comment.php';
  752. $this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class Comment/')));
  753. $filename = '/my/path/tag.php';
  754. $this->Task->expectAt(4, 'createFile', array($filename, new PatternExpectation('/class Tag/')));
  755. $this->Task->execute();
  756. $this->assertEqual(count(ClassRegistry::keys()), 0);
  757. $this->assertEqual(count(ClassRegistry::mapKeys()), 0);
  758. }
  759. /**
  760. * test that skipTables changes how all() works.
  761. *
  762. * @return void
  763. */
  764. function testSkipTablesAndAll() {
  765. $this->Task->connection = 'test_suite';
  766. $this->Task->path = '/my/path/';
  767. $this->Task->args = array('all');
  768. $this->Task->setReturnValue('_checkUnitTest', true);
  769. $this->Task->skipTables = array('tags');
  770. $this->Task->Fixture->expectCallCount('bake', 4);
  771. $this->Task->Test->expectCallCount('bake', 4);
  772. $filename = '/my/path/article.php';
  773. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/')));
  774. $filename = '/my/path/articles_tag.php';
  775. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticlesTag/')));
  776. $filename = '/my/path/category_thread.php';
  777. $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class CategoryThread/')));
  778. $filename = '/my/path/comment.php';
  779. $this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class Comment/')));
  780. $this->Task->execute();
  781. }
  782. /**
  783. * test the interactive side of bake.
  784. *
  785. * @return void
  786. * @access public
  787. */
  788. function testExecuteIntoInteractive() {
  789. $this->Task->connection = 'test_suite';
  790. $this->Task->path = '/my/path/';
  791. $this->Task->interactive = true;
  792. $this->Task->setReturnValueAt(0, 'in', '1'); //choose article
  793. $this->Task->setReturnValueAt(1, 'in', 'n'); //no validation
  794. $this->Task->setReturnValueAt(2, 'in', 'y'); //yes to associations
  795. $this->Task->setReturnValueAt(3, 'in', 'y'); //yes to comment relation
  796. $this->Task->setReturnValueAt(4, 'in', 'y'); //yes to user relation
  797. $this->Task->setReturnValueAt(5, 'in', 'y'); //yes to tag relation
  798. $this->Task->setReturnValueAt(6, 'in', 'n'); //no to additional assocs
  799. $this->Task->setReturnValueAt(7, 'in', 'y'); //yes to looksGood?
  800. $this->Task->setReturnValue('_checkUnitTest', true);
  801. $this->Task->Test->expectOnce('bake');
  802. $this->Task->Fixture->expectOnce('bake');
  803. $filename = '/my/path/article.php';
  804. $this->Task->expectOnce('createFile');
  805. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/')));
  806. $this->Task->execute();
  807. $this->assertEqual(count(ClassRegistry::keys()), 0);
  808. $this->assertEqual(count(ClassRegistry::mapKeys()), 0);
  809. }
  810. /**
  811. * test using bake interactively with a table that does not exist.
  812. *
  813. * @return void
  814. * @access public
  815. */
  816. function testExecuteWithNonExistantTableName() {
  817. $this->Task->connection = 'test_suite';
  818. $this->Task->path = '/my/path/';
  819. $this->Task->expectOnce('_stop');
  820. $this->Task->expectOnce('err');
  821. $this->Task->setReturnValueAt(0, 'in', 'Foobar');
  822. $this->Task->setReturnValueAt(1, 'in', 'y');
  823. $this->Task->execute();
  824. }
  825. }