ModelTaskTest.php 26 KB

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