ModelTaskTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. $this->assertContains("->allowEmpty('id', 'create')", $result);
  666. $this->assertContains("->allowEmpty('email')", $result);
  667. $this->assertContains("->validatePresence('name', 'create')", $result);
  668. }
  669. /**
  670. * test baking
  671. *
  672. * @return void
  673. */
  674. public function testBakeTableConfig() {
  675. $config = [
  676. 'table' => 'articles',
  677. 'primaryKey' => ['id'],
  678. 'displayField' => 'title',
  679. 'behaviors' => ['Timestamp' => ''],
  680. ];
  681. $model = TableRegistry::get('BakeArticles');
  682. $result = $this->Task->bakeTable($model, $config);
  683. $this->assertContains('public function initialize(array $config) {', $result);
  684. $this->assertContains("this->primaryKey(['id']);\n", $result);
  685. $this->assertContains("this->displayField('title');\n", $result);
  686. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  687. $this->assertContains("this->table('articles');\n", $result);
  688. $this->assertContains('use Cake\Validation\Validator;', $result);
  689. }
  690. /**
  691. * test baking relations
  692. *
  693. * @return void
  694. */
  695. public function testBakeTableRelations() {
  696. $associations = [
  697. 'belongsTo' => [
  698. [
  699. 'alias' => 'SomethingElse',
  700. 'foreignKey' => 'something_else_id',
  701. ],
  702. [
  703. 'alias' => 'BakeUser',
  704. 'foreignKey' => 'bake_user_id',
  705. ],
  706. ],
  707. 'hasMany' => [
  708. [
  709. 'alias' => 'BakeComment',
  710. 'foreignKey' => 'parent_id',
  711. ],
  712. ],
  713. 'belongsToMany' => [
  714. [
  715. 'alias' => 'BakeTag',
  716. 'foreignKey' => 'bake_article_id',
  717. 'joinTable' => 'bake_articles_bake_tags',
  718. 'targetForeignKey' => 'bake_tag_id',
  719. ],
  720. ]
  721. ];
  722. $model = TableRegistry::get('BakeArticles');
  723. $result = $this->Task->bakeTable($model, compact('associations'));
  724. $this->assertContains("this->hasMany('BakeComment', [", $result);
  725. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  726. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  727. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  728. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  729. }
  730. /**
  731. * test baking an entity class
  732. *
  733. * @return void
  734. */
  735. public function testBakeEntity() {
  736. $config = [
  737. 'fields' => []
  738. ];
  739. $model = TableRegistry::get('BakeArticles');
  740. $result = $this->Task->bakeEntity($model, $config);
  741. $this->assertContains('namespace App\Model\Entity;', $result);
  742. $this->assertContains('use Cake\ORM\Entity;', $result);
  743. $this->assertContains('class BakeArticle extends Entity {', $result);
  744. $this->assertNotContains('$_accessible', $result);
  745. }
  746. /**
  747. * test baking an entity class
  748. *
  749. * @return void
  750. */
  751. public function testBakeEntityFields() {
  752. $config = [
  753. 'fields' => ['title', 'body', 'published']
  754. ];
  755. $model = TableRegistry::get('BakeArticles');
  756. $result = $this->Task->bakeEntity($model, $config);
  757. $this->assertContains("protected \$_accessible = [", $result);
  758. $this->assertContains("'title' => true,", $result);
  759. $this->assertContains("'body' => true,", $result);
  760. $this->assertContains("'published' => true", $result);
  761. $this->assertNotContains("protected \$_hidden", $result);
  762. }
  763. /**
  764. * test baking an entity class sets hidden fields.
  765. *
  766. * @return void
  767. */
  768. public function testBakeEntityHidden() {
  769. $model = TableRegistry::get('BakeUsers');
  770. $config = [
  771. 'hidden' => ['password'],
  772. ];
  773. $result = $this->Task->bakeEntity($model, $config);
  774. $this->assertContains("protected \$_hidden = [", $result);
  775. $this->assertContains("'password'", $result);
  776. $this->assertNotContains("protected \$_accessible", $result);
  777. }
  778. /**
  779. * test bake() with a -plugin param
  780. *
  781. * @return void
  782. */
  783. public function testBakeTableWithPlugin() {
  784. $this->Task->plugin = 'ControllerTest';
  785. // fake plugin path
  786. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  787. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
  788. $this->Task->expects($this->once())->method('createFile')
  789. ->with($path, $this->logicalAnd(
  790. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  791. $this->stringContains('use Cake\\ORM\\Table;'),
  792. $this->stringContains('class BakeArticlesTable extends Table {')
  793. ));
  794. $model = TableRegistry::get('BakeArticles');
  795. $this->Task->bakeTable($model);
  796. }
  797. /**
  798. * test bake() with a -plugin param
  799. *
  800. * @return void
  801. */
  802. public function testBakeEntityWithPlugin() {
  803. $this->Task->plugin = 'ControllerTest';
  804. // fake plugin path
  805. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  806. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  807. $path = $this->_normalizePath($path);
  808. $this->Task->expects($this->once())->method('createFile')
  809. ->with($path, $this->logicalAnd(
  810. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  811. $this->stringContains('use Cake\\ORM\\Entity;'),
  812. $this->stringContains('class BakeArticle extends Entity {')
  813. ));
  814. $model = TableRegistry::get('BakeArticles');
  815. $this->Task->bakeEntity($model);
  816. }
  817. /**
  818. * test that execute with no args
  819. *
  820. * @return void
  821. */
  822. public function testMainNoArgs() {
  823. $this->_useMockedOut();
  824. $this->Task->connection = 'test';
  825. $this->Task->path = '/my/path/';
  826. $this->Task->expects($this->at(0))
  827. ->method('out')
  828. ->with($this->stringContains('Choose a model to bake from the following:'));
  829. $this->Task->main();
  830. }
  831. /**
  832. * test that execute passes runs bake depending with named model.
  833. *
  834. * @return void
  835. */
  836. public function testMainWithNamedModel() {
  837. $this->Task->connection = 'test';
  838. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  839. $this->Task->expects($this->at(0))
  840. ->method('createFile')
  841. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  842. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  843. $this->Task->expects($this->at(1))
  844. ->method('createFile')
  845. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  846. $this->Task->main('BakeArticles');
  847. }
  848. /**
  849. * data provider for testMainWithNamedModelVariations
  850. *
  851. * @return void
  852. */
  853. public static function nameVariations() {
  854. return array(
  855. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  856. );
  857. }
  858. /**
  859. * test that execute passes with different inflections of the same name.
  860. *
  861. * @dataProvider nameVariations
  862. * @return void
  863. */
  864. public function testMainWithNamedModelVariations($name) {
  865. $this->Task->connection = 'test';
  866. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  867. $this->Task->expects($this->at(0))
  868. ->method('createFile')
  869. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  870. $this->Task->main($name);
  871. }
  872. /**
  873. * test that execute runs all() when args[0] = all
  874. *
  875. * @return void
  876. */
  877. public function testMainIntoAll() {
  878. $count = count($this->Task->listAll());
  879. if ($count != count($this->fixtures)) {
  880. $this->markTestSkipped('Additional tables detected.');
  881. }
  882. $this->Task->connection = 'test';
  883. $this->Task->Fixture->expects($this->exactly($count))
  884. ->method('bake');
  885. $this->Task->Test->expects($this->exactly($count))
  886. ->method('bake');
  887. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  888. $this->Task->expects($this->at(0))
  889. ->method('createFile')
  890. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  891. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  892. $this->Task->expects($this->at(1))
  893. ->method('createFile')
  894. ->with($filename, $this->stringContains('class BakeArticle extends'));
  895. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  896. $this->Task->expects($this->at(2))
  897. ->method('createFile')
  898. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  899. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  900. $this->Task->expects($this->at(3))
  901. ->method('createFile')
  902. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  903. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  904. $this->Task->expects($this->at(4))
  905. ->method('createFile')
  906. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  907. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  908. $this->Task->expects($this->at(5))
  909. ->method('createFile')
  910. ->with($filename, $this->stringContains('class BakeComment extends'));
  911. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  912. $this->Task->expects($this->at(6))
  913. ->method('createFile')
  914. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  915. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  916. $this->Task->expects($this->at(7))
  917. ->method('createFile')
  918. ->with($filename, $this->stringContains('class BakeTag extends'));
  919. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  920. $this->Task->expects($this->at(8))
  921. ->method('createFile')
  922. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  923. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  924. $this->Task->expects($this->at(9))
  925. ->method('createFile')
  926. ->with($filename, $this->stringContains('class CategoryThread extends'));
  927. $this->Task->all();
  928. }
  929. /**
  930. * test that skipTables changes how all() works.
  931. *
  932. * @return void
  933. */
  934. public function testSkipTablesAndAll() {
  935. $count = count($this->Task->listAll('test'));
  936. if ($count != count($this->fixtures)) {
  937. $this->markTestSkipped('Additional tables detected.');
  938. }
  939. $this->Task->connection = 'test';
  940. $this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
  941. $this->Task->Fixture->expects($this->exactly(7))
  942. ->method('bake');
  943. $this->Task->Test->expects($this->exactly(7))
  944. ->method('bake');
  945. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  946. $this->Task->expects($this->at(1))
  947. ->method('createFile')
  948. ->with($filename);
  949. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  950. $this->Task->expects($this->at(3))
  951. ->method('createFile')
  952. ->with($filename);
  953. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  954. $this->Task->expects($this->at(5))
  955. ->method('createFile')
  956. ->with($filename);
  957. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  958. $this->Task->expects($this->at(7))
  959. ->method('createFile')
  960. ->with($filename);
  961. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  962. $this->Task->expects($this->at(9))
  963. ->method('createFile')
  964. ->with($filename);
  965. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  966. $this->Task->expects($this->at(11))
  967. ->method('createFile')
  968. ->with($filename);
  969. $this->Task->all();
  970. }
  971. }