PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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