ModelTaskTest.php 25 KB

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