ModelTaskTest.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 1.2.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console\Command\Task;
  16. use Cake\Console\Command\Task\ModelTask;
  17. use Cake\Console\Command\Task\TemplateTask;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Model\Model;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\ClassRegistry;
  24. use Cake\Utility\Inflector;
  25. /**
  26. * ModelTaskTest class
  27. */
  28. class ModelTaskTest extends TestCase {
  29. /**
  30. * fixtures
  31. *
  32. * @var array
  33. */
  34. public $fixtures = array(
  35. 'core.bake_article', 'core.bake_comment', 'core.bake_articles_bake_tag',
  36. 'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree',
  37. 'core.counter_cache_user', 'core.counter_cache_post'
  38. );
  39. /**
  40. * setUp method
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  47. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  48. array('in', 'err', 'createFile', '_stop', '_checkUnitTest'),
  49. array($io)
  50. );
  51. $this->Task->connection = 'test';
  52. $this->_setupOtherMocks();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Setup a mock that has out mocked. Normally this is not used as it makes $this->at() really tricky.
  57. *
  58. * @return void
  59. */
  60. protected function _useMockedOut() {
  61. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  62. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  63. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
  64. array($io)
  65. );
  66. $this->_setupOtherMocks();
  67. }
  68. /**
  69. * sets up the rest of the dependencies for Model Task
  70. *
  71. * @return void
  72. */
  73. protected function _setupOtherMocks() {
  74. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  75. $this->Task->Fixture = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
  76. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
  77. $this->Task->Template = new TemplateTask($io);
  78. $this->Task->Template->interactive = false;
  79. $this->Task->name = 'Model';
  80. }
  81. /**
  82. * tearDown method
  83. *
  84. * @return void
  85. */
  86. public function tearDown() {
  87. parent::tearDown();
  88. unset($this->Task);
  89. $this->fixtureManager->shutDown();
  90. }
  91. /**
  92. * Test that listAll uses the connection property
  93. *
  94. * @return void
  95. */
  96. public function testListAllConnection() {
  97. $this->_useMockedOut();
  98. $this->Task->connection = 'test';
  99. $result = $this->Task->listAll();
  100. $this->assertContains('bake_articles', $result);
  101. $this->assertContains('bake_articles_bake_tags', $result);
  102. $this->assertContains('bake_tags', $result);
  103. $this->assertContains('bake_comments', $result);
  104. $this->assertContains('category_threads', $result);
  105. }
  106. /**
  107. * Test getName() method.
  108. *
  109. * @return void
  110. */
  111. public function testGetTable() {
  112. $result = $this->Task->getTable('BakeArticle');
  113. $this->assertEquals('bake_articles', $result);
  114. $result = $this->Task->getTable('BakeArticles');
  115. $this->assertEquals('bake_articles', $result);
  116. $this->Task->params['table'] = 'bake_articles';
  117. $result = $this->Task->getTable('Article');
  118. $this->assertEquals('bake_articles', $result);
  119. }
  120. /**
  121. * Test getting the a table class.
  122. *
  123. * @return void
  124. */
  125. public function testGetTableObject() {
  126. $result = $this->Task->getTableObject('Article', 'bake_articles');
  127. $this->assertInstanceOf('Cake\ORM\Table', $result);
  128. $this->assertEquals('bake_articles', $result->table());
  129. $this->assertEquals('Article', $result->alias());
  130. }
  131. /**
  132. * Test getAssociations with off flag.
  133. *
  134. * @return void
  135. */
  136. public function testGetAssociationsNoFlag() {
  137. $this->Task->params['no-associations'] = true;
  138. $articles = TableRegistry::get('BakeArticle');
  139. $this->assertEquals([], $this->Task->getAssociations($articles));
  140. }
  141. /**
  142. * Test applying associations.
  143. *
  144. * @return void
  145. */
  146. public function testApplyAssociations() {
  147. $articles = TableRegistry::get('BakeArticles');
  148. $assocs = [
  149. 'belongsTo' => [
  150. [
  151. 'alias' => 'BakeUsers',
  152. 'foreignKey' => 'bake_user_id',
  153. ],
  154. ],
  155. 'hasMany' => [
  156. [
  157. 'alias' => 'BakeComments',
  158. 'foreignKey' => 'bake_article_id',
  159. ],
  160. ],
  161. 'belongsToMany' => [
  162. [
  163. 'alias' => 'BakeTags',
  164. 'foreignKey' => 'bake_article_id',
  165. 'joinTable' => 'bake_articles_bake_tags',
  166. 'targetForeignKey' => 'bake_tag_id',
  167. ],
  168. ],
  169. ];
  170. $original = $articles->associations()->keys();
  171. $this->assertEquals([], $original);
  172. $this->Task->applyAssociations($articles, $assocs);
  173. $new = $articles->associations()->keys();
  174. $expected = ['bakeusers', 'bakecomments', 'baketags'];
  175. $this->assertEquals($expected, $new);
  176. }
  177. /**
  178. * Test applying associations does nothing on a concrete class
  179. *
  180. * @return void
  181. */
  182. public function testApplyAssociationsConcreteClass() {
  183. Configure::write('App.namespace', 'TestApp');
  184. $articles = TableRegistry::get('Articles');
  185. $assocs = [
  186. 'belongsTo' => [
  187. [
  188. 'alias' => 'BakeUsers',
  189. 'foreignKey' => 'bake_user_id',
  190. ],
  191. ],
  192. 'hasMany' => [
  193. [
  194. 'alias' => 'BakeComments',
  195. 'foreignKey' => 'bake_article_id',
  196. ],
  197. ],
  198. 'belongsToMany' => [
  199. [
  200. 'alias' => 'BakeTags',
  201. 'foreignKey' => 'bake_article_id',
  202. 'joinTable' => 'bake_articles_bake_tags',
  203. 'targetForeignKey' => 'bake_tag_id',
  204. ],
  205. ],
  206. ];
  207. $original = $articles->associations()->keys();
  208. $this->Task->applyAssociations($articles, $assocs);
  209. $new = $articles->associations()->keys();
  210. $this->assertEquals($original, $new);
  211. }
  212. /**
  213. * Test getAssociations
  214. *
  215. * @return void
  216. */
  217. public function testGetAssociations() {
  218. $articles = TableRegistry::get('BakeArticles');
  219. $result = $this->Task->getAssociations($articles);
  220. $expected = [
  221. 'belongsTo' => [
  222. [
  223. 'alias' => 'BakeUsers',
  224. 'foreignKey' => 'bake_user_id'
  225. ],
  226. ],
  227. 'hasMany' => [
  228. [
  229. 'alias' => 'BakeComments',
  230. 'foreignKey' => 'bake_article_id',
  231. ],
  232. ],
  233. 'belongsToMany' => [
  234. [
  235. 'alias' => 'BakeTags',
  236. 'foreignKey' => 'bake_article_id',
  237. 'joinTable' => 'bake_articles_bake_tags',
  238. 'targetForeignKey' => 'bake_tag_id',
  239. ],
  240. ],
  241. ];
  242. $this->assertEquals($expected, $result);
  243. }
  244. /**
  245. * test that belongsTo generation works.
  246. *
  247. * @return void
  248. */
  249. public function testBelongsToGeneration() {
  250. $model = TableRegistry::get('BakeComments');
  251. $result = $this->Task->findBelongsTo($model, []);
  252. $expected = [
  253. 'belongsTo' => [
  254. [
  255. 'alias' => 'BakeArticles',
  256. 'foreignKey' => 'bake_article_id'
  257. ],
  258. [
  259. 'alias' => 'BakeUsers',
  260. 'foreignKey' => 'bake_user_id'
  261. ],
  262. ]
  263. ];
  264. $this->assertEquals($expected, $result);
  265. $model = TableRegistry::get('CategoryThreads');
  266. $result = $this->Task->findBelongsTo($model, array());
  267. $expected = [
  268. 'belongsTo' => [
  269. [
  270. 'alias' => 'ParentCategoryThreads',
  271. 'className' => 'CategoryThreads',
  272. 'foreignKey' => 'parent_id'
  273. ],
  274. ]
  275. ];
  276. $this->assertEquals($expected, $result);
  277. $this->Task->plugin = 'Blog';
  278. $result = $this->Task->findBelongsTo($model, array());
  279. $expected = [
  280. 'belongsTo' => [
  281. [
  282. 'alias' => 'ParentCategoryThreads',
  283. 'className' => 'Blog.CategoryThreads',
  284. 'foreignKey' => 'parent_id'
  285. ],
  286. ]
  287. ];
  288. $this->assertEquals($expected, $result);
  289. }
  290. /**
  291. * test that hasOne and/or hasMany relations are generated properly.
  292. *
  293. * @return void
  294. */
  295. public function testHasManyGeneration() {
  296. $this->Task->connection = 'test';
  297. $model = TableRegistry::get('BakeArticles');
  298. $result = $this->Task->findHasMany($model, []);
  299. $expected = [
  300. 'hasMany' => [
  301. [
  302. 'alias' => 'BakeComments',
  303. 'foreignKey' => 'bake_article_id',
  304. ],
  305. ],
  306. ];
  307. $this->assertEquals($expected, $result);
  308. $model = TableRegistry::get('CategoryThreads');
  309. $result = $this->Task->findHasMany($model, []);
  310. $expected = [
  311. 'hasMany' => [
  312. [
  313. 'alias' => 'ChildCategoryThreads',
  314. 'className' => 'CategoryThreads',
  315. 'foreignKey' => 'parent_id',
  316. ],
  317. ]
  318. ];
  319. $this->assertEquals($expected, $result);
  320. $this->Task->plugin = 'Blog';
  321. $result = $this->Task->findHasMany($model, array());
  322. $expected = [
  323. 'hasMany' => [
  324. [
  325. 'alias' => 'ChildCategoryThreads',
  326. 'className' => 'Blog.CategoryThreads',
  327. 'foreignKey' => 'parent_id'
  328. ],
  329. ]
  330. ];
  331. $this->assertEquals($expected, $result);
  332. }
  333. /**
  334. * Test that HABTM generation works
  335. *
  336. * @return void
  337. */
  338. public function testHasAndBelongsToManyGeneration() {
  339. $this->Task->connection = 'test';
  340. $model = TableRegistry::get('BakeArticles');
  341. $result = $this->Task->findBelongsToMany($model, []);
  342. $expected = [
  343. 'belongsToMany' => [
  344. [
  345. 'alias' => 'BakeTags',
  346. 'foreignKey' => 'bake_article_id',
  347. 'joinTable' => 'bake_articles_bake_tags',
  348. 'targetForeignKey' => 'bake_tag_id',
  349. ],
  350. ],
  351. ];
  352. $this->assertEquals($expected, $result);
  353. }
  354. /**
  355. * Test getting accessible fields.
  356. *
  357. * @return void
  358. */
  359. public function testGetFields() {
  360. $model = TableRegistry::get('BakeArticles');
  361. $result = $this->Task->getFields($model);
  362. $expected = [
  363. 'bake_user_id',
  364. 'title',
  365. 'body',
  366. 'published',
  367. ];
  368. $this->assertEquals($expected, $result);
  369. }
  370. /**
  371. * Test getting accessible fields includes associations.
  372. *
  373. * @return void
  374. */
  375. public function testGetFieldsAssociations() {
  376. $model = TableRegistry::get('BakeArticles');
  377. $model->belongsToMany('BakeTags');
  378. $model->belongsTo('BakeAuthors');
  379. $model->hasMany('BakeComments');
  380. $result = $this->Task->getFields($model);
  381. $this->assertContains('bake_tags', $result);
  382. $this->assertContains('bake_comments', $result);
  383. $this->assertContains('bake_author', $result);
  384. }
  385. /**
  386. * Test getting field with the no- option
  387. *
  388. * @return void
  389. */
  390. public function testGetFieldsDisabled() {
  391. $model = TableRegistry::get('BakeArticles');
  392. $this->Task->params['no-fields'] = true;
  393. $result = $this->Task->getFields($model);
  394. $this->assertEquals([], $result);
  395. }
  396. /**
  397. * Test getting field with a whitelist
  398. *
  399. * @return void
  400. */
  401. public function testGetFieldsWhiteList() {
  402. $model = TableRegistry::get('BakeArticles');
  403. $this->Task->params['fields'] = 'id, title , , body , created';
  404. $result = $this->Task->getFields($model);
  405. $expected = [
  406. 'id',
  407. 'title',
  408. 'body',
  409. 'created',
  410. ];
  411. $this->assertEquals($expected, $result);
  412. }
  413. /**
  414. * Test getting hidden fields.
  415. *
  416. * @return void
  417. */
  418. public function testGetHiddenFields() {
  419. $model = TableRegistry::get('Users');
  420. $result = $this->Task->getHiddenFields($model);
  421. $expected = [
  422. 'password',
  423. ];
  424. $this->assertEquals($expected, $result);
  425. }
  426. /**
  427. * Test getting hidden field with the no- option
  428. *
  429. * @return void
  430. */
  431. public function testGetHiddenFieldsDisabled() {
  432. $model = TableRegistry::get('Users');
  433. $this->Task->params['no-hidden'] = true;
  434. $result = $this->Task->getHiddenFields($model);
  435. $this->assertEquals([], $result);
  436. }
  437. /**
  438. * Test getting hidden field with a whitelist
  439. *
  440. * @return void
  441. */
  442. public function testGetHiddenFieldsWhiteList() {
  443. $model = TableRegistry::get('Users');
  444. $this->Task->params['hidden'] = 'id, title , , body , created';
  445. $result = $this->Task->getHiddenFields($model);
  446. $expected = [
  447. 'id',
  448. 'title',
  449. 'body',
  450. 'created',
  451. ];
  452. $this->assertEquals($expected, $result);
  453. }
  454. /**
  455. * Test getting primary key
  456. *
  457. * @return void
  458. */
  459. public function testGetPrimaryKey() {
  460. $model = TableRegistry::get('BakeArticles');
  461. $result = $this->Task->getPrimaryKey($model);
  462. $expected = ['id'];
  463. $this->assertEquals($expected, $result);
  464. $this->Task->params['primary-key'] = 'id, , account_id';
  465. $result = $this->Task->getPrimaryKey($model);
  466. $expected = ['id', 'account_id'];
  467. $this->assertEquals($expected, $result);
  468. }
  469. /**
  470. * test getting validation rules with the no-validation rule.
  471. *
  472. * @return void
  473. */
  474. public function testGetValidationDisabled() {
  475. $model = TableRegistry::get('BakeArticles');
  476. $this->Task->params['no-validation'] = true;
  477. $result = $this->Task->getValidation($model);
  478. $this->assertEquals([], $result);
  479. }
  480. /**
  481. * test getting validation rules.
  482. *
  483. * @return void
  484. */
  485. public function testGetValidation() {
  486. $model = TableRegistry::get('BakeArticles');
  487. $result = $this->Task->getValidation($model);
  488. $expected = [
  489. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  490. 'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
  491. 'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  492. 'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
  493. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  494. ];
  495. $this->assertEquals($expected, $result);
  496. $model = TableRegistry::get('BakeComments');
  497. $result = $this->Task->getValidation($model);
  498. $expected = [
  499. 'bake_article_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  500. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  501. 'comment' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  502. 'published' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  503. 'otherid' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  504. ];
  505. $this->assertEquals($expected, $result);
  506. }
  507. /**
  508. * Tests that a username column will get a validateUnique rule applied
  509. *
  510. * @return void
  511. */
  512. public function testGetValidationWithUnique() {
  513. $model = TableRegistry::get('Users');
  514. $result = $this->Task->getValidation($model);
  515. $expected = [
  516. 'password' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  517. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']],
  518. 'username' => [
  519. 'valid' => [
  520. 'rule' => false,
  521. 'allowEmpty' => true
  522. ],
  523. 'unique' => [
  524. 'rule' => 'validateUnique',
  525. 'provider' => 'table'
  526. ]
  527. ]
  528. ];
  529. $this->assertEquals($expected, $result);
  530. }
  531. /**
  532. * test non interactive doActsAs
  533. *
  534. * @return void
  535. */
  536. public function testGetBehaviors() {
  537. $model = TableRegistry::get('NumberTrees');
  538. $result = $this->Task->getBehaviors($model);
  539. $this->assertEquals(['Tree' => []], $result);
  540. $model = TableRegistry::get('BakeArticles');
  541. $result = $this->Task->getBehaviors($model);
  542. $this->assertEquals(['Timestamp' => []], $result);
  543. TableRegistry::clear();
  544. TableRegistry::get('Users', [
  545. 'table' => 'counter_cache_users'
  546. ]);
  547. $model = TableRegistry::get('Posts', [
  548. 'table' => 'counter_cache_posts'
  549. ]);
  550. $result = $this->Task->getBehaviors($model);
  551. $expected = [
  552. 'CounterCache' => ["'Users' => ['post_count']"]
  553. ];
  554. $this->assertEquals($expected, $result);
  555. }
  556. /**
  557. * Test getDisplayField() method.
  558. *
  559. * @return void
  560. */
  561. public function testGetDisplayField() {
  562. $model = TableRegistry::get('BakeArticles');
  563. $result = $this->Task->getDisplayField($model);
  564. $this->assertEquals('title', $result);
  565. $this->Task->params['display-field'] = 'custom';
  566. $result = $this->Task->getDisplayField($model);
  567. $this->assertEquals('custom', $result);
  568. }
  569. /**
  570. * Ensure that the fixture object is correctly called.
  571. *
  572. * @return void
  573. */
  574. public function testBakeFixture() {
  575. $this->Task->plugin = 'TestPlugin';
  576. $this->Task->Fixture->expects($this->at(0))
  577. ->method('bake')
  578. ->with('BakeArticle', 'bake_articles');
  579. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  580. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  581. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  582. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  583. }
  584. /**
  585. * Ensure that the fixture baking can be disabled
  586. *
  587. * @return void
  588. */
  589. public function testBakeFixtureDisabled() {
  590. $this->Task->params['no-fixture'] = true;
  591. $this->Task->plugin = 'TestPlugin';
  592. $this->Task->Fixture->expects($this->never())
  593. ->method('bake');
  594. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  595. }
  596. /**
  597. * Ensure that the test object is correctly called.
  598. *
  599. * @return void
  600. */
  601. public function testBakeTest() {
  602. $this->Task->plugin = 'TestPlugin';
  603. $this->Task->Test->expects($this->at(0))
  604. ->method('bake')
  605. ->with('Table', 'BakeArticle');
  606. $this->Task->bakeTest('BakeArticle');
  607. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  608. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  609. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  610. }
  611. /**
  612. * Ensure that test baking can be disabled.
  613. *
  614. * @return void
  615. */
  616. public function testBakeTestDisabled() {
  617. $this->Task->params['no-test'] = true;
  618. $this->Task->plugin = 'TestPlugin';
  619. $this->Task->Test->expects($this->never())
  620. ->method('bake');
  621. $this->Task->bakeTest('BakeArticle');
  622. }
  623. /**
  624. * test baking validation
  625. *
  626. * @return void
  627. */
  628. public function testBakeTableValidation() {
  629. $validation = [
  630. 'id' => [
  631. 'valid' => array(
  632. 'allowEmpty' => 'create',
  633. 'rule' => 'numeric',
  634. )
  635. ],
  636. 'name' => [
  637. 'valid' => [
  638. 'allowEmpty' => false,
  639. 'rule' => false,
  640. ]
  641. ],
  642. 'email' => [
  643. 'valid' => [
  644. 'allowEmpty' => true,
  645. 'rule' => 'email'
  646. ],
  647. 'unique' => [
  648. 'rule' => 'validateUnique',
  649. 'provider' => 'table'
  650. ]
  651. ]
  652. ];
  653. $model = TableRegistry::get('BakeArticles');
  654. $result = $this->Task->bakeTable($model, compact('validation'));
  655. $this->assertContains('namespace App\Model\Table;', $result);
  656. $this->assertContains('use Cake\ORM\Table;', $result);
  657. $this->assertContains('use Cake\Validation\Validator;', $result);
  658. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  659. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  660. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  661. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  662. $this->assertContains(
  663. "->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
  664. $result)
  665. ;
  666. $this->assertContains("->allowEmpty('id', 'create')", $result);
  667. $this->assertContains("->allowEmpty('email')", $result);
  668. $this->assertContains("->validatePresence('name', 'create')", $result);
  669. }
  670. /**
  671. * test baking
  672. *
  673. * @return void
  674. */
  675. public function testBakeTableConfig() {
  676. $config = [
  677. 'table' => 'articles',
  678. 'primaryKey' => ['id'],
  679. 'displayField' => 'title',
  680. 'behaviors' => ['Timestamp' => ''],
  681. ];
  682. $model = TableRegistry::get('BakeArticles');
  683. $result = $this->Task->bakeTable($model, $config);
  684. $this->assertContains('public function initialize(array $config) {', $result);
  685. $this->assertContains("this->primaryKey(['id']);\n", $result);
  686. $this->assertContains("this->displayField('title');\n", $result);
  687. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  688. $this->assertContains("this->table('articles');\n", $result);
  689. $this->assertContains('use Cake\Validation\Validator;', $result);
  690. }
  691. /**
  692. * test baking relations
  693. *
  694. * @return void
  695. */
  696. public function testBakeTableRelations() {
  697. $associations = [
  698. 'belongsTo' => [
  699. [
  700. 'alias' => 'SomethingElse',
  701. 'foreignKey' => 'something_else_id',
  702. ],
  703. [
  704. 'alias' => 'BakeUser',
  705. 'foreignKey' => 'bake_user_id',
  706. ],
  707. ],
  708. 'hasMany' => [
  709. [
  710. 'alias' => 'BakeComment',
  711. 'foreignKey' => 'parent_id',
  712. ],
  713. ],
  714. 'belongsToMany' => [
  715. [
  716. 'alias' => 'BakeTag',
  717. 'foreignKey' => 'bake_article_id',
  718. 'joinTable' => 'bake_articles_bake_tags',
  719. 'targetForeignKey' => 'bake_tag_id',
  720. ],
  721. ]
  722. ];
  723. $model = TableRegistry::get('BakeArticles');
  724. $result = $this->Task->bakeTable($model, compact('associations'));
  725. $this->assertContains("this->hasMany('BakeComment', [", $result);
  726. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  727. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  728. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  729. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  730. }
  731. /**
  732. * test baking an entity class
  733. *
  734. * @return void
  735. */
  736. public function testBakeEntity() {
  737. $config = [
  738. 'fields' => []
  739. ];
  740. $model = TableRegistry::get('BakeArticles');
  741. $result = $this->Task->bakeEntity($model, $config);
  742. $this->assertContains('namespace App\Model\Entity;', $result);
  743. $this->assertContains('use Cake\ORM\Entity;', $result);
  744. $this->assertContains('class BakeArticle extends Entity {', $result);
  745. $this->assertNotContains('$_accessible', $result);
  746. }
  747. /**
  748. * test baking an entity class
  749. *
  750. * @return void
  751. */
  752. public function testBakeEntityFields() {
  753. $config = [
  754. 'fields' => ['title', 'body', 'published']
  755. ];
  756. $model = TableRegistry::get('BakeArticles');
  757. $result = $this->Task->bakeEntity($model, $config);
  758. $this->assertContains("protected \$_accessible = [", $result);
  759. $this->assertContains("'title' => true,", $result);
  760. $this->assertContains("'body' => true,", $result);
  761. $this->assertContains("'published' => true", $result);
  762. $this->assertNotContains("protected \$_hidden", $result);
  763. }
  764. /**
  765. * test baking an entity class sets hidden fields.
  766. *
  767. * @return void
  768. */
  769. public function testBakeEntityHidden() {
  770. $model = TableRegistry::get('BakeUsers');
  771. $config = [
  772. 'hidden' => ['password'],
  773. ];
  774. $result = $this->Task->bakeEntity($model, $config);
  775. $this->assertContains("protected \$_hidden = [", $result);
  776. $this->assertContains("'password'", $result);
  777. $this->assertNotContains("protected \$_accessible", $result);
  778. }
  779. /**
  780. * test bake() with a -plugin param
  781. *
  782. * @return void
  783. */
  784. public function testBakeTableWithPlugin() {
  785. $this->Task->plugin = 'ControllerTest';
  786. // fake plugin path
  787. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  788. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
  789. $this->Task->expects($this->once())->method('createFile')
  790. ->with($path, $this->logicalAnd(
  791. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  792. $this->stringContains('use Cake\\ORM\\Table;'),
  793. $this->stringContains('class BakeArticlesTable extends Table {')
  794. ));
  795. $model = TableRegistry::get('BakeArticles');
  796. $this->Task->bakeTable($model);
  797. }
  798. /**
  799. * test bake() with a -plugin param
  800. *
  801. * @return void
  802. */
  803. public function testBakeEntityWithPlugin() {
  804. $this->Task->plugin = 'ControllerTest';
  805. // fake plugin path
  806. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  807. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  808. $path = $this->_normalizePath($path);
  809. $this->Task->expects($this->once())->method('createFile')
  810. ->with($path, $this->logicalAnd(
  811. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  812. $this->stringContains('use Cake\\ORM\\Entity;'),
  813. $this->stringContains('class BakeArticle extends Entity {')
  814. ));
  815. $model = TableRegistry::get('BakeArticles');
  816. $this->Task->bakeEntity($model);
  817. }
  818. /**
  819. * test that execute with no args
  820. *
  821. * @return void
  822. */
  823. public function testMainNoArgs() {
  824. $this->_useMockedOut();
  825. $this->Task->connection = 'test';
  826. $this->Task->path = '/my/path/';
  827. $this->Task->expects($this->at(0))
  828. ->method('out')
  829. ->with($this->stringContains('Choose a model to bake from the following:'));
  830. $this->Task->main();
  831. }
  832. /**
  833. * test that execute passes runs bake depending with named model.
  834. *
  835. * @return void
  836. */
  837. public function testMainWithNamedModel() {
  838. $this->Task->connection = 'test';
  839. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  840. $this->Task->expects($this->at(0))
  841. ->method('createFile')
  842. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  843. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  844. $this->Task->expects($this->at(1))
  845. ->method('createFile')
  846. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  847. $this->Task->main('BakeArticles');
  848. }
  849. /**
  850. * data provider for testMainWithNamedModelVariations
  851. *
  852. * @return void
  853. */
  854. public static function nameVariations() {
  855. return array(
  856. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  857. );
  858. }
  859. /**
  860. * test that execute passes with different inflections of the same name.
  861. *
  862. * @dataProvider nameVariations
  863. * @return void
  864. */
  865. public function testMainWithNamedModelVariations($name) {
  866. $this->Task->connection = 'test';
  867. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  868. $this->Task->expects($this->at(0))
  869. ->method('createFile')
  870. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  871. $this->Task->main($name);
  872. }
  873. /**
  874. * test that execute runs all() when args[0] = all
  875. *
  876. * @return void
  877. */
  878. public function testMainIntoAll() {
  879. $count = count($this->Task->listAll());
  880. if ($count != count($this->fixtures)) {
  881. $this->markTestSkipped('Additional tables detected.');
  882. }
  883. $this->Task->connection = 'test';
  884. $this->Task->Fixture->expects($this->exactly($count))
  885. ->method('bake');
  886. $this->Task->Test->expects($this->exactly($count))
  887. ->method('bake');
  888. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  889. $this->Task->expects($this->at(0))
  890. ->method('createFile')
  891. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  892. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  893. $this->Task->expects($this->at(1))
  894. ->method('createFile')
  895. ->with($filename, $this->stringContains('class BakeArticle extends'));
  896. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  897. $this->Task->expects($this->at(2))
  898. ->method('createFile')
  899. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  900. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  901. $this->Task->expects($this->at(3))
  902. ->method('createFile')
  903. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  904. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  905. $this->Task->expects($this->at(4))
  906. ->method('createFile')
  907. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  908. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  909. $this->Task->expects($this->at(5))
  910. ->method('createFile')
  911. ->with($filename, $this->stringContains('class BakeComment extends'));
  912. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  913. $this->Task->expects($this->at(6))
  914. ->method('createFile')
  915. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  916. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  917. $this->Task->expects($this->at(7))
  918. ->method('createFile')
  919. ->with($filename, $this->stringContains('class BakeTag extends'));
  920. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  921. $this->Task->expects($this->at(8))
  922. ->method('createFile')
  923. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  924. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  925. $this->Task->expects($this->at(9))
  926. ->method('createFile')
  927. ->with($filename, $this->stringContains('class CategoryThread extends'));
  928. $this->Task->all();
  929. }
  930. /**
  931. * test that skipTables changes how all() works.
  932. *
  933. * @return void
  934. */
  935. public function testSkipTablesAndAll() {
  936. $count = count($this->Task->listAll('test'));
  937. if ($count != count($this->fixtures)) {
  938. $this->markTestSkipped('Additional tables detected.');
  939. }
  940. $this->Task->connection = 'test';
  941. $this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
  942. $this->Task->Fixture->expects($this->exactly(7))
  943. ->method('bake');
  944. $this->Task->Test->expects($this->exactly(7))
  945. ->method('bake');
  946. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  947. $this->Task->expects($this->at(1))
  948. ->method('createFile')
  949. ->with($filename);
  950. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  951. $this->Task->expects($this->at(3))
  952. ->method('createFile')
  953. ->with($filename);
  954. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  955. $this->Task->expects($this->at(5))
  956. ->method('createFile')
  957. ->with($filename);
  958. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  959. $this->Task->expects($this->at(7))
  960. ->method('createFile')
  961. ->with($filename);
  962. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  963. $this->Task->expects($this->at(9))
  964. ->method('createFile')
  965. ->with($filename);
  966. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  967. $this->Task->expects($this->at(11))
  968. ->method('createFile')
  969. ->with($filename);
  970. $this->Task->all();
  971. }
  972. }