ModelTaskTest.php 25 KB

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