ModelTaskTest.php 28 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 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 testBakeWithPlugin() {
  534. $this->markTestIncomplete('Not done here yet');
  535. $this->Task->plugin = 'ControllerTest';
  536. //fake plugin path
  537. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  538. $path = APP . 'Plugin/ControllerTest/Model/BakeArticle.php';
  539. $this->Task->expects($this->once())->method('createFile')
  540. ->with($path, $this->stringContains('BakeArticle extends ControllerTestAppModel'));
  541. $result = $this->Task->bake('BakeArticle', array(), array());
  542. $this->assertContains("App::uses('ControllerTestAppModel', 'ControllerTest.Model');", $result);
  543. $this->assertEquals(count(ClassRegistry::keys()), 0);
  544. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  545. }
  546. /**
  547. * test that execute passes runs bake depending with named model.
  548. *
  549. * @return void
  550. */
  551. public function testExecuteWithNamedModel() {
  552. $this->markTestIncomplete('Not done here yet');
  553. $this->Task->connection = 'test';
  554. $this->Task->path = '/my/path/';
  555. $this->Task->args = array('BakeArticle');
  556. $filename = '/my/path/BakeArticle.php';
  557. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  558. $this->Task->expects($this->once())->method('createFile')
  559. ->with($filename, $this->stringContains('class BakeArticle extends AppModel'));
  560. $this->Task->execute();
  561. $this->assertEquals(count(ClassRegistry::keys()), 0);
  562. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  563. }
  564. /**
  565. * data provider for testExecuteWithNamedModelVariations
  566. *
  567. * @return void
  568. */
  569. public static function nameVariations() {
  570. return array(
  571. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  572. );
  573. }
  574. /**
  575. * test that execute passes with different inflections of the same name.
  576. *
  577. * @dataProvider nameVariations
  578. * @return void
  579. */
  580. public function testExecuteWithNamedModelVariations($name) {
  581. $this->markTestIncomplete('Not done here yet');
  582. $this->Task->connection = 'test';
  583. $this->Task->path = '/my/path/';
  584. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  585. $this->Task->args = array($name);
  586. $filename = '/my/path/BakeArticle.php';
  587. $this->Task->expects($this->at(0))->method('createFile')
  588. ->with($filename, $this->stringContains('class BakeArticle extends AppModel'));
  589. $this->Task->execute();
  590. }
  591. /**
  592. * test that execute with a model name picks up hasMany associations.
  593. *
  594. * @return void
  595. */
  596. public function testExecuteWithNamedModelHasManyCreated() {
  597. $this->markTestIncomplete('Not done here yet');
  598. $this->Task->connection = 'test';
  599. $this->Task->path = '/my/path/';
  600. $this->Task->args = array('BakeArticle');
  601. $filename = '/my/path/BakeArticle.php';
  602. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  603. $this->Task->expects($this->at(0))->method('createFile')
  604. ->with($filename, $this->stringContains("'BakeComment' => array("));
  605. $this->Task->execute();
  606. }
  607. /**
  608. * test that execute runs all() when args[0] = all
  609. *
  610. * @return void
  611. */
  612. public function testExecuteIntoAll() {
  613. $this->markTestIncomplete('Not done here yet');
  614. $count = count($this->Task->listAll('test'));
  615. if ($count != count($this->fixtures)) {
  616. $this->markTestSkipped('Additional tables detected.');
  617. }
  618. $this->Task->connection = 'test';
  619. $this->Task->path = '/my/path/';
  620. $this->Task->args = array('all');
  621. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  622. $this->Task->Fixture->expects($this->exactly(5))->method('bake');
  623. $this->Task->Test->expects($this->exactly(5))->method('bake');
  624. $filename = '/my/path/BakeArticle.php';
  625. $this->Task->expects($this->at(1))->method('createFile')
  626. ->with($filename, $this->stringContains('class BakeArticle'));
  627. $filename = '/my/path/BakeArticlesBakeTag.php';
  628. $this->Task->expects($this->at(2))->method('createFile')
  629. ->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
  630. $filename = '/my/path/BakeComment.php';
  631. $this->Task->expects($this->at(3))->method('createFile')
  632. ->with($filename, $this->stringContains('class BakeComment'));
  633. $filename = '/my/path/BakeComment.php';
  634. $this->Task->expects($this->at(3))->method('createFile')
  635. ->with($filename, $this->stringContains('public $primaryKey = \'otherid\';'));
  636. $filename = '/my/path/BakeTag.php';
  637. $this->Task->expects($this->at(4))->method('createFile')
  638. ->with($filename, $this->stringContains('class BakeTag'));
  639. $filename = '/my/path/BakeTag.php';
  640. $this->Task->expects($this->at(4))->method('createFile')
  641. ->with($filename, $this->logicalNot($this->stringContains('public $primaryKey')));
  642. $filename = '/my/path/CategoryThread.php';
  643. $this->Task->expects($this->at(5))->method('createFile')
  644. ->with($filename, $this->stringContains('class CategoryThread'));
  645. $this->Task->execute();
  646. $this->assertEquals(count(ClassRegistry::keys()), 0);
  647. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  648. }
  649. /**
  650. * test that odd tablenames aren't inflected back from modelname
  651. *
  652. * @return void
  653. */
  654. public function testExecuteIntoAllOddTables() {
  655. $this->markTestIncomplete('Not done here yet');
  656. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  657. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  658. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  659. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
  660. array($out, $out, $in)
  661. );
  662. $this->_setupOtherMocks();
  663. $this->Task->connection = 'test';
  664. $this->Task->path = '/my/path/';
  665. $this->Task->args = array('all');
  666. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  667. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
  668. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  669. $this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
  670. $this->Task->expects($this->at(3))->method('bake')->with($object, false)->will($this->returnValue(true));
  671. $this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
  672. $this->Task->execute();
  673. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  674. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  675. $this->Task = $this->getMock('ModelTask',
  676. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'doActsAs', 'createFile'),
  677. array($out, $out, $in)
  678. );
  679. $this->_setupOtherMocks();
  680. $this->Task->connection = 'test';
  681. $this->Task->path = '/my/path/';
  682. $this->Task->args = array('all');
  683. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  684. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
  685. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  686. $this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
  687. $this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
  688. $this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
  689. $this->Task->expects($this->once())->method('doActsAs')->will($this->returnValue(array()));
  690. $filename = '/my/path/BakeOdd.php';
  691. $this->Task->expects($this->once())->method('createFile')
  692. ->with($filename, $this->stringContains('class BakeOdd'));
  693. $filename = '/my/path/BakeOdd.php';
  694. $this->Task->expects($this->once())->method('createFile')
  695. ->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
  696. $this->Task->execute();
  697. }
  698. /**
  699. * test that odd tablenames aren't inflected back from modelname
  700. *
  701. * @return void
  702. */
  703. public function testExecuteIntoBakeOddTables() {
  704. $this->markTestIncomplete('Not done here yet');
  705. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  706. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  707. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  708. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
  709. array($out, $out, $in)
  710. );
  711. $this->_setupOtherMocks();
  712. $this->Task->connection = 'test';
  713. $this->Task->path = '/my/path/';
  714. $this->Task->args = array('BakeOdd');
  715. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  716. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  717. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  718. $this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
  719. $this->Task->expects($this->once())->method('bake')->with($object, false)->will($this->returnValue(true));
  720. $this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
  721. $this->Task->execute();
  722. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  723. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  724. $this->Task = $this->getMock('ModelTask',
  725. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'doActsAs', 'createFile'),
  726. array($out, $out, $in)
  727. );
  728. $this->_setupOtherMocks();
  729. $this->Task->connection = 'test';
  730. $this->Task->path = '/my/path/';
  731. $this->Task->args = array('BakeOdd');
  732. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  733. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  734. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  735. $this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
  736. $this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
  737. $this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
  738. $this->Task->expects($this->once())->method('doActsAs')->will($this->returnValue(array()));
  739. $filename = '/my/path/BakeOdd.php';
  740. $this->Task->expects($this->once())->method('createFile')
  741. ->with($filename, $this->stringContains('class BakeOdd'));
  742. $filename = '/my/path/BakeOdd.php';
  743. $this->Task->expects($this->once())->method('createFile')
  744. ->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
  745. $this->Task->execute();
  746. }
  747. /**
  748. * test that skipTables changes how all() works.
  749. *
  750. * @return void
  751. */
  752. public function testSkipTablesAndAll() {
  753. $this->markTestIncomplete('Not done here yet');
  754. $count = count($this->Task->listAll('test'));
  755. if ($count != count($this->fixtures)) {
  756. $this->markTestSkipped('Additional tables detected.');
  757. }
  758. $this->Task->connection = 'test';
  759. $this->Task->path = '/my/path/';
  760. $this->Task->args = array('all');
  761. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  762. $this->Task->skipTables = array('bake_tags');
  763. $this->Task->Fixture->expects($this->exactly(4))->method('bake');
  764. $this->Task->Test->expects($this->exactly(4))->method('bake');
  765. $filename = '/my/path/BakeArticle.php';
  766. $this->Task->expects($this->at(1))->method('createFile')
  767. ->with($filename, $this->stringContains('class BakeArticle'));
  768. $filename = '/my/path/BakeArticlesBakeTag.php';
  769. $this->Task->expects($this->at(2))->method('createFile')
  770. ->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
  771. $filename = '/my/path/BakeComment.php';
  772. $this->Task->expects($this->at(3))->method('createFile')
  773. ->with($filename, $this->stringContains('class BakeComment'));
  774. $filename = '/my/path/CategoryThread.php';
  775. $this->Task->expects($this->at(4))->method('createFile')
  776. ->with($filename, $this->stringContains('class CategoryThread'));
  777. $this->Task->execute();
  778. }
  779. /**
  780. * test using bake interactively with a table that does not exist.
  781. *
  782. * @return void
  783. */
  784. public function testExecuteWithNonExistantTableName() {
  785. $this->markTestIncomplete('Not done here yet');
  786. $this->Task->connection = 'test';
  787. $this->Task->path = '/my/path/';
  788. $this->Task->expects($this->any())->method('in')
  789. ->will($this->onConsecutiveCalls(
  790. 'Foobar', // Or type in the name of the model
  791. 'y', // Do you want to use this table
  792. 'n' // Doesn't exist, continue anyway?
  793. ));
  794. $this->Task->execute();
  795. }
  796. /**
  797. * test using bake interactively with a table that does not exist.
  798. *
  799. * @return void
  800. */
  801. public function testForcedExecuteWithNonExistantTableName() {
  802. $this->markTestIncomplete('Not done here yet');
  803. $this->Task->connection = 'test';
  804. $this->Task->path = '/my/path/';
  805. $this->Task->expects($this->any())->method('in')
  806. ->will($this->onConsecutiveCalls(
  807. 'Foobar', // Or type in the name of the model
  808. 'y', // Do you want to use this table
  809. 'y', // Doesn't exist, continue anyway?
  810. 'id', // Primary key
  811. 'y' // Looks good?
  812. ));
  813. $this->Task->execute();
  814. }
  815. }