ModelTaskTest.php 25 KB

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