ModelTaskTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. '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. TableRegistry::clear();
  518. TableRegistry::get('Users', [
  519. 'table' => 'counter_cache_users'
  520. ]);
  521. $model = TableRegistry::get('Posts', [
  522. 'table' => 'counter_cache_posts'
  523. ]);
  524. $result = $this->Task->getBehaviors($model);
  525. $expected = [
  526. 'CounterCache' => ["'Users' => ['post_count']"]
  527. ];
  528. $this->assertEquals($expected, $result);
  529. }
  530. /**
  531. * Test getDisplayField() method.
  532. *
  533. * @return void
  534. */
  535. public function testGetDisplayField() {
  536. $model = TableRegistry::get('BakeArticles');
  537. $result = $this->Task->getDisplayField($model);
  538. $this->assertEquals('title', $result);
  539. $this->Task->params['display-field'] = 'custom';
  540. $result = $this->Task->getDisplayField($model);
  541. $this->assertEquals('custom', $result);
  542. }
  543. /**
  544. * Ensure that the fixture object is correctly called.
  545. *
  546. * @return void
  547. */
  548. public function testBakeFixture() {
  549. $this->Task->plugin = 'TestPlugin';
  550. $this->Task->Fixture->expects($this->at(0))
  551. ->method('bake')
  552. ->with('BakeArticle', 'bake_articles');
  553. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  554. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  555. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  556. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  557. }
  558. /**
  559. * Ensure that the fixture baking can be disabled
  560. *
  561. * @return void
  562. */
  563. public function testBakeFixtureDisabled() {
  564. $this->Task->params['no-fixture'] = true;
  565. $this->Task->plugin = 'TestPlugin';
  566. $this->Task->Fixture->expects($this->never())
  567. ->method('bake');
  568. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  569. }
  570. /**
  571. * Ensure that the test object is correctly called.
  572. *
  573. * @return void
  574. */
  575. public function testBakeTest() {
  576. $this->Task->plugin = 'TestPlugin';
  577. $this->Task->Test->expects($this->at(0))
  578. ->method('bake')
  579. ->with('Table', 'BakeArticle');
  580. $this->Task->bakeTest('BakeArticle');
  581. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  582. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  583. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  584. }
  585. /**
  586. * Ensure that test baking can be disabled.
  587. *
  588. * @return void
  589. */
  590. public function testBakeTestDisabled() {
  591. $this->Task->params['no-test'] = true;
  592. $this->Task->plugin = 'TestPlugin';
  593. $this->Task->Test->expects($this->never())
  594. ->method('bake');
  595. $this->Task->bakeTest('BakeArticle');
  596. }
  597. /**
  598. * test baking validation
  599. *
  600. * @return void
  601. */
  602. public function testBakeTableValidation() {
  603. $validation = [
  604. 'id' => [
  605. 'valid' => array(
  606. 'allowEmpty' => 'create',
  607. 'rule' => 'numeric',
  608. )
  609. ],
  610. 'name' => [
  611. 'valid' => [
  612. 'allowEmpty' => false,
  613. 'rule' => false,
  614. ]
  615. ],
  616. 'email' => [
  617. 'valid' => [
  618. 'allowEmpty' => true,
  619. 'rule' => 'email'
  620. ],
  621. 'unique' => [
  622. 'rule' => 'validateUnique',
  623. 'provider' => 'table'
  624. ]
  625. ]
  626. ];
  627. $model = TableRegistry::get('BakeArticles');
  628. $result = $this->Task->bakeTable($model, compact('validation'));
  629. $this->assertContains('namespace App\Model\Table;', $result);
  630. $this->assertContains('use Cake\ORM\Table;', $result);
  631. $this->assertContains('use Cake\Validation\Validator;', $result);
  632. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  633. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  634. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  635. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  636. $this->assertContains(
  637. "->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
  638. $result)
  639. ;
  640. $this->assertContains("->allowEmpty('id', 'create')", $result);
  641. $this->assertContains("->allowEmpty('email')", $result);
  642. $this->assertContains("->validatePresence('name', 'create')", $result);
  643. }
  644. /**
  645. * test baking
  646. *
  647. * @return void
  648. */
  649. public function testBakeTableConfig() {
  650. $config = [
  651. 'table' => 'articles',
  652. 'primaryKey' => ['id'],
  653. 'displayField' => 'title',
  654. 'behaviors' => ['Timestamp' => ''],
  655. ];
  656. $model = TableRegistry::get('BakeArticles');
  657. $result = $this->Task->bakeTable($model, $config);
  658. $this->assertContains('public function initialize(array $config) {', $result);
  659. $this->assertContains("this->primaryKey(['id']);\n", $result);
  660. $this->assertContains("this->displayField('title');\n", $result);
  661. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  662. $this->assertContains("this->table('articles');\n", $result);
  663. $this->assertContains('use Cake\Validation\Validator;', $result);
  664. }
  665. /**
  666. * test baking relations
  667. *
  668. * @return void
  669. */
  670. public function testBakeTableRelations() {
  671. $associations = [
  672. 'belongsTo' => [
  673. [
  674. 'alias' => 'SomethingElse',
  675. 'foreignKey' => 'something_else_id',
  676. ],
  677. [
  678. 'alias' => 'BakeUser',
  679. 'foreignKey' => 'bake_user_id',
  680. ],
  681. ],
  682. 'hasMany' => [
  683. [
  684. 'alias' => 'BakeComment',
  685. 'foreignKey' => 'parent_id',
  686. ],
  687. ],
  688. 'belongsToMany' => [
  689. [
  690. 'alias' => 'BakeTag',
  691. 'foreignKey' => 'bake_article_id',
  692. 'joinTable' => 'bake_articles_bake_tags',
  693. 'targetForeignKey' => 'bake_tag_id',
  694. ],
  695. ]
  696. ];
  697. $model = TableRegistry::get('BakeArticles');
  698. $result = $this->Task->bakeTable($model, compact('associations'));
  699. $this->assertContains("this->hasMany('BakeComment', [", $result);
  700. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  701. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  702. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  703. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  704. }
  705. /**
  706. * test baking an entity class
  707. *
  708. * @return void
  709. */
  710. public function testBakeEntity() {
  711. $config = [
  712. 'fields' => []
  713. ];
  714. $model = TableRegistry::get('BakeArticles');
  715. $result = $this->Task->bakeEntity($model, $config);
  716. $this->assertContains('namespace App\Model\Entity;', $result);
  717. $this->assertContains('use Cake\ORM\Entity;', $result);
  718. $this->assertContains('class BakeArticle extends Entity {', $result);
  719. $this->assertNotContains('$_accessible', $result);
  720. }
  721. /**
  722. * test baking an entity class
  723. *
  724. * @return void
  725. */
  726. public function testBakeEntityFields() {
  727. $config = [
  728. 'fields' => ['title', 'body', 'published']
  729. ];
  730. $model = TableRegistry::get('BakeArticles');
  731. $result = $this->Task->bakeEntity($model, $config);
  732. $this->assertContains("protected \$_accessible = [", $result);
  733. $this->assertContains("'title' => true,", $result);
  734. $this->assertContains("'body' => true,", $result);
  735. $this->assertContains("'published' => true", $result);
  736. $this->assertNotContains("protected \$_hidden", $result);
  737. }
  738. /**
  739. * test baking an entity class sets hidden fields.
  740. *
  741. * @return void
  742. */
  743. public function testBakeEntityHidden() {
  744. $model = TableRegistry::get('BakeUsers');
  745. $config = [
  746. 'hidden' => ['password'],
  747. ];
  748. $result = $this->Task->bakeEntity($model, $config);
  749. $this->assertContains("protected \$_hidden = [", $result);
  750. $this->assertContains("'password'", $result);
  751. $this->assertNotContains("protected \$_accessible", $result);
  752. }
  753. /**
  754. * test bake() with a -plugin param
  755. *
  756. * @return void
  757. */
  758. public function testBakeTableWithPlugin() {
  759. $this->Task->plugin = 'ControllerTest';
  760. // fake plugin path
  761. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  762. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
  763. $this->Task->expects($this->once())->method('createFile')
  764. ->with($path, $this->logicalAnd(
  765. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  766. $this->stringContains('use Cake\\ORM\\Table;'),
  767. $this->stringContains('class BakeArticlesTable extends Table {')
  768. ));
  769. $model = TableRegistry::get('BakeArticles');
  770. $this->Task->bakeTable($model);
  771. }
  772. /**
  773. * test bake() with a -plugin param
  774. *
  775. * @return void
  776. */
  777. public function testBakeEntityWithPlugin() {
  778. $this->Task->plugin = 'ControllerTest';
  779. // fake plugin path
  780. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  781. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  782. $path = $this->_normalizePath($path);
  783. $this->Task->expects($this->once())->method('createFile')
  784. ->with($path, $this->logicalAnd(
  785. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  786. $this->stringContains('use Cake\\ORM\\Entity;'),
  787. $this->stringContains('class BakeArticle extends Entity {')
  788. ));
  789. $model = TableRegistry::get('BakeArticles');
  790. $this->Task->bakeEntity($model);
  791. }
  792. /**
  793. * test that execute with no args
  794. *
  795. * @return void
  796. */
  797. public function testMainNoArgs() {
  798. $this->_useMockedOut();
  799. $this->Task->connection = 'test';
  800. $this->Task->path = '/my/path/';
  801. $this->Task->expects($this->at(0))
  802. ->method('out')
  803. ->with($this->stringContains('Choose a model to bake from the following:'));
  804. $this->Task->main();
  805. }
  806. /**
  807. * test that execute passes runs bake depending with named model.
  808. *
  809. * @return void
  810. */
  811. public function testMainWithNamedModel() {
  812. $this->Task->connection = 'test';
  813. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  814. $this->Task->expects($this->at(0))
  815. ->method('createFile')
  816. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  817. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  818. $this->Task->expects($this->at(1))
  819. ->method('createFile')
  820. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  821. $this->Task->main('BakeArticles');
  822. }
  823. /**
  824. * data provider for testMainWithNamedModelVariations
  825. *
  826. * @return void
  827. */
  828. public static function nameVariations() {
  829. return array(
  830. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  831. );
  832. }
  833. /**
  834. * test that execute passes with different inflections of the same name.
  835. *
  836. * @dataProvider nameVariations
  837. * @return void
  838. */
  839. public function testMainWithNamedModelVariations($name) {
  840. $this->Task->connection = 'test';
  841. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  842. $this->Task->expects($this->at(0))
  843. ->method('createFile')
  844. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  845. $this->Task->main($name);
  846. }
  847. /**
  848. * test that execute runs all() when args[0] = all
  849. *
  850. * @return void
  851. */
  852. public function testMainIntoAll() {
  853. $count = count($this->Task->listAll());
  854. if ($count != count($this->fixtures)) {
  855. $this->markTestSkipped('Additional tables detected.');
  856. }
  857. $this->Task->connection = 'test';
  858. $this->Task->Fixture->expects($this->exactly($count))
  859. ->method('bake');
  860. $this->Task->Test->expects($this->exactly($count))
  861. ->method('bake');
  862. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  863. $this->Task->expects($this->at(0))
  864. ->method('createFile')
  865. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  866. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  867. $this->Task->expects($this->at(1))
  868. ->method('createFile')
  869. ->with($filename, $this->stringContains('class BakeArticle extends'));
  870. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  871. $this->Task->expects($this->at(2))
  872. ->method('createFile')
  873. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  874. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  875. $this->Task->expects($this->at(3))
  876. ->method('createFile')
  877. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  878. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  879. $this->Task->expects($this->at(4))
  880. ->method('createFile')
  881. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  882. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  883. $this->Task->expects($this->at(5))
  884. ->method('createFile')
  885. ->with($filename, $this->stringContains('class BakeComment extends'));
  886. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  887. $this->Task->expects($this->at(6))
  888. ->method('createFile')
  889. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  890. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  891. $this->Task->expects($this->at(7))
  892. ->method('createFile')
  893. ->with($filename, $this->stringContains('class BakeTag extends'));
  894. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  895. $this->Task->expects($this->at(8))
  896. ->method('createFile')
  897. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  898. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  899. $this->Task->expects($this->at(9))
  900. ->method('createFile')
  901. ->with($filename, $this->stringContains('class CategoryThread extends'));
  902. $this->Task->all();
  903. }
  904. /**
  905. * test that skipTables changes how all() works.
  906. *
  907. * @return void
  908. */
  909. public function testSkipTablesAndAll() {
  910. $count = count($this->Task->listAll('test'));
  911. if ($count != count($this->fixtures)) {
  912. $this->markTestSkipped('Additional tables detected.');
  913. }
  914. $this->Task->connection = 'test';
  915. $this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
  916. $this->Task->Fixture->expects($this->exactly(7))
  917. ->method('bake');
  918. $this->Task->Test->expects($this->exactly(7))
  919. ->method('bake');
  920. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  921. $this->Task->expects($this->at(1))
  922. ->method('createFile')
  923. ->with($filename);
  924. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  925. $this->Task->expects($this->at(3))
  926. ->method('createFile')
  927. ->with($filename);
  928. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  929. $this->Task->expects($this->at(5))
  930. ->method('createFile')
  931. ->with($filename);
  932. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  933. $this->Task->expects($this->at(7))
  934. ->method('createFile')
  935. ->with($filename);
  936. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  937. $this->Task->expects($this->at(9))
  938. ->method('createFile')
  939. ->with($filename);
  940. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  941. $this->Task->expects($this->at(11))
  942. ->method('createFile')
  943. ->with($filename);
  944. $this->Task->all();
  945. }
  946. }