ModelTaskTest.php 24 KB

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