ModelTaskTest.php 28 KB

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