ModelTaskTest.php 29 KB

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