ModelTaskTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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' => ['rule' => 'numeric', 'allowEmpty' => false],
  392. 'title' => ['rule' => false, 'allowEmpty' => false],
  393. 'body' => ['rule' => false, 'allowEmpty' => true],
  394. 'published' => ['rule' => 'boolean', 'allowEmpty' => true],
  395. ];
  396. $this->assertEquals($expected, $result);
  397. $model = TableRegistry::get('BakeComments');
  398. $result = $this->Task->getValidation($model);
  399. $expected = [
  400. 'bake_article_id' => ['rule' => 'numeric', 'allowEmpty' => false],
  401. 'bake_user_id' => ['rule' => 'numeric', 'allowEmpty' => false],
  402. 'comment' => ['rule' => false, 'allowEmpty' => true],
  403. 'published' => ['rule' => false, 'allowEmpty' => true]
  404. ];
  405. $this->assertEquals($expected, $result);
  406. }
  407. /**
  408. * test non interactive doActsAs
  409. *
  410. * @return void
  411. */
  412. public function testGetBehaviors() {
  413. $model = TableRegistry::get('NumberTrees');
  414. $result = $this->Task->getBehaviors($model);
  415. $this->assertEquals(['Tree' => []], $result);
  416. $model = TableRegistry::get('BakeArticles');
  417. $result = $this->Task->getBehaviors($model);
  418. $this->assertEquals(['Timestamp' => []], $result);
  419. $model = TableRegistry::get('CounterCacheUsers');
  420. $result = $this->Task->getBehaviors($model);
  421. $expected = [
  422. 'CounterCache' => ["'Posts' => ['post_count']"]
  423. ];
  424. $this->assertEquals($expected, $result);
  425. }
  426. /**
  427. * Test getDisplayField() method.
  428. *
  429. * @return void
  430. */
  431. public function testGetDisplayField() {
  432. $model = TableRegistry::get('BakeArticles');
  433. $result = $this->Task->getDisplayField($model);
  434. $this->assertEquals('title', $result);
  435. $this->Task->params['display-field'] = 'custom';
  436. $result = $this->Task->getDisplayField($model);
  437. $this->assertEquals('custom', $result);
  438. }
  439. /**
  440. * Ensure that the fixture object is correctly called.
  441. *
  442. * @return void
  443. */
  444. public function testBakeFixture() {
  445. $this->Task->plugin = 'TestPlugin';
  446. $this->Task->Fixture->expects($this->at(0))
  447. ->method('bake')
  448. ->with('BakeArticle', 'bake_articles');
  449. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  450. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  451. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  452. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  453. }
  454. /**
  455. * Ensure that the fixture baking can be disabled
  456. *
  457. * @return void
  458. */
  459. public function testBakeFixtureDisabled() {
  460. $this->Task->params['no-fixture'] = true;
  461. $this->Task->plugin = 'TestPlugin';
  462. $this->Task->Fixture->expects($this->never())
  463. ->method('bake');
  464. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  465. }
  466. /**
  467. * Ensure that the test object is correctly called.
  468. *
  469. * @return void
  470. */
  471. public function testBakeTest() {
  472. $this->Task->plugin = 'TestPlugin';
  473. $this->Task->Test->expects($this->at(0))
  474. ->method('bake')
  475. ->with('Table', 'BakeArticle');
  476. $this->Task->bakeTest('BakeArticle');
  477. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  478. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  479. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  480. }
  481. /**
  482. * Ensure that test baking can be disabled.
  483. *
  484. * @return void
  485. */
  486. public function testBakeTestDisabled() {
  487. $this->Task->params['no-test'] = true;
  488. $this->Task->plugin = 'TestPlugin';
  489. $this->Task->Test->expects($this->never())
  490. ->method('bake');
  491. $this->Task->bakeTest('BakeArticle');
  492. }
  493. /**
  494. * test baking validation
  495. *
  496. * @return void
  497. */
  498. public function testBakeTableValidation() {
  499. $validation = array(
  500. 'id' => array(
  501. 'allowEmpty' => 'create',
  502. 'rule' => 'numeric',
  503. ),
  504. 'name' => array(
  505. 'allowEmpty' => false,
  506. 'rule' => false,
  507. ),
  508. 'email' => array(
  509. 'allowEmpty' => true,
  510. 'rule' => 'email',
  511. ),
  512. );
  513. $model = TableRegistry::get('BakeArticles');
  514. $result = $this->Task->bakeTable($model, compact('validation'));
  515. $this->assertContains('namespace App\Model\Table;', $result);
  516. $this->assertContains('use Cake\ORM\Table;', $result);
  517. $this->assertContains('use Cake\Validation\Validator;', $result);
  518. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  519. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  520. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  521. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  522. $this->assertContains("->allowEmpty('id', 'create')", $result);
  523. $this->assertContains("->allowEmpty('email')", $result);
  524. $this->assertContains("->validatePresence('name', 'create')", $result);
  525. }
  526. /**
  527. * test baking
  528. *
  529. * @return void
  530. */
  531. public function testBakeTableConfig() {
  532. $config = [
  533. 'table' => 'articles',
  534. 'primaryKey' => ['id'],
  535. 'displayField' => 'title',
  536. 'behaviors' => ['Timestamp' => ''],
  537. ];
  538. $model = TableRegistry::get('BakeArticles');
  539. $result = $this->Task->bakeTable($model, $config);
  540. $this->assertContains('public function initialize(array $config) {', $result);
  541. $this->assertContains("this->primaryKey(['id']);\n", $result);
  542. $this->assertContains("this->displayField('title');\n", $result);
  543. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  544. $this->assertContains("this->table('articles');\n", $result);
  545. $this->assertContains('use Cake\Validation\Validator;', $result);
  546. }
  547. /**
  548. * test baking relations
  549. *
  550. * @return void
  551. */
  552. public function testBakeTableRelations() {
  553. $associations = [
  554. 'belongsTo' => [
  555. [
  556. 'alias' => 'SomethingElse',
  557. 'foreignKey' => 'something_else_id',
  558. ],
  559. [
  560. 'alias' => 'BakeUser',
  561. 'foreignKey' => 'bake_user_id',
  562. ],
  563. ],
  564. 'hasMany' => [
  565. [
  566. 'alias' => 'BakeComment',
  567. 'foreignKey' => 'parent_id',
  568. ],
  569. ],
  570. 'belongsToMany' => [
  571. [
  572. 'alias' => 'BakeTag',
  573. 'foreignKey' => 'bake_article_id',
  574. 'joinTable' => 'bake_articles_bake_tags',
  575. 'targetForeignKey' => 'bake_tag_id',
  576. ],
  577. ]
  578. ];
  579. $model = TableRegistry::get('BakeArticles');
  580. $result = $this->Task->bakeTable($model, compact('associations'));
  581. $this->assertContains("this->hasMany('BakeComment', [", $result);
  582. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  583. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  584. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  585. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  586. }
  587. /**
  588. * test baking an entity class
  589. *
  590. * @return void
  591. */
  592. public function testBakeEntity() {
  593. $config = [
  594. 'fields' => []
  595. ];
  596. $model = TableRegistry::get('BakeArticles');
  597. $result = $this->Task->bakeEntity($model, $config);
  598. $this->assertContains('namespace App\Model\Entity;', $result);
  599. $this->assertContains('use Cake\ORM\Entity;', $result);
  600. $this->assertContains('class BakeArticle extends Entity {', $result);
  601. $this->assertNotContains('$_accessible', $result);
  602. }
  603. /**
  604. * test baking an entity class
  605. *
  606. * @return void
  607. */
  608. public function testBakeEntityFields() {
  609. $config = [
  610. 'fields' => ['title', 'body', 'published']
  611. ];
  612. $model = TableRegistry::get('BakeArticles');
  613. $result = $this->Task->bakeEntity($model, $config);
  614. $this->assertContains("protected \$_accessible = [", $result);
  615. $this->assertContains("'title' => true,", $result);
  616. $this->assertContains("'body' => true,", $result);
  617. $this->assertContains("'published' => true", $result);
  618. $this->assertNotContains("protected \$_hidden", $result);
  619. }
  620. /**
  621. * test baking an entity class sets hidden fields.
  622. *
  623. * @return void
  624. */
  625. public function testBakeEntityHidden() {
  626. $model = TableRegistry::get('BakeUsers');
  627. $config = [
  628. 'hidden' => ['password'],
  629. ];
  630. $result = $this->Task->bakeEntity($model, $config);
  631. $this->assertContains("protected \$_hidden = [", $result);
  632. $this->assertContains("'password'", $result);
  633. $this->assertNotContains("protected \$_accessible", $result);
  634. }
  635. /**
  636. * test bake() with a -plugin param
  637. *
  638. * @return void
  639. */
  640. public function testBakeTableWithPlugin() {
  641. $this->Task->plugin = 'ControllerTest';
  642. // fake plugin path
  643. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  644. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/Model/Table/BakeArticlesTable.php');
  645. $this->Task->expects($this->once())->method('createFile')
  646. ->with($path, $this->logicalAnd(
  647. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  648. $this->stringContains('use Cake\\ORM\\Table;'),
  649. $this->stringContains('class BakeArticlesTable extends Table {')
  650. ));
  651. $model = TableRegistry::get('BakeArticles');
  652. $this->Task->bakeTable($model);
  653. }
  654. /**
  655. * test bake() with a -plugin param
  656. *
  657. * @return void
  658. */
  659. public function testBakeEntityWithPlugin() {
  660. $this->Task->plugin = 'ControllerTest';
  661. // fake plugin path
  662. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  663. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  664. $path = $this->_normalizePath($path);
  665. $this->Task->expects($this->once())->method('createFile')
  666. ->with($path, $this->logicalAnd(
  667. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  668. $this->stringContains('use Cake\\ORM\\Entity;'),
  669. $this->stringContains('class BakeArticle extends Entity {')
  670. ));
  671. $model = TableRegistry::get('BakeArticles');
  672. $this->Task->bakeEntity($model);
  673. }
  674. /**
  675. * test that execute with no args
  676. *
  677. * @return void
  678. */
  679. public function testMainNoArgs() {
  680. $this->_useMockedOut();
  681. $this->Task->connection = 'test';
  682. $this->Task->path = '/my/path/';
  683. $this->Task->expects($this->at(0))
  684. ->method('out')
  685. ->with($this->stringContains('Choose a model to bake from the following:'));
  686. $this->Task->main();
  687. }
  688. /**
  689. * test that execute passes runs bake depending with named model.
  690. *
  691. * @return void
  692. */
  693. public function testMainWithNamedModel() {
  694. $this->Task->connection = 'test';
  695. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  696. $this->Task->expects($this->at(0))
  697. ->method('createFile')
  698. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  699. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  700. $this->Task->expects($this->at(1))
  701. ->method('createFile')
  702. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  703. $this->Task->main('BakeArticles');
  704. }
  705. /**
  706. * data provider for testMainWithNamedModelVariations
  707. *
  708. * @return void
  709. */
  710. public static function nameVariations() {
  711. return array(
  712. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  713. );
  714. }
  715. /**
  716. * test that execute passes with different inflections of the same name.
  717. *
  718. * @dataProvider nameVariations
  719. * @return void
  720. */
  721. public function testMainWithNamedModelVariations($name) {
  722. $this->Task->connection = 'test';
  723. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  724. $this->Task->expects($this->at(0))
  725. ->method('createFile')
  726. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  727. $this->Task->main($name);
  728. }
  729. /**
  730. * test that execute runs all() when args[0] = all
  731. *
  732. * @return void
  733. */
  734. public function testMainIntoAll() {
  735. $count = count($this->Task->listAll());
  736. if ($count != count($this->fixtures)) {
  737. $this->markTestSkipped('Additional tables detected.');
  738. }
  739. $this->Task->connection = 'test';
  740. $this->Task->Fixture->expects($this->exactly($count))
  741. ->method('bake');
  742. $this->Task->Test->expects($this->exactly($count))
  743. ->method('bake');
  744. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  745. $this->Task->expects($this->at(0))
  746. ->method('createFile')
  747. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  748. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  749. $this->Task->expects($this->at(1))
  750. ->method('createFile')
  751. ->with($filename, $this->stringContains('class BakeArticle extends'));
  752. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  753. $this->Task->expects($this->at(2))
  754. ->method('createFile')
  755. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  756. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  757. $this->Task->expects($this->at(3))
  758. ->method('createFile')
  759. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  760. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  761. $this->Task->expects($this->at(4))
  762. ->method('createFile')
  763. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  764. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  765. $this->Task->expects($this->at(5))
  766. ->method('createFile')
  767. ->with($filename, $this->stringContains('class BakeComment extends'));
  768. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  769. $this->Task->expects($this->at(6))
  770. ->method('createFile')
  771. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  772. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  773. $this->Task->expects($this->at(7))
  774. ->method('createFile')
  775. ->with($filename, $this->stringContains('class BakeTag extends'));
  776. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  777. $this->Task->expects($this->at(8))
  778. ->method('createFile')
  779. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  780. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  781. $this->Task->expects($this->at(9))
  782. ->method('createFile')
  783. ->with($filename, $this->stringContains('class CategoryThread extends'));
  784. $this->Task->all();
  785. }
  786. /**
  787. * test that skipTables changes how all() works.
  788. *
  789. * @return void
  790. */
  791. public function testSkipTablesAndAll() {
  792. $count = count($this->Task->listAll('test'));
  793. if ($count != count($this->fixtures)) {
  794. $this->markTestSkipped('Additional tables detected.');
  795. }
  796. $this->Task->connection = 'test';
  797. $this->Task->skipTables = ['bake_tags'];
  798. $this->Task->Fixture->expects($this->exactly(7))
  799. ->method('bake');
  800. $this->Task->Test->expects($this->exactly(7))
  801. ->method('bake');
  802. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  803. $this->Task->expects($this->at(1))
  804. ->method('createFile')
  805. ->with($filename);
  806. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  807. $this->Task->expects($this->at(3))
  808. ->method('createFile')
  809. ->with($filename);
  810. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  811. $this->Task->expects($this->at(5))
  812. ->method('createFile')
  813. ->with($filename);
  814. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  815. $this->Task->expects($this->at(7))
  816. ->method('createFile')
  817. ->with($filename);
  818. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  819. $this->Task->expects($this->at(9))
  820. ->method('createFile')
  821. ->with($filename);
  822. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  823. $this->Task->expects($this->at(11))
  824. ->method('createFile')
  825. ->with($filename);
  826. $this->Task->all();
  827. }
  828. }