ModelTaskTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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', 'core.articles_tag'
  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 getAssociations in a plugin
  246. *
  247. * @return void
  248. */
  249. public function testGetAssociationsPlugin() {
  250. $articles = TableRegistry::get('BakeArticles');
  251. $this->Task->plugin = 'TestPlugin';
  252. $result = $this->Task->getAssociations($articles);
  253. $expected = [
  254. 'belongsTo' => [
  255. [
  256. 'alias' => 'BakeUsers',
  257. 'className' => 'TestPlugin.BakeUsers',
  258. 'foreignKey' => 'bake_user_id'
  259. ],
  260. ],
  261. 'hasMany' => [
  262. [
  263. 'alias' => 'BakeComments',
  264. 'className' => 'TestPlugin.BakeComments',
  265. 'foreignKey' => 'bake_article_id',
  266. ],
  267. ],
  268. 'belongsToMany' => [
  269. [
  270. 'alias' => 'BakeTags',
  271. 'className' => 'TestPlugin.BakeTags',
  272. 'foreignKey' => 'bake_article_id',
  273. 'joinTable' => 'bake_articles_bake_tags',
  274. 'targetForeignKey' => 'bake_tag_id',
  275. ],
  276. ],
  277. ];
  278. $this->assertEquals($expected, $result);
  279. }
  280. /**
  281. * test that belongsTo generation works.
  282. *
  283. * @return void
  284. */
  285. public function testBelongsToGeneration() {
  286. $model = TableRegistry::get('BakeComments');
  287. $result = $this->Task->findBelongsTo($model, []);
  288. $expected = [
  289. 'belongsTo' => [
  290. [
  291. 'alias' => 'BakeArticles',
  292. 'foreignKey' => 'bake_article_id'
  293. ],
  294. [
  295. 'alias' => 'BakeUsers',
  296. 'foreignKey' => 'bake_user_id'
  297. ],
  298. ]
  299. ];
  300. $this->assertEquals($expected, $result);
  301. $model = TableRegistry::get('CategoryThreads');
  302. $result = $this->Task->findBelongsTo($model, array());
  303. $expected = [
  304. 'belongsTo' => [
  305. [
  306. 'alias' => 'ParentCategoryThreads',
  307. 'className' => 'CategoryThreads',
  308. 'foreignKey' => 'parent_id'
  309. ],
  310. ]
  311. ];
  312. $this->assertEquals($expected, $result);
  313. $this->Task->plugin = 'Blog';
  314. $result = $this->Task->findBelongsTo($model, array());
  315. $expected = [
  316. 'belongsTo' => [
  317. [
  318. 'alias' => 'ParentCategoryThreads',
  319. 'className' => 'Blog.CategoryThreads',
  320. 'foreignKey' => 'parent_id'
  321. ],
  322. ]
  323. ];
  324. $this->assertEquals($expected, $result);
  325. }
  326. /**
  327. * Test that belongsTo generation works for models with composite
  328. * primary keys
  329. *
  330. * @return void
  331. */
  332. public function testBelongsToGenerationCompositeKey() {
  333. $model = TableRegistry::get('ArticlesTags');
  334. $result = $this->Task->findBelongsTo($model, []);
  335. $expected = [
  336. 'belongsTo' => [
  337. [
  338. 'alias' => 'Articles',
  339. 'foreignKey' => 'article_id'
  340. ],
  341. [
  342. 'alias' => 'Tags',
  343. 'foreignKey' => 'tag_id'
  344. ]
  345. ]
  346. ];
  347. $this->assertEquals($expected, $result);
  348. }
  349. /**
  350. * test that hasOne and/or hasMany relations are generated properly.
  351. *
  352. * @return void
  353. */
  354. public function testHasManyGeneration() {
  355. $this->Task->connection = 'test';
  356. $model = TableRegistry::get('BakeArticles');
  357. $result = $this->Task->findHasMany($model, []);
  358. $expected = [
  359. 'hasMany' => [
  360. [
  361. 'alias' => 'BakeComments',
  362. 'foreignKey' => 'bake_article_id',
  363. ],
  364. ],
  365. ];
  366. $this->assertEquals($expected, $result);
  367. $model = TableRegistry::get('CategoryThreads');
  368. $result = $this->Task->findHasMany($model, []);
  369. $expected = [
  370. 'hasMany' => [
  371. [
  372. 'alias' => 'ChildCategoryThreads',
  373. 'className' => 'CategoryThreads',
  374. 'foreignKey' => 'parent_id',
  375. ],
  376. ]
  377. ];
  378. $this->assertEquals($expected, $result);
  379. $this->Task->plugin = 'Blog';
  380. $result = $this->Task->findHasMany($model, array());
  381. $expected = [
  382. 'hasMany' => [
  383. [
  384. 'alias' => 'ChildCategoryThreads',
  385. 'className' => 'Blog.CategoryThreads',
  386. 'foreignKey' => 'parent_id'
  387. ],
  388. ]
  389. ];
  390. $this->assertEquals($expected, $result);
  391. }
  392. /**
  393. * Test that HABTM generation works
  394. *
  395. * @return void
  396. */
  397. public function testHasAndBelongsToManyGeneration() {
  398. $this->Task->connection = 'test';
  399. $model = TableRegistry::get('BakeArticles');
  400. $result = $this->Task->findBelongsToMany($model, []);
  401. $expected = [
  402. 'belongsToMany' => [
  403. [
  404. 'alias' => 'BakeTags',
  405. 'foreignKey' => 'bake_article_id',
  406. 'joinTable' => 'bake_articles_bake_tags',
  407. 'targetForeignKey' => 'bake_tag_id',
  408. ],
  409. ],
  410. ];
  411. $this->assertEquals($expected, $result);
  412. }
  413. /**
  414. * Test getting accessible fields.
  415. *
  416. * @return void
  417. */
  418. public function testGetFields() {
  419. $model = TableRegistry::get('BakeArticles');
  420. $result = $this->Task->getFields($model);
  421. $expected = [
  422. 'bake_user_id',
  423. 'title',
  424. 'body',
  425. 'published',
  426. ];
  427. $this->assertEquals($expected, $result);
  428. }
  429. /**
  430. * Test getting accessible fields includes associations.
  431. *
  432. * @return void
  433. */
  434. public function testGetFieldsAssociations() {
  435. $model = TableRegistry::get('BakeArticles');
  436. $model->belongsToMany('BakeTags');
  437. $model->belongsTo('BakeAuthors');
  438. $model->hasMany('BakeComments');
  439. $result = $this->Task->getFields($model);
  440. $this->assertContains('bake_tags', $result);
  441. $this->assertContains('bake_comments', $result);
  442. $this->assertContains('bake_author', $result);
  443. }
  444. /**
  445. * Test getting field with the no- option
  446. *
  447. * @return void
  448. */
  449. public function testGetFieldsDisabled() {
  450. $model = TableRegistry::get('BakeArticles');
  451. $this->Task->params['no-fields'] = true;
  452. $result = $this->Task->getFields($model);
  453. $this->assertEquals([], $result);
  454. }
  455. /**
  456. * Test getting field with a whitelist
  457. *
  458. * @return void
  459. */
  460. public function testGetFieldsWhiteList() {
  461. $model = TableRegistry::get('BakeArticles');
  462. $this->Task->params['fields'] = 'id, title , , body , created';
  463. $result = $this->Task->getFields($model);
  464. $expected = [
  465. 'id',
  466. 'title',
  467. 'body',
  468. 'created',
  469. ];
  470. $this->assertEquals($expected, $result);
  471. }
  472. /**
  473. * Test getting hidden fields.
  474. *
  475. * @return void
  476. */
  477. public function testGetHiddenFields() {
  478. $model = TableRegistry::get('Users');
  479. $result = $this->Task->getHiddenFields($model);
  480. $expected = [
  481. 'password',
  482. ];
  483. $this->assertEquals($expected, $result);
  484. }
  485. /**
  486. * Test getting hidden field with the no- option
  487. *
  488. * @return void
  489. */
  490. public function testGetHiddenFieldsDisabled() {
  491. $model = TableRegistry::get('Users');
  492. $this->Task->params['no-hidden'] = true;
  493. $result = $this->Task->getHiddenFields($model);
  494. $this->assertEquals([], $result);
  495. }
  496. /**
  497. * Test getting hidden field with a whitelist
  498. *
  499. * @return void
  500. */
  501. public function testGetHiddenFieldsWhiteList() {
  502. $model = TableRegistry::get('Users');
  503. $this->Task->params['hidden'] = 'id, title , , body , created';
  504. $result = $this->Task->getHiddenFields($model);
  505. $expected = [
  506. 'id',
  507. 'title',
  508. 'body',
  509. 'created',
  510. ];
  511. $this->assertEquals($expected, $result);
  512. }
  513. /**
  514. * Test getting primary key
  515. *
  516. * @return void
  517. */
  518. public function testGetPrimaryKey() {
  519. $model = TableRegistry::get('BakeArticles');
  520. $result = $this->Task->getPrimaryKey($model);
  521. $expected = ['id'];
  522. $this->assertEquals($expected, $result);
  523. $this->Task->params['primary-key'] = 'id, , account_id';
  524. $result = $this->Task->getPrimaryKey($model);
  525. $expected = ['id', 'account_id'];
  526. $this->assertEquals($expected, $result);
  527. }
  528. /**
  529. * test getting validation rules with the no-validation rule.
  530. *
  531. * @return void
  532. */
  533. public function testGetValidationDisabled() {
  534. $model = TableRegistry::get('BakeArticles');
  535. $this->Task->params['no-validation'] = true;
  536. $result = $this->Task->getValidation($model);
  537. $this->assertEquals([], $result);
  538. }
  539. /**
  540. * test getting validation rules.
  541. *
  542. * @return void
  543. */
  544. public function testGetValidation() {
  545. $model = TableRegistry::get('BakeArticles');
  546. $result = $this->Task->getValidation($model);
  547. $expected = [
  548. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  549. 'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
  550. 'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  551. 'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
  552. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  553. ];
  554. $this->assertEquals($expected, $result);
  555. $model = TableRegistry::get('BakeComments');
  556. $result = $this->Task->getValidation($model);
  557. $expected = [
  558. 'bake_article_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  559. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  560. 'comment' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  561. 'published' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  562. 'otherid' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  563. ];
  564. $this->assertEquals($expected, $result);
  565. }
  566. /**
  567. * Tests that a username column will get a validateUnique rule applied
  568. *
  569. * @return void
  570. */
  571. public function testGetValidationWithUnique() {
  572. $model = TableRegistry::get('Users');
  573. $result = $this->Task->getValidation($model);
  574. $expected = [
  575. 'password' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  576. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']],
  577. 'username' => [
  578. 'valid' => [
  579. 'rule' => false,
  580. 'allowEmpty' => true
  581. ],
  582. 'unique' => [
  583. 'rule' => 'validateUnique',
  584. 'provider' => 'table'
  585. ]
  586. ]
  587. ];
  588. $this->assertEquals($expected, $result);
  589. }
  590. /**
  591. * test non interactive doActsAs
  592. *
  593. * @return void
  594. */
  595. public function testGetBehaviors() {
  596. $model = TableRegistry::get('NumberTrees');
  597. $result = $this->Task->getBehaviors($model);
  598. $this->assertEquals(['Tree' => []], $result);
  599. $model = TableRegistry::get('BakeArticles');
  600. $result = $this->Task->getBehaviors($model);
  601. $this->assertEquals(['Timestamp' => []], $result);
  602. TableRegistry::clear();
  603. TableRegistry::get('Users', [
  604. 'table' => 'counter_cache_users'
  605. ]);
  606. $model = TableRegistry::get('Posts', [
  607. 'table' => 'counter_cache_posts'
  608. ]);
  609. $result = $this->Task->getBehaviors($model);
  610. $expected = [
  611. 'CounterCache' => ["'Users' => ['post_count']"]
  612. ];
  613. $this->assertEquals($expected, $result);
  614. }
  615. /**
  616. * Test getDisplayField() method.
  617. *
  618. * @return void
  619. */
  620. public function testGetDisplayField() {
  621. $model = TableRegistry::get('BakeArticles');
  622. $result = $this->Task->getDisplayField($model);
  623. $this->assertEquals('title', $result);
  624. $this->Task->params['display-field'] = 'custom';
  625. $result = $this->Task->getDisplayField($model);
  626. $this->assertEquals('custom', $result);
  627. }
  628. /**
  629. * Ensure that the fixture object is correctly called.
  630. *
  631. * @return void
  632. */
  633. public function testBakeFixture() {
  634. $this->Task->plugin = 'TestPlugin';
  635. $this->Task->Fixture->expects($this->at(0))
  636. ->method('bake')
  637. ->with('BakeArticle', 'bake_articles');
  638. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  639. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  640. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  641. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  642. }
  643. /**
  644. * Ensure that the fixture baking can be disabled
  645. *
  646. * @return void
  647. */
  648. public function testBakeFixtureDisabled() {
  649. $this->Task->params['no-fixture'] = true;
  650. $this->Task->plugin = 'TestPlugin';
  651. $this->Task->Fixture->expects($this->never())
  652. ->method('bake');
  653. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  654. }
  655. /**
  656. * Ensure that the test object is correctly called.
  657. *
  658. * @return void
  659. */
  660. public function testBakeTest() {
  661. $this->Task->plugin = 'TestPlugin';
  662. $this->Task->Test->expects($this->at(0))
  663. ->method('bake')
  664. ->with('Table', 'BakeArticle');
  665. $this->Task->bakeTest('BakeArticle');
  666. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  667. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  668. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  669. }
  670. /**
  671. * Ensure that test baking can be disabled.
  672. *
  673. * @return void
  674. */
  675. public function testBakeTestDisabled() {
  676. $this->Task->params['no-test'] = true;
  677. $this->Task->plugin = 'TestPlugin';
  678. $this->Task->Test->expects($this->never())
  679. ->method('bake');
  680. $this->Task->bakeTest('BakeArticle');
  681. }
  682. /**
  683. * test baking validation
  684. *
  685. * @return void
  686. */
  687. public function testBakeTableValidation() {
  688. $validation = [
  689. 'id' => [
  690. 'valid' => array(
  691. 'allowEmpty' => 'create',
  692. 'rule' => 'numeric',
  693. )
  694. ],
  695. 'name' => [
  696. 'valid' => [
  697. 'allowEmpty' => false,
  698. 'rule' => false,
  699. ]
  700. ],
  701. 'email' => [
  702. 'valid' => [
  703. 'allowEmpty' => true,
  704. 'rule' => 'email'
  705. ],
  706. 'unique' => [
  707. 'rule' => 'validateUnique',
  708. 'provider' => 'table'
  709. ]
  710. ]
  711. ];
  712. $model = TableRegistry::get('BakeArticles');
  713. $result = $this->Task->bakeTable($model, compact('validation'));
  714. $this->assertContains('namespace App\Model\Table;', $result);
  715. $this->assertContains('use Cake\ORM\Table;', $result);
  716. $this->assertContains('use Cake\Validation\Validator;', $result);
  717. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  718. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  719. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  720. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  721. $this->assertContains(
  722. "->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
  723. $result);
  724. $this->assertContains("->allowEmpty('id', 'create')", $result);
  725. $this->assertContains("->allowEmpty('email')", $result);
  726. $this->assertContains("->validatePresence('name', 'create')", $result);
  727. }
  728. /**
  729. * test baking
  730. *
  731. * @return void
  732. */
  733. public function testBakeTableConfig() {
  734. $config = [
  735. 'table' => 'articles',
  736. 'primaryKey' => ['id'],
  737. 'displayField' => 'title',
  738. 'behaviors' => ['Timestamp' => ''],
  739. ];
  740. $model = TableRegistry::get('BakeArticles');
  741. $result = $this->Task->bakeTable($model, $config);
  742. $this->assertContains('public function initialize(array $config) {', $result);
  743. $this->assertContains("this->primaryKey('id');\n", $result);
  744. $this->assertContains("this->displayField('title');\n", $result);
  745. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  746. $this->assertContains("this->table('articles');\n", $result);
  747. $this->assertContains('use Cake\Validation\Validator;', $result);
  748. }
  749. /**
  750. * test baking relations
  751. *
  752. * @return void
  753. */
  754. public function testBakeTableRelations() {
  755. $associations = [
  756. 'belongsTo' => [
  757. [
  758. 'alias' => 'SomethingElse',
  759. 'foreignKey' => 'something_else_id',
  760. ],
  761. [
  762. 'alias' => 'BakeUser',
  763. 'foreignKey' => 'bake_user_id',
  764. ],
  765. ],
  766. 'hasMany' => [
  767. [
  768. 'alias' => 'BakeComment',
  769. 'foreignKey' => 'parent_id',
  770. ],
  771. ],
  772. 'belongsToMany' => [
  773. [
  774. 'alias' => 'BakeTag',
  775. 'foreignKey' => 'bake_article_id',
  776. 'joinTable' => 'bake_articles_bake_tags',
  777. 'targetForeignKey' => 'bake_tag_id',
  778. ],
  779. ]
  780. ];
  781. $model = TableRegistry::get('BakeArticles');
  782. $result = $this->Task->bakeTable($model, compact('associations'));
  783. $this->assertContains("this->hasMany('BakeComment', [", $result);
  784. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  785. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  786. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  787. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  788. }
  789. /**
  790. * test baking an entity class
  791. *
  792. * @return void
  793. */
  794. public function testBakeEntity() {
  795. $config = [
  796. 'fields' => []
  797. ];
  798. $model = TableRegistry::get('BakeArticles');
  799. $result = $this->Task->bakeEntity($model, $config);
  800. $this->assertContains('namespace App\Model\Entity;', $result);
  801. $this->assertContains('use Cake\ORM\Entity;', $result);
  802. $this->assertContains('class BakeArticle extends Entity {', $result);
  803. $this->assertNotContains('$_accessible', $result);
  804. }
  805. /**
  806. * test baking an entity class
  807. *
  808. * @return void
  809. */
  810. public function testBakeEntityFields() {
  811. $config = [
  812. 'fields' => ['title', 'body', 'published']
  813. ];
  814. $model = TableRegistry::get('BakeArticles');
  815. $result = $this->Task->bakeEntity($model, $config);
  816. $this->assertContains("protected \$_accessible = [", $result);
  817. $this->assertContains("'title' => true,", $result);
  818. $this->assertContains("'body' => true,", $result);
  819. $this->assertContains("'published' => true", $result);
  820. $this->assertNotContains("protected \$_hidden", $result);
  821. }
  822. /**
  823. * test baking an entity class sets hidden fields.
  824. *
  825. * @return void
  826. */
  827. public function testBakeEntityHidden() {
  828. $model = TableRegistry::get('BakeUsers');
  829. $config = [
  830. 'hidden' => ['password'],
  831. ];
  832. $result = $this->Task->bakeEntity($model, $config);
  833. $this->assertContains("protected \$_hidden = [", $result);
  834. $this->assertContains("'password'", $result);
  835. $this->assertNotContains("protected \$_accessible", $result);
  836. }
  837. /**
  838. * test bake() with a -plugin param
  839. *
  840. * @return void
  841. */
  842. public function testBakeTableWithPlugin() {
  843. $this->Task->plugin = 'ControllerTest';
  844. // fake plugin path
  845. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  846. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
  847. $this->Task->expects($this->once())->method('createFile')
  848. ->with($path, $this->logicalAnd(
  849. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  850. $this->stringContains('use Cake\\ORM\\Table;'),
  851. $this->stringContains('class BakeArticlesTable extends Table {')
  852. ));
  853. $model = TableRegistry::get('BakeArticles');
  854. $this->Task->bakeTable($model);
  855. }
  856. /**
  857. * test bake() with a -plugin param
  858. *
  859. * @return void
  860. */
  861. public function testBakeEntityWithPlugin() {
  862. $this->Task->plugin = 'ControllerTest';
  863. // fake plugin path
  864. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  865. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  866. $path = $this->_normalizePath($path);
  867. $this->Task->expects($this->once())->method('createFile')
  868. ->with($path, $this->logicalAnd(
  869. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  870. $this->stringContains('use Cake\\ORM\\Entity;'),
  871. $this->stringContains('class BakeArticle extends Entity {')
  872. ));
  873. $model = TableRegistry::get('BakeArticles');
  874. $this->Task->bakeEntity($model);
  875. }
  876. /**
  877. * test that execute with no args
  878. *
  879. * @return void
  880. */
  881. public function testMainNoArgs() {
  882. $this->_useMockedOut();
  883. $this->Task->connection = 'test';
  884. $this->Task->path = '/my/path/';
  885. $this->Task->expects($this->at(0))
  886. ->method('out')
  887. ->with($this->stringContains('Choose a model to bake from the following:'));
  888. $this->Task->main();
  889. }
  890. /**
  891. * test that execute passes runs bake depending with named model.
  892. *
  893. * @return void
  894. */
  895. public function testMainWithNamedModel() {
  896. $this->Task->connection = 'test';
  897. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  898. $this->Task->expects($this->at(0))
  899. ->method('createFile')
  900. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  901. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  902. $this->Task->expects($this->at(1))
  903. ->method('createFile')
  904. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  905. $this->Task->main('BakeArticles');
  906. }
  907. /**
  908. * data provider for testMainWithNamedModelVariations
  909. *
  910. * @return void
  911. */
  912. public static function nameVariations() {
  913. return array(
  914. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  915. );
  916. }
  917. /**
  918. * test that execute passes with different inflections of the same name.
  919. *
  920. * @dataProvider nameVariations
  921. * @return void
  922. */
  923. public function testMainWithNamedModelVariations($name) {
  924. $this->Task->connection = 'test';
  925. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  926. $this->Task->expects($this->at(0))
  927. ->method('createFile')
  928. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  929. $this->Task->main($name);
  930. }
  931. /**
  932. * test that execute runs all() when args[0] = all
  933. *
  934. * @return void
  935. */
  936. public function testMainIntoAll() {
  937. $count = count($this->Task->listAll());
  938. if ($count != count($this->fixtures)) {
  939. $this->markTestSkipped('Additional tables detected.');
  940. }
  941. $this->Task->connection = 'test';
  942. $this->Task->Fixture->expects($this->exactly($count))
  943. ->method('bake');
  944. $this->Task->Test->expects($this->exactly($count))
  945. ->method('bake');
  946. $filename = $this->_normalizePath(APP . 'Model/Table/ArticlesTagsTable.php');
  947. $this->Task->expects($this->at(1))
  948. ->method('createFile')
  949. ->with($filename, $this->stringContains('class ArticlesTagsTable extends'));
  950. $filename = $this->_normalizePath(APP . 'Model/Entity/ArticlesTag.php');
  951. $this->Task->expects($this->at(2))
  952. ->method('createFile')
  953. ->with($filename, $this->stringContains('class ArticlesTag extends'));
  954. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  955. $this->Task->expects($this->at(3))
  956. ->method('createFile')
  957. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  958. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  959. $this->Task->expects($this->at(4))
  960. ->method('createFile')
  961. ->with($filename, $this->stringContains('class BakeArticle extends'));
  962. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  963. $this->Task->expects($this->at(5))
  964. ->method('createFile')
  965. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  966. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  967. $this->Task->expects($this->at(6))
  968. ->method('createFile')
  969. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  970. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  971. $this->Task->expects($this->at(7))
  972. ->method('createFile')
  973. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  974. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  975. $this->Task->expects($this->at(8))
  976. ->method('createFile')
  977. ->with($filename, $this->stringContains('class BakeComment extends'));
  978. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  979. $this->Task->expects($this->at(9))
  980. ->method('createFile')
  981. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  982. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  983. $this->Task->expects($this->at(10))
  984. ->method('createFile')
  985. ->with($filename, $this->stringContains('class BakeTag extends'));
  986. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  987. $this->Task->expects($this->at(11))
  988. ->method('createFile')
  989. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  990. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  991. $this->Task->expects($this->at(12))
  992. ->method('createFile')
  993. ->with($filename, $this->stringContains('class CategoryThread extends'));
  994. $this->Task->all();
  995. }
  996. /**
  997. * test that skipTables changes how all() works.
  998. *
  999. * @return void
  1000. */
  1001. public function testSkipTablesAndAll() {
  1002. $count = count($this->Task->listAll('test'));
  1003. if ($count != count($this->fixtures)) {
  1004. $this->markTestSkipped('Additional tables detected.');
  1005. }
  1006. $this->Task->connection = 'test';
  1007. $this->Task->skipTables = ['articles_tags', 'bake_tags', 'counter_cache_posts'];
  1008. $this->Task->Fixture->expects($this->exactly(7))
  1009. ->method('bake');
  1010. $this->Task->Test->expects($this->exactly(7))
  1011. ->method('bake');
  1012. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  1013. $this->Task->expects($this->at(1))
  1014. ->method('createFile')
  1015. ->with($filename);
  1016. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  1017. $this->Task->expects($this->at(3))
  1018. ->method('createFile')
  1019. ->with($filename);
  1020. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  1021. $this->Task->expects($this->at(5))
  1022. ->method('createFile')
  1023. ->with($filename);
  1024. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  1025. $this->Task->expects($this->at(7))
  1026. ->method('createFile')
  1027. ->with($filename);
  1028. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  1029. $this->Task->expects($this->at(9))
  1030. ->method('createFile')
  1031. ->with($filename);
  1032. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  1033. $this->Task->expects($this->at(11))
  1034. ->method('createFile')
  1035. ->with($filename);
  1036. $this->Task->all();
  1037. }
  1038. }