ModelTaskTest.php 28 KB

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