ModelTaskTest.php 23 KB

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