ModelTaskTest.php 24 KB

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