ModelTaskTest.php 24 KB

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