ModelTaskTest.php 24 KB

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