ModelTaskTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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-2012, 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-2012, 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 testListAllArgument() {
  101. $this->_useMockedOut();
  102. $result = $this->Task->listAll('test');
  103. $this->assertContains('bake_articles', $result);
  104. $this->assertContains('bake_articles_bake_tags', $result);
  105. $this->assertContains('bake_tags', $result);
  106. $this->assertContains('bake_comments', $result);
  107. $this->assertContains('category_threads', $result);
  108. }
  109. /**
  110. * Test that listAll uses the connection property
  111. *
  112. * @return void
  113. */
  114. public function testListAllConnection() {
  115. $this->_useMockedOut();
  116. $this->Task->connection = 'test';
  117. $result = $this->Task->listAll();
  118. $this->assertContains('bake_articles', $result);
  119. $this->assertContains('bake_articles_bake_tags', $result);
  120. $this->assertContains('bake_tags', $result);
  121. $this->assertContains('bake_comments', $result);
  122. $this->assertContains('category_threads', $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. $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->assertEquals($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->assertEquals($expected, $result);
  178. }
  179. /**
  180. * test getTable with non-conventional tablenames
  181. *
  182. * @return void
  183. */
  184. public function testGetTableOddTableInteractive() {
  185. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  186. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  187. $this->Task = $this->getMock('ModelTask',
  188. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables'),
  189. array($out, $out, $in)
  190. );
  191. $this->_setupOtherMocks();
  192. $this->Task->connection = 'test';
  193. $this->Task->path = '/my/path/';
  194. $this->Task->interactive = true;
  195. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  196. $this->Task->expects($this->any())->method('in')
  197. ->will($this->onConsecutiveCalls(
  198. 2 // bake_odd
  199. ));
  200. $result = $this->Task->getName();
  201. $expected = 'BakeOdd';
  202. $this->assertEquals($expected, $result);
  203. $result = $this->Task->getTable($result);
  204. $expected = 'bake_odd';
  205. $this->assertEquals($expected, $result);
  206. }
  207. /**
  208. * test getTable with non-conventional tablenames
  209. *
  210. * @return void
  211. */
  212. public function testGetTableOddTable() {
  213. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  214. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  215. $this->Task = $this->getMock('ModelTask',
  216. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables'),
  217. array($out, $out, $in)
  218. );
  219. $this->_setupOtherMocks();
  220. $this->Task->connection = 'test';
  221. $this->Task->path = '/my/path/';
  222. $this->Task->interactive = false;
  223. $this->Task->args = array('BakeOdd');
  224. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  225. $this->Task->listAll();
  226. $result = $this->Task->getTable('BakeOdd');
  227. $expected = 'bake_odd';
  228. $this->assertEquals($expected, $result);
  229. }
  230. /**
  231. * test that initializing the validations works.
  232. *
  233. * @return void
  234. */
  235. public function testInitValidations() {
  236. $result = $this->Task->initValidations();
  237. $this->assertTrue(in_array('notempty', $result));
  238. }
  239. /**
  240. * test that individual field validation works, with interactive = false
  241. * tests the guessing features of validation
  242. *
  243. * @return void
  244. */
  245. public function testFieldValidationGuessing() {
  246. $this->Task->interactive = false;
  247. $this->Task->initValidations();
  248. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  249. $expected = array('notempty' => 'notempty');
  250. $this->assertEquals($expected, $result);
  251. $result = $this->Task->fieldValidation('text', array('type' => 'date', 'length' => 10, 'null' => false));
  252. $expected = array('date' => 'date');
  253. $this->assertEquals($expected, $result);
  254. $result = $this->Task->fieldValidation('text', array('type' => 'time', 'length' => 10, 'null' => false));
  255. $expected = array('time' => 'time');
  256. $this->assertEquals($expected, $result);
  257. $result = $this->Task->fieldValidation('email', array('type' => 'string', 'length' => 10, 'null' => false));
  258. $expected = array('email' => 'email');
  259. $this->assertEquals($expected, $result);
  260. $result = $this->Task->fieldValidation('test', array('type' => 'integer', 'length' => 10, 'null' => false));
  261. $expected = array('numeric' => 'numeric');
  262. $this->assertEquals($expected, $result);
  263. $result = $this->Task->fieldValidation('test', array('type' => 'boolean', 'length' => 10, 'null' => false));
  264. $expected = array('boolean' => 'boolean');
  265. $this->assertEquals($expected, $result);
  266. }
  267. /**
  268. * test that interactive field validation works and returns multiple validators.
  269. *
  270. * @return void
  271. */
  272. public function testInteractiveFieldValidation() {
  273. $this->Task->initValidations();
  274. $this->Task->interactive = true;
  275. $this->Task->expects($this->any())->method('in')
  276. ->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
  277. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  278. $expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
  279. $this->assertEquals($expected, $result);
  280. }
  281. /**
  282. * test that a bogus response doesn't cause errors to bubble up.
  283. *
  284. * @return void
  285. */
  286. public function testInteractiveFieldValidationWithBogusResponse() {
  287. $this->_useMockedOut();
  288. $this->Task->initValidations();
  289. $this->Task->interactive = true;
  290. $this->Task->expects($this->any())->method('in')
  291. ->will($this->onConsecutiveCalls('999999', '24', 'n'));
  292. $this->Task->expects($this->at(10))->method('out')
  293. ->with($this->stringContains('make a valid'));
  294. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  295. $expected = array('notempty' => 'notempty');
  296. $this->assertEquals($expected, $result);
  297. }
  298. /**
  299. * test that a regular expression can be used for validation.
  300. *
  301. * @return void
  302. */
  303. public function testInteractiveFieldValidationWithRegexp() {
  304. $this->Task->initValidations();
  305. $this->Task->interactive = true;
  306. $this->Task->expects($this->any())->method('in')
  307. ->will($this->onConsecutiveCalls('/^[a-z]{0,9}$/', 'n'));
  308. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  309. $expected = array('a_z_0_9' => '/^[a-z]{0,9}$/');
  310. $this->assertEquals($expected, $result);
  311. }
  312. /**
  313. * test the validation Generation routine
  314. *
  315. * @return void
  316. */
  317. public function testNonInteractiveDoValidation() {
  318. $Model = $this->getMock('Model');
  319. $Model->primaryKey = 'id';
  320. $Model->expects($this->any())->method('schema')->will($this->returnValue(array(
  321. 'id' => array(
  322. 'type' => 'integer',
  323. 'length' => 11,
  324. 'null' => false,
  325. 'key' => 'primary',
  326. ),
  327. 'name' => array(
  328. 'type' => 'string',
  329. 'length' => 20,
  330. 'null' => false,
  331. ),
  332. 'email' => array(
  333. 'type' => 'string',
  334. 'length' => 255,
  335. 'null' => false,
  336. ),
  337. 'some_date' => array(
  338. 'type' => 'date',
  339. 'length' => '',
  340. 'null' => false,
  341. ),
  342. 'some_time' => array(
  343. 'type' => 'time',
  344. 'length' => '',
  345. 'null' => false,
  346. ),
  347. 'created' => array(
  348. 'type' => 'datetime',
  349. 'length' => '',
  350. 'null' => false,
  351. )
  352. )));
  353. $this->Task->interactive = false;
  354. $result = $this->Task->doValidation($Model);
  355. $expected = array(
  356. 'name' => array(
  357. 'notempty' => 'notempty'
  358. ),
  359. 'email' => array(
  360. 'email' => 'email',
  361. ),
  362. 'some_date' => array(
  363. 'date' => 'date'
  364. ),
  365. 'some_time' => array(
  366. 'time' => 'time'
  367. ),
  368. );
  369. $this->assertEquals($expected, $result);
  370. }
  371. /**
  372. * test that finding primary key works
  373. *
  374. * @return void
  375. */
  376. public function testFindPrimaryKey() {
  377. $fields = array(
  378. 'one' => array(),
  379. 'two' => array(),
  380. 'key' => array('key' => 'primary')
  381. );
  382. $anything = new PHPUnit_Framework_Constraint_IsAnything();
  383. $this->Task->expects($this->once())->method('in')
  384. ->with($anything, null, 'key')
  385. ->will($this->returnValue('my_field'));
  386. $result = $this->Task->findPrimaryKey($fields);
  387. $expected = 'my_field';
  388. $this->assertEquals($expected, $result);
  389. }
  390. /**
  391. * test finding Display field
  392. *
  393. * @return void
  394. */
  395. public function testFindDisplayFieldNone() {
  396. $fields = array(
  397. 'id' => array(), 'tagname' => array(), 'body' => array(),
  398. 'created' => array(), 'modified' => array()
  399. );
  400. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  401. $result = $this->Task->findDisplayField($fields);
  402. $this->assertFalse($result);
  403. }
  404. /**
  405. * Test finding a displayname from user input
  406. *
  407. * @return void
  408. */
  409. public function testFindDisplayName() {
  410. $fields = array(
  411. 'id' => array(), 'tagname' => array(), 'body' => array(),
  412. 'created' => array(), 'modified' => array()
  413. );
  414. $this->Task->expects($this->any())->method('in')
  415. ->will($this->onConsecutiveCalls('y', 2));
  416. $result = $this->Task->findDisplayField($fields);
  417. $this->assertEquals('tagname', $result);
  418. }
  419. /**
  420. * test that belongsTo generation works.
  421. *
  422. * @return void
  423. */
  424. public function testBelongsToGeneration() {
  425. $model = new Model(array('ds' => 'test', 'name' => 'BakeComment'));
  426. $result = $this->Task->findBelongsTo($model, array());
  427. $expected = array(
  428. 'belongsTo' => array(
  429. array(
  430. 'alias' => 'BakeArticle',
  431. 'className' => 'BakeArticle',
  432. 'foreignKey' => 'bake_article_id',
  433. ),
  434. array(
  435. 'alias' => 'BakeUser',
  436. 'className' => 'BakeUser',
  437. 'foreignKey' => 'bake_user_id',
  438. ),
  439. )
  440. );
  441. $this->assertEquals($expected, $result);
  442. $model = new Model(array('ds' => 'test', 'name' => 'CategoryThread'));
  443. $result = $this->Task->findBelongsTo($model, array());
  444. $expected = array(
  445. 'belongsTo' => array(
  446. array(
  447. 'alias' => 'ParentCategoryThread',
  448. 'className' => 'CategoryThread',
  449. 'foreignKey' => 'parent_id',
  450. ),
  451. )
  452. );
  453. $this->assertEquals($expected, $result);
  454. }
  455. /**
  456. * test that hasOne and/or hasMany relations are generated properly.
  457. *
  458. * @return void
  459. */
  460. public function testHasManyHasOneGeneration() {
  461. $model = new Model(array('ds' => 'test', 'name' => 'BakeArticle'));
  462. $this->Task->connection = 'test';
  463. $this->Task->listAll();
  464. $result = $this->Task->findHasOneAndMany($model, array());
  465. $expected = array(
  466. 'hasMany' => array(
  467. array(
  468. 'alias' => 'BakeComment',
  469. 'className' => 'BakeComment',
  470. 'foreignKey' => 'bake_article_id',
  471. ),
  472. ),
  473. 'hasOne' => array(
  474. array(
  475. 'alias' => 'BakeComment',
  476. 'className' => 'BakeComment',
  477. 'foreignKey' => 'bake_article_id',
  478. ),
  479. ),
  480. );
  481. $this->assertEquals($expected, $result);
  482. $model = new Model(array('ds' => 'test', 'name' => 'CategoryThread'));
  483. $result = $this->Task->findHasOneAndMany($model, array());
  484. $expected = array(
  485. 'hasOne' => array(
  486. array(
  487. 'alias' => 'ChildCategoryThread',
  488. 'className' => 'CategoryThread',
  489. 'foreignKey' => 'parent_id',
  490. ),
  491. ),
  492. 'hasMany' => array(
  493. array(
  494. 'alias' => 'ChildCategoryThread',
  495. 'className' => 'CategoryThread',
  496. 'foreignKey' => 'parent_id',
  497. ),
  498. )
  499. );
  500. $this->assertEquals($expected, $result);
  501. }
  502. /**
  503. * Test that HABTM generation works
  504. *
  505. * @return void
  506. */
  507. public function testHasAndBelongsToManyGeneration() {
  508. $model = new Model(array('ds' => 'test', 'name' => 'BakeArticle'));
  509. $this->Task->connection = 'test';
  510. $this->Task->listAll();
  511. $result = $this->Task->findHasAndBelongsToMany($model, array());
  512. $expected = array(
  513. 'hasAndBelongsToMany' => array(
  514. array(
  515. 'alias' => 'BakeTag',
  516. 'className' => 'BakeTag',
  517. 'foreignKey' => 'bake_article_id',
  518. 'joinTable' => 'bake_articles_bake_tags',
  519. 'associationForeignKey' => 'bake_tag_id',
  520. ),
  521. ),
  522. );
  523. $this->assertEquals($expected, $result);
  524. }
  525. /**
  526. * test non interactive doAssociations
  527. *
  528. * @return void
  529. */
  530. public function testDoAssociationsNonInteractive() {
  531. $this->Task->connection = 'test';
  532. $this->Task->interactive = false;
  533. $model = new Model(array('ds' => 'test', 'name' => 'BakeArticle'));
  534. $result = $this->Task->doAssociations($model);
  535. $expected = array(
  536. 'belongsTo' => array(
  537. array(
  538. 'alias' => 'BakeUser',
  539. 'className' => 'BakeUser',
  540. 'foreignKey' => 'bake_user_id',
  541. ),
  542. ),
  543. 'hasMany' => array(
  544. array(
  545. 'alias' => 'BakeComment',
  546. 'className' => 'BakeComment',
  547. 'foreignKey' => 'bake_article_id',
  548. ),
  549. ),
  550. 'hasAndBelongsToMany' => array(
  551. array(
  552. 'alias' => 'BakeTag',
  553. 'className' => 'BakeTag',
  554. 'foreignKey' => 'bake_article_id',
  555. 'joinTable' => 'bake_articles_bake_tags',
  556. 'associationForeignKey' => 'bake_tag_id',
  557. ),
  558. ),
  559. );
  560. $this->assertEquals($expected, $result);
  561. }
  562. /**
  563. * Ensure that the fixture object is correctly called.
  564. *
  565. * @return void
  566. */
  567. public function testBakeFixture() {
  568. $this->Task->plugin = 'TestPlugin';
  569. $this->Task->interactive = true;
  570. $this->Task->Fixture->expects($this->at(0))->method('bake')->with('BakeArticle', 'bake_articles');
  571. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  572. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  573. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  574. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  575. }
  576. /**
  577. * Ensure that the test object is correctly called.
  578. *
  579. * @return void
  580. */
  581. public function testBakeTest() {
  582. $this->Task->plugin = 'TestPlugin';
  583. $this->Task->interactive = true;
  584. $this->Task->Test->expects($this->at(0))->method('bake')->with('Model', 'BakeArticle');
  585. $this->Task->bakeTest('BakeArticle');
  586. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  587. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  588. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  589. }
  590. /**
  591. * test confirming of associations, and that when an association is hasMany
  592. * a question for the hasOne is also not asked.
  593. *
  594. * @return void
  595. */
  596. public function testConfirmAssociations() {
  597. $associations = array(
  598. 'hasOne' => array(
  599. array(
  600. 'alias' => 'ChildCategoryThread',
  601. 'className' => 'CategoryThread',
  602. 'foreignKey' => 'parent_id',
  603. ),
  604. ),
  605. 'hasMany' => array(
  606. array(
  607. 'alias' => 'ChildCategoryThread',
  608. 'className' => 'CategoryThread',
  609. 'foreignKey' => 'parent_id',
  610. ),
  611. ),
  612. 'belongsTo' => array(
  613. array(
  614. 'alias' => 'User',
  615. 'className' => 'User',
  616. 'foreignKey' => 'user_id',
  617. ),
  618. )
  619. );
  620. $model = new Model(array('ds' => 'test', 'name' => 'CategoryThread'));
  621. $this->Task->expects($this->any())->method('in')
  622. ->will($this->onConsecutiveCalls('n', 'y', 'n', 'n', 'n'));
  623. $result = $this->Task->confirmAssociations($model, $associations);
  624. $this->assertTrue(empty($result['hasOne']));
  625. $result = $this->Task->confirmAssociations($model, $associations);
  626. $this->assertTrue(empty($result['hasMany']));
  627. $this->assertTrue(empty($result['hasOne']));
  628. }
  629. /**
  630. * test that inOptions generates questions and only accepts a valid answer
  631. *
  632. * @return void
  633. */
  634. public function testInOptions() {
  635. $this->_useMockedOut();
  636. $options = array('one', 'two', 'three');
  637. $this->Task->expects($this->at(0))->method('out')->with('1. one');
  638. $this->Task->expects($this->at(1))->method('out')->with('2. two');
  639. $this->Task->expects($this->at(2))->method('out')->with('3. three');
  640. $this->Task->expects($this->at(3))->method('in')->will($this->returnValue(10));
  641. $this->Task->expects($this->at(4))->method('out')->with('1. one');
  642. $this->Task->expects($this->at(5))->method('out')->with('2. two');
  643. $this->Task->expects($this->at(6))->method('out')->with('3. three');
  644. $this->Task->expects($this->at(7))->method('in')->will($this->returnValue(2));
  645. $result = $this->Task->inOptions($options, 'Pick a number');
  646. $this->assertEquals(1, $result);
  647. }
  648. /**
  649. * test baking validation
  650. *
  651. * @return void
  652. */
  653. public function testBakeValidation() {
  654. $validate = array(
  655. 'name' => array(
  656. 'notempty' => 'notempty'
  657. ),
  658. 'email' => array(
  659. 'email' => 'email',
  660. ),
  661. 'some_date' => array(
  662. 'date' => 'date'
  663. ),
  664. 'some_time' => array(
  665. 'time' => 'time'
  666. )
  667. );
  668. $result = $this->Task->bake('BakeArticle', compact('validate'));
  669. $this->assertRegExp('/class BakeArticle extends AppModel \{/', $result);
  670. $this->assertRegExp('/\$validate \= array\(/', $result);
  671. $expected = <<< STRINGEND
  672. array(
  673. 'notempty' => array(
  674. 'rule' => array('notempty'),
  675. //'message' => 'Your custom message here',
  676. //'allowEmpty' => false,
  677. //'required' => false,
  678. //'last' => false, // Stop validation after this rule
  679. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  680. ),
  681. STRINGEND;
  682. $this->assertRegExp('/' . preg_quote(str_replace("\r\n", "\n", $expected), '/') . '/', $result);
  683. }
  684. /**
  685. * test baking relations
  686. *
  687. * @return void
  688. */
  689. public function testBakeRelations() {
  690. $associations = array(
  691. 'belongsTo' => array(
  692. array(
  693. 'alias' => 'SomethingElse',
  694. 'className' => 'SomethingElse',
  695. 'foreignKey' => 'something_else_id',
  696. ),
  697. array(
  698. 'alias' => 'BakeUser',
  699. 'className' => 'BakeUser',
  700. 'foreignKey' => 'bake_user_id',
  701. ),
  702. ),
  703. 'hasOne' => array(
  704. array(
  705. 'alias' => 'OtherModel',
  706. 'className' => 'OtherModel',
  707. 'foreignKey' => 'other_model_id',
  708. ),
  709. ),
  710. 'hasMany' => array(
  711. array(
  712. 'alias' => 'BakeComment',
  713. 'className' => 'BakeComment',
  714. 'foreignKey' => 'parent_id',
  715. ),
  716. ),
  717. 'hasAndBelongsToMany' => array(
  718. array(
  719. 'alias' => 'BakeTag',
  720. 'className' => 'BakeTag',
  721. 'foreignKey' => 'bake_article_id',
  722. 'joinTable' => 'bake_articles_bake_tags',
  723. 'associationForeignKey' => 'bake_tag_id',
  724. ),
  725. )
  726. );
  727. $result = $this->Task->bake('BakeArticle', compact('associations'));
  728. $this->assertContains(' * @property BakeUser $BakeUser', $result);
  729. $this->assertContains(' * @property OtherModel $OtherModel', $result);
  730. $this->assertContains(' * @property BakeComment $BakeComment', $result);
  731. $this->assertContains(' * @property BakeTag $BakeTag', $result);
  732. $this->assertRegExp('/\$hasAndBelongsToMany \= array\(/', $result);
  733. $this->assertRegExp('/\$hasMany \= array\(/', $result);
  734. $this->assertRegExp('/\$belongsTo \= array\(/', $result);
  735. $this->assertRegExp('/\$hasOne \= array\(/', $result);
  736. $this->assertRegExp('/BakeTag/', $result);
  737. $this->assertRegExp('/OtherModel/', $result);
  738. $this->assertRegExp('/SomethingElse/', $result);
  739. $this->assertRegExp('/BakeComment/', $result);
  740. }
  741. /**
  742. * test bake() with a -plugin param
  743. *
  744. * @return void
  745. */
  746. public function testBakeWithPlugin() {
  747. $this->Task->plugin = 'ControllerTest';
  748. //fake plugin path
  749. CakePlugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  750. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Model' . DS . 'BakeArticle.php';
  751. $this->Task->expects($this->once())->method('createFile')
  752. ->with($path, $this->stringContains('BakeArticle extends ControllerTestAppModel'));
  753. $result = $this->Task->bake('BakeArticle', array(), array());
  754. $this->assertContains("App::uses('ControllerTestAppModel', 'ControllerTest.Model');", $result);
  755. $this->assertEquals(count(ClassRegistry::keys()), 0);
  756. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  757. }
  758. /**
  759. * test that execute passes runs bake depending with named model.
  760. *
  761. * @return void
  762. */
  763. public function testExecuteWithNamedModel() {
  764. $this->Task->connection = 'test';
  765. $this->Task->path = '/my/path/';
  766. $this->Task->args = array('BakeArticle');
  767. $filename = '/my/path/BakeArticle.php';
  768. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  769. $this->Task->expects($this->once())->method('createFile')
  770. ->with($filename, $this->stringContains('class BakeArticle extends AppModel'));
  771. $this->Task->execute();
  772. $this->assertEquals(count(ClassRegistry::keys()), 0);
  773. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  774. }
  775. /**
  776. * data provider for testExecuteWithNamedModelVariations
  777. *
  778. * @return void
  779. */
  780. public static function nameVariations() {
  781. return array(
  782. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  783. );
  784. }
  785. /**
  786. * test that execute passes with different inflections of the same name.
  787. *
  788. * @dataProvider nameVariations
  789. * @return void
  790. */
  791. public function testExecuteWithNamedModelVariations($name) {
  792. $this->Task->connection = 'test';
  793. $this->Task->path = '/my/path/';
  794. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  795. $this->Task->args = array($name);
  796. $filename = '/my/path/BakeArticle.php';
  797. $this->Task->expects($this->at(0))->method('createFile')
  798. ->with($filename, $this->stringContains('class BakeArticle extends AppModel'));
  799. $this->Task->execute();
  800. }
  801. /**
  802. * test that execute with a model name picks up hasMany associations.
  803. *
  804. * @return void
  805. */
  806. public function testExecuteWithNamedModelHasManyCreated() {
  807. $this->Task->connection = 'test';
  808. $this->Task->path = '/my/path/';
  809. $this->Task->args = array('BakeArticle');
  810. $filename = '/my/path/BakeArticle.php';
  811. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  812. $this->Task->expects($this->at(0))->method('createFile')
  813. ->with($filename, $this->stringContains("'BakeComment' => array("));
  814. $this->Task->execute();
  815. }
  816. /**
  817. * test that execute runs all() when args[0] = all
  818. *
  819. * @return void
  820. */
  821. public function testExecuteIntoAll() {
  822. $count = count($this->Task->listAll('test'));
  823. if ($count != count($this->fixtures)) {
  824. $this->markTestSkipped('Additional tables detected.');
  825. }
  826. $this->Task->connection = 'test';
  827. $this->Task->path = '/my/path/';
  828. $this->Task->args = array('all');
  829. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  830. $this->Task->Fixture->expects($this->exactly(5))->method('bake');
  831. $this->Task->Test->expects($this->exactly(5))->method('bake');
  832. $filename = '/my/path/BakeArticle.php';
  833. $this->Task->expects($this->at(1))->method('createFile')
  834. ->with($filename, $this->stringContains('class BakeArticle'));
  835. $filename = '/my/path/BakeArticlesBakeTag.php';
  836. $this->Task->expects($this->at(2))->method('createFile')
  837. ->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
  838. $filename = '/my/path/BakeComment.php';
  839. $this->Task->expects($this->at(3))->method('createFile')
  840. ->with($filename, $this->stringContains('class BakeComment'));
  841. $filename = '/my/path/BakeComment.php';
  842. $this->Task->expects($this->at(3))->method('createFile')
  843. ->with($filename, $this->stringContains('public $primaryKey = \'otherid\';'));
  844. $filename = '/my/path/BakeTag.php';
  845. $this->Task->expects($this->at(4))->method('createFile')
  846. ->with($filename, $this->stringContains('class BakeTag'));
  847. $filename = '/my/path/BakeTag.php';
  848. $this->Task->expects($this->at(4))->method('createFile')
  849. ->with($filename, $this->logicalNot($this->stringContains('public $primaryKey')));
  850. $filename = '/my/path/CategoryThread.php';
  851. $this->Task->expects($this->at(5))->method('createFile')
  852. ->with($filename, $this->stringContains('class CategoryThread'));
  853. $this->Task->execute();
  854. $this->assertEquals(count(ClassRegistry::keys()), 0);
  855. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  856. }
  857. /**
  858. * test that odd tablenames arent inflected back from modelname
  859. *
  860. * @return void
  861. */
  862. public function testExecuteIntoAllOddTables() {
  863. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  864. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  865. $this->Task = $this->getMock('ModelTask',
  866. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
  867. array($out, $out, $in)
  868. );
  869. $this->_setupOtherMocks();
  870. $this->Task->connection = 'test';
  871. $this->Task->path = '/my/path/';
  872. $this->Task->args = array('all');
  873. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  874. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
  875. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  876. $this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
  877. $this->Task->expects($this->at(3))->method('bake')->with($object, false)->will($this->returnValue(true));
  878. $this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
  879. $this->Task->execute();
  880. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  881. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  882. $this->Task = $this->getMock('ModelTask',
  883. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'createFile'),
  884. array($out, $out, $in)
  885. );
  886. $this->_setupOtherMocks();
  887. $this->Task->connection = 'test';
  888. $this->Task->path = '/my/path/';
  889. $this->Task->args = array('all');
  890. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  891. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
  892. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  893. $this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
  894. $this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
  895. $this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
  896. $filename = '/my/path/BakeOdd.php';
  897. $this->Task->expects($this->once())->method('createFile')
  898. ->with($filename, $this->stringContains('class BakeOdd'));
  899. $filename = '/my/path/BakeOdd.php';
  900. $this->Task->expects($this->once())->method('createFile')
  901. ->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
  902. $this->Task->execute();
  903. }
  904. /**
  905. * test that odd tablenames arent inflected back from modelname
  906. *
  907. * @return void
  908. */
  909. public function testExecuteIntoBakeOddTables() {
  910. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  911. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  912. $this->Task = $this->getMock('ModelTask',
  913. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
  914. array($out, $out, $in)
  915. );
  916. $this->_setupOtherMocks();
  917. $this->Task->connection = 'test';
  918. $this->Task->path = '/my/path/';
  919. $this->Task->args = array('BakeOdd');
  920. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  921. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  922. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  923. $this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
  924. $this->Task->expects($this->once())->method('bake')->with($object, false)->will($this->returnValue(true));
  925. $this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
  926. $this->Task->execute();
  927. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  928. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  929. $this->Task = $this->getMock('ModelTask',
  930. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'createFile'),
  931. array($out, $out, $in)
  932. );
  933. $this->_setupOtherMocks();
  934. $this->Task->connection = 'test';
  935. $this->Task->path = '/my/path/';
  936. $this->Task->args = array('BakeOdd');
  937. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  938. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  939. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  940. $this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
  941. $this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
  942. $this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
  943. $filename = '/my/path/BakeOdd.php';
  944. $this->Task->expects($this->once())->method('createFile')
  945. ->with($filename, $this->stringContains('class BakeOdd'));
  946. $filename = '/my/path/BakeOdd.php';
  947. $this->Task->expects($this->once())->method('createFile')
  948. ->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
  949. $this->Task->execute();
  950. }
  951. /**
  952. * test that skipTables changes how all() works.
  953. *
  954. * @return void
  955. */
  956. public function testSkipTablesAndAll() {
  957. $count = count($this->Task->listAll('test'));
  958. if ($count != count($this->fixtures)) {
  959. $this->markTestSkipped('Additional tables detected.');
  960. }
  961. $this->Task->connection = 'test';
  962. $this->Task->path = '/my/path/';
  963. $this->Task->args = array('all');
  964. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  965. $this->Task->skipTables = array('bake_tags');
  966. $this->Task->Fixture->expects($this->exactly(4))->method('bake');
  967. $this->Task->Test->expects($this->exactly(4))->method('bake');
  968. $filename = '/my/path/BakeArticle.php';
  969. $this->Task->expects($this->at(1))->method('createFile')
  970. ->with($filename, $this->stringContains('class BakeArticle'));
  971. $filename = '/my/path/BakeArticlesBakeTag.php';
  972. $this->Task->expects($this->at(2))->method('createFile')
  973. ->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
  974. $filename = '/my/path/BakeComment.php';
  975. $this->Task->expects($this->at(3))->method('createFile')
  976. ->with($filename, $this->stringContains('class BakeComment'));
  977. $filename = '/my/path/CategoryThread.php';
  978. $this->Task->expects($this->at(4))->method('createFile')
  979. ->with($filename, $this->stringContains('class CategoryThread'));
  980. $this->Task->execute();
  981. }
  982. /**
  983. * test the interactive side of bake.
  984. *
  985. * @return void
  986. */
  987. public function testExecuteIntoInteractive() {
  988. $tables = $this->Task->listAll('test');
  989. $article = array_search('bake_articles', $tables) + 1;
  990. $this->Task->connection = 'test';
  991. $this->Task->path = '/my/path/';
  992. $this->Task->interactive = true;
  993. $this->Task->expects($this->any())->method('in')
  994. ->will($this->onConsecutiveCalls(
  995. $article, // article
  996. 'n', // no validation
  997. 'y', // associations
  998. 'y', // comment relation
  999. 'y', // user relation
  1000. 'y', // tag relation
  1001. 'n', // additional assocs
  1002. 'y' // looks good?
  1003. ));
  1004. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  1005. $this->Task->Test->expects($this->once())->method('bake');
  1006. $this->Task->Fixture->expects($this->once())->method('bake');
  1007. $filename = '/my/path/BakeArticle.php';
  1008. $this->Task->expects($this->once())->method('createFile')
  1009. ->with($filename, $this->stringContains('class BakeArticle'));
  1010. $this->Task->execute();
  1011. $this->assertEquals(count(ClassRegistry::keys()), 0);
  1012. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  1013. }
  1014. /**
  1015. * test using bake interactively with a table that does not exist.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testExecuteWithNonExistantTableName() {
  1020. $this->Task->connection = 'test';
  1021. $this->Task->path = '/my/path/';
  1022. $this->Task->expects($this->any())->method('in')
  1023. ->will($this->onConsecutiveCalls(
  1024. 'Foobar', // Or type in the name of the model
  1025. 'y', // Do you want to use this table
  1026. 'n' // Doesn't exist, continue anyway?
  1027. ));
  1028. $this->Task->execute();
  1029. }
  1030. /**
  1031. * test using bake interactively with a table that does not exist.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testForcedExecuteWithNonExistantTableName() {
  1036. $this->Task->connection = 'test';
  1037. $this->Task->path = '/my/path/';
  1038. $this->Task->expects($this->any())->method('in')
  1039. ->will($this->onConsecutiveCalls(
  1040. 'Foobar', // Or type in the name of the model
  1041. 'y', // Do you want to use this table
  1042. 'y', // Doesn't exist, continue anyway?
  1043. 'id', // Primary key
  1044. 'y' // Looks good?
  1045. ));
  1046. $this->Task->execute();
  1047. }
  1048. }