ModelTaskTest.php 24 KB

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