ModelTaskTest.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 1.2.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console\Command\Task;
  16. use Cake\Console\Command\Task\ModelTask;
  17. use Cake\Console\Command\Task\TemplateTask;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Model\Model;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\ClassRegistry;
  24. use Cake\Utility\Inflector;
  25. /**
  26. * ModelTaskTest class
  27. */
  28. class ModelTaskTest extends TestCase {
  29. /**
  30. * fixtures
  31. *
  32. * @var array
  33. */
  34. public $fixtures = array(
  35. 'core.bake_article', 'core.bake_comment', 'core.bake_articles_bake_tag',
  36. 'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree',
  37. 'core.counter_cache_user', 'core.counter_cache_post'
  38. );
  39. /**
  40. * setUp method
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  47. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  48. array('in', 'err', 'createFile', '_stop', '_checkUnitTest'),
  49. array($io)
  50. );
  51. $this->Task->connection = 'test';
  52. $this->_setupOtherMocks();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Setup a mock that has out mocked. Normally this is not used as it makes $this->at() really tricky.
  57. *
  58. * @return void
  59. */
  60. protected function _useMockedOut() {
  61. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  62. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  63. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
  64. array($io)
  65. );
  66. $this->_setupOtherMocks();
  67. }
  68. /**
  69. * sets up the rest of the dependencies for Model Task
  70. *
  71. * @return void
  72. */
  73. protected function _setupOtherMocks() {
  74. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  75. $this->Task->Fixture = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
  76. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
  77. $this->Task->Template = new TemplateTask($io);
  78. $this->Task->Template->interactive = false;
  79. $this->Task->name = 'Model';
  80. }
  81. /**
  82. * tearDown method
  83. *
  84. * @return void
  85. */
  86. public function tearDown() {
  87. parent::tearDown();
  88. unset($this->Task);
  89. $this->fixtureManager->shutDown();
  90. }
  91. /**
  92. * Test that listAll uses the connection property
  93. *
  94. * @return void
  95. */
  96. public function testListAllConnection() {
  97. $this->_useMockedOut();
  98. $this->Task->connection = 'test';
  99. $result = $this->Task->listAll();
  100. $this->assertContains('bake_articles', $result);
  101. $this->assertContains('bake_articles_bake_tags', $result);
  102. $this->assertContains('bake_tags', $result);
  103. $this->assertContains('bake_comments', $result);
  104. $this->assertContains('category_threads', $result);
  105. }
  106. /**
  107. * Test getName() method.
  108. *
  109. * @return void
  110. */
  111. public function testGetTable() {
  112. $result = $this->Task->getTable('BakeArticle');
  113. $this->assertEquals('bake_articles', $result);
  114. $result = $this->Task->getTable('BakeArticles');
  115. $this->assertEquals('bake_articles', $result);
  116. $this->Task->params['table'] = 'bake_articles';
  117. $result = $this->Task->getTable('Article');
  118. $this->assertEquals('bake_articles', $result);
  119. }
  120. /**
  121. * Test getting the a table class.
  122. *
  123. * @return void
  124. */
  125. public function testGetTableObject() {
  126. $result = $this->Task->getTableObject('Article', 'bake_articles');
  127. $this->assertInstanceOf('Cake\ORM\Table', $result);
  128. $this->assertEquals('bake_articles', $result->table());
  129. $this->assertEquals('Article', $result->alias());
  130. }
  131. /**
  132. * Test getAssociations with off flag.
  133. *
  134. * @return void
  135. */
  136. public function testGetAssociationsNoFlag() {
  137. $this->Task->params['no-associations'] = true;
  138. $articles = TableRegistry::get('BakeArticle');
  139. $this->assertEquals([], $this->Task->getAssociations($articles));
  140. }
  141. /**
  142. * Test applying associations.
  143. *
  144. * @return void
  145. */
  146. public function testApplyAssociations() {
  147. $articles = TableRegistry::get('BakeArticles');
  148. $assocs = [
  149. 'belongsTo' => [
  150. [
  151. 'alias' => 'BakeUsers',
  152. 'foreignKey' => 'bake_user_id',
  153. ],
  154. ],
  155. 'hasMany' => [
  156. [
  157. 'alias' => 'BakeComments',
  158. 'foreignKey' => 'bake_article_id',
  159. ],
  160. ],
  161. 'belongsToMany' => [
  162. [
  163. 'alias' => 'BakeTags',
  164. 'foreignKey' => 'bake_article_id',
  165. 'joinTable' => 'bake_articles_bake_tags',
  166. 'targetForeignKey' => 'bake_tag_id',
  167. ],
  168. ],
  169. ];
  170. $original = $articles->associations()->keys();
  171. $this->assertEquals([], $original);
  172. $this->Task->applyAssociations($articles, $assocs);
  173. $new = $articles->associations()->keys();
  174. $expected = ['bakeusers', 'bakecomments', 'baketags'];
  175. $this->assertEquals($expected, $new);
  176. }
  177. /**
  178. * Test applying associations does nothing on a concrete class
  179. *
  180. * @return void
  181. */
  182. public function testApplyAssociationsConcreteClass() {
  183. Configure::write('App.namespace', 'TestApp');
  184. $articles = TableRegistry::get('Articles');
  185. $assocs = [
  186. 'belongsTo' => [
  187. [
  188. 'alias' => 'BakeUsers',
  189. 'foreignKey' => 'bake_user_id',
  190. ],
  191. ],
  192. 'hasMany' => [
  193. [
  194. 'alias' => 'BakeComments',
  195. 'foreignKey' => 'bake_article_id',
  196. ],
  197. ],
  198. 'belongsToMany' => [
  199. [
  200. 'alias' => 'BakeTags',
  201. 'foreignKey' => 'bake_article_id',
  202. 'joinTable' => 'bake_articles_bake_tags',
  203. 'targetForeignKey' => 'bake_tag_id',
  204. ],
  205. ],
  206. ];
  207. $original = $articles->associations()->keys();
  208. $this->Task->applyAssociations($articles, $assocs);
  209. $new = $articles->associations()->keys();
  210. $this->assertEquals($original, $new);
  211. }
  212. /**
  213. * Test getAssociations
  214. *
  215. * @return void
  216. */
  217. public function testGetAssociations() {
  218. $articles = TableRegistry::get('BakeArticles');
  219. $result = $this->Task->getAssociations($articles);
  220. $expected = [
  221. 'belongsTo' => [
  222. [
  223. 'alias' => 'BakeUsers',
  224. 'foreignKey' => 'bake_user_id'
  225. ],
  226. ],
  227. 'hasMany' => [
  228. [
  229. 'alias' => 'BakeComments',
  230. 'foreignKey' => 'bake_article_id',
  231. ],
  232. ],
  233. 'belongsToMany' => [
  234. [
  235. 'alias' => 'BakeTags',
  236. 'foreignKey' => 'bake_article_id',
  237. 'joinTable' => 'bake_articles_bake_tags',
  238. 'targetForeignKey' => 'bake_tag_id',
  239. ],
  240. ],
  241. ];
  242. $this->assertEquals($expected, $result);
  243. }
  244. /**
  245. * Test getAssociations in a plugin
  246. *
  247. * @return void
  248. */
  249. public function testGetAssociationsPlugin() {
  250. $articles = TableRegistry::get('BakeArticles');
  251. $this->Task->plugin = 'TestPlugin';
  252. $result = $this->Task->getAssociations($articles);
  253. $expected = [
  254. 'belongsTo' => [
  255. [
  256. 'alias' => 'BakeUsers',
  257. 'className' => 'TestPlugin.BakeUsers',
  258. 'foreignKey' => 'bake_user_id'
  259. ],
  260. ],
  261. 'hasMany' => [
  262. [
  263. 'alias' => 'BakeComments',
  264. 'className' => 'TestPlugin.BakeComments',
  265. 'foreignKey' => 'bake_article_id',
  266. ],
  267. ],
  268. 'belongsToMany' => [
  269. [
  270. 'alias' => 'BakeTags',
  271. 'className' => 'TestPlugin.BakeTags',
  272. 'foreignKey' => 'bake_article_id',
  273. 'joinTable' => 'bake_articles_bake_tags',
  274. 'targetForeignKey' => 'bake_tag_id',
  275. ],
  276. ],
  277. ];
  278. $this->assertEquals($expected, $result);
  279. }
  280. /**
  281. * test that belongsTo generation works.
  282. *
  283. * @return void
  284. */
  285. public function testBelongsToGeneration() {
  286. $model = TableRegistry::get('BakeComments');
  287. $result = $this->Task->findBelongsTo($model, []);
  288. $expected = [
  289. 'belongsTo' => [
  290. [
  291. 'alias' => 'BakeArticles',
  292. 'foreignKey' => 'bake_article_id'
  293. ],
  294. [
  295. 'alias' => 'BakeUsers',
  296. 'foreignKey' => 'bake_user_id'
  297. ],
  298. ]
  299. ];
  300. $this->assertEquals($expected, $result);
  301. $model = TableRegistry::get('CategoryThreads');
  302. $result = $this->Task->findBelongsTo($model, array());
  303. $expected = [
  304. 'belongsTo' => [
  305. [
  306. 'alias' => 'ParentCategoryThreads',
  307. 'className' => 'CategoryThreads',
  308. 'foreignKey' => 'parent_id'
  309. ],
  310. ]
  311. ];
  312. $this->assertEquals($expected, $result);
  313. $this->Task->plugin = 'Blog';
  314. $result = $this->Task->findBelongsTo($model, array());
  315. $expected = [
  316. 'belongsTo' => [
  317. [
  318. 'alias' => 'ParentCategoryThreads',
  319. 'className' => 'Blog.CategoryThreads',
  320. 'foreignKey' => 'parent_id'
  321. ],
  322. ]
  323. ];
  324. $this->assertEquals($expected, $result);
  325. }
  326. /**
  327. * test that hasOne and/or hasMany relations are generated properly.
  328. *
  329. * @return void
  330. */
  331. public function testHasManyGeneration() {
  332. $this->Task->connection = 'test';
  333. $model = TableRegistry::get('BakeArticles');
  334. $result = $this->Task->findHasMany($model, []);
  335. $expected = [
  336. 'hasMany' => [
  337. [
  338. 'alias' => 'BakeComments',
  339. 'foreignKey' => 'bake_article_id',
  340. ],
  341. ],
  342. ];
  343. $this->assertEquals($expected, $result);
  344. $model = TableRegistry::get('CategoryThreads');
  345. $result = $this->Task->findHasMany($model, []);
  346. $expected = [
  347. 'hasMany' => [
  348. [
  349. 'alias' => 'ChildCategoryThreads',
  350. 'className' => 'CategoryThreads',
  351. 'foreignKey' => 'parent_id',
  352. ],
  353. ]
  354. ];
  355. $this->assertEquals($expected, $result);
  356. $this->Task->plugin = 'Blog';
  357. $result = $this->Task->findHasMany($model, array());
  358. $expected = [
  359. 'hasMany' => [
  360. [
  361. 'alias' => 'ChildCategoryThreads',
  362. 'className' => 'Blog.CategoryThreads',
  363. 'foreignKey' => 'parent_id'
  364. ],
  365. ]
  366. ];
  367. $this->assertEquals($expected, $result);
  368. }
  369. /**
  370. * Test that HABTM generation works
  371. *
  372. * @return void
  373. */
  374. public function testHasAndBelongsToManyGeneration() {
  375. $this->Task->connection = 'test';
  376. $model = TableRegistry::get('BakeArticles');
  377. $result = $this->Task->findBelongsToMany($model, []);
  378. $expected = [
  379. 'belongsToMany' => [
  380. [
  381. 'alias' => 'BakeTags',
  382. 'foreignKey' => 'bake_article_id',
  383. 'joinTable' => 'bake_articles_bake_tags',
  384. 'targetForeignKey' => 'bake_tag_id',
  385. ],
  386. ],
  387. ];
  388. $this->assertEquals($expected, $result);
  389. }
  390. /**
  391. * Test getting accessible fields.
  392. *
  393. * @return void
  394. */
  395. public function testGetFields() {
  396. $model = TableRegistry::get('BakeArticles');
  397. $result = $this->Task->getFields($model);
  398. $expected = [
  399. 'bake_user_id',
  400. 'title',
  401. 'body',
  402. 'published',
  403. ];
  404. $this->assertEquals($expected, $result);
  405. }
  406. /**
  407. * Test getting accessible fields includes associations.
  408. *
  409. * @return void
  410. */
  411. public function testGetFieldsAssociations() {
  412. $model = TableRegistry::get('BakeArticles');
  413. $model->belongsToMany('BakeTags');
  414. $model->belongsTo('BakeAuthors');
  415. $model->hasMany('BakeComments');
  416. $result = $this->Task->getFields($model);
  417. $this->assertContains('bake_tags', $result);
  418. $this->assertContains('bake_comments', $result);
  419. $this->assertContains('bake_author', $result);
  420. }
  421. /**
  422. * Test getting field with the no- option
  423. *
  424. * @return void
  425. */
  426. public function testGetFieldsDisabled() {
  427. $model = TableRegistry::get('BakeArticles');
  428. $this->Task->params['no-fields'] = true;
  429. $result = $this->Task->getFields($model);
  430. $this->assertEquals([], $result);
  431. }
  432. /**
  433. * Test getting field with a whitelist
  434. *
  435. * @return void
  436. */
  437. public function testGetFieldsWhiteList() {
  438. $model = TableRegistry::get('BakeArticles');
  439. $this->Task->params['fields'] = 'id, title , , body , created';
  440. $result = $this->Task->getFields($model);
  441. $expected = [
  442. 'id',
  443. 'title',
  444. 'body',
  445. 'created',
  446. ];
  447. $this->assertEquals($expected, $result);
  448. }
  449. /**
  450. * Test getting hidden fields.
  451. *
  452. * @return void
  453. */
  454. public function testGetHiddenFields() {
  455. $model = TableRegistry::get('Users');
  456. $result = $this->Task->getHiddenFields($model);
  457. $expected = [
  458. 'password',
  459. ];
  460. $this->assertEquals($expected, $result);
  461. }
  462. /**
  463. * Test getting hidden field with the no- option
  464. *
  465. * @return void
  466. */
  467. public function testGetHiddenFieldsDisabled() {
  468. $model = TableRegistry::get('Users');
  469. $this->Task->params['no-hidden'] = true;
  470. $result = $this->Task->getHiddenFields($model);
  471. $this->assertEquals([], $result);
  472. }
  473. /**
  474. * Test getting hidden field with a whitelist
  475. *
  476. * @return void
  477. */
  478. public function testGetHiddenFieldsWhiteList() {
  479. $model = TableRegistry::get('Users');
  480. $this->Task->params['hidden'] = 'id, title , , body , created';
  481. $result = $this->Task->getHiddenFields($model);
  482. $expected = [
  483. 'id',
  484. 'title',
  485. 'body',
  486. 'created',
  487. ];
  488. $this->assertEquals($expected, $result);
  489. }
  490. /**
  491. * Test getting primary key
  492. *
  493. * @return void
  494. */
  495. public function testGetPrimaryKey() {
  496. $model = TableRegistry::get('BakeArticles');
  497. $result = $this->Task->getPrimaryKey($model);
  498. $expected = ['id'];
  499. $this->assertEquals($expected, $result);
  500. $this->Task->params['primary-key'] = 'id, , account_id';
  501. $result = $this->Task->getPrimaryKey($model);
  502. $expected = ['id', 'account_id'];
  503. $this->assertEquals($expected, $result);
  504. }
  505. /**
  506. * test getting validation rules with the no-validation rule.
  507. *
  508. * @return void
  509. */
  510. public function testGetValidationDisabled() {
  511. $model = TableRegistry::get('BakeArticles');
  512. $this->Task->params['no-validation'] = true;
  513. $result = $this->Task->getValidation($model);
  514. $this->assertEquals([], $result);
  515. }
  516. /**
  517. * test getting validation rules.
  518. *
  519. * @return void
  520. */
  521. public function testGetValidation() {
  522. $model = TableRegistry::get('BakeArticles');
  523. $result = $this->Task->getValidation($model);
  524. $expected = [
  525. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  526. 'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
  527. 'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  528. 'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
  529. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  530. ];
  531. $this->assertEquals($expected, $result);
  532. $model = TableRegistry::get('BakeComments');
  533. $result = $this->Task->getValidation($model);
  534. $expected = [
  535. 'bake_article_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  536. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  537. 'comment' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  538. 'published' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  539. 'otherid' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  540. ];
  541. $this->assertEquals($expected, $result);
  542. }
  543. /**
  544. * Tests that a username column will get a validateUnique rule applied
  545. *
  546. * @return void
  547. */
  548. public function testGetValidationWithUnique() {
  549. $model = TableRegistry::get('Users');
  550. $result = $this->Task->getValidation($model);
  551. $expected = [
  552. 'password' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  553. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']],
  554. 'username' => [
  555. 'valid' => [
  556. 'rule' => false,
  557. 'allowEmpty' => true
  558. ],
  559. 'unique' => [
  560. 'rule' => 'validateUnique',
  561. 'provider' => 'table'
  562. ]
  563. ]
  564. ];
  565. $this->assertEquals($expected, $result);
  566. }
  567. /**
  568. * test non interactive doActsAs
  569. *
  570. * @return void
  571. */
  572. public function testGetBehaviors() {
  573. $model = TableRegistry::get('NumberTrees');
  574. $result = $this->Task->getBehaviors($model);
  575. $this->assertEquals(['Tree' => []], $result);
  576. $model = TableRegistry::get('BakeArticles');
  577. $result = $this->Task->getBehaviors($model);
  578. $this->assertEquals(['Timestamp' => []], $result);
  579. TableRegistry::clear();
  580. TableRegistry::get('Users', [
  581. 'table' => 'counter_cache_users'
  582. ]);
  583. $model = TableRegistry::get('Posts', [
  584. 'table' => 'counter_cache_posts'
  585. ]);
  586. $result = $this->Task->getBehaviors($model);
  587. $expected = [
  588. 'CounterCache' => ["'Users' => ['post_count']"]
  589. ];
  590. $this->assertEquals($expected, $result);
  591. }
  592. /**
  593. * Test getDisplayField() method.
  594. *
  595. * @return void
  596. */
  597. public function testGetDisplayField() {
  598. $model = TableRegistry::get('BakeArticles');
  599. $result = $this->Task->getDisplayField($model);
  600. $this->assertEquals('title', $result);
  601. $this->Task->params['display-field'] = 'custom';
  602. $result = $this->Task->getDisplayField($model);
  603. $this->assertEquals('custom', $result);
  604. }
  605. /**
  606. * Ensure that the fixture object is correctly called.
  607. *
  608. * @return void
  609. */
  610. public function testBakeFixture() {
  611. $this->Task->plugin = 'TestPlugin';
  612. $this->Task->Fixture->expects($this->at(0))
  613. ->method('bake')
  614. ->with('BakeArticle', 'bake_articles');
  615. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  616. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  617. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  618. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  619. }
  620. /**
  621. * Ensure that the fixture baking can be disabled
  622. *
  623. * @return void
  624. */
  625. public function testBakeFixtureDisabled() {
  626. $this->Task->params['no-fixture'] = true;
  627. $this->Task->plugin = 'TestPlugin';
  628. $this->Task->Fixture->expects($this->never())
  629. ->method('bake');
  630. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  631. }
  632. /**
  633. * Ensure that the test object is correctly called.
  634. *
  635. * @return void
  636. */
  637. public function testBakeTest() {
  638. $this->Task->plugin = 'TestPlugin';
  639. $this->Task->Test->expects($this->at(0))
  640. ->method('bake')
  641. ->with('Table', 'BakeArticle');
  642. $this->Task->bakeTest('BakeArticle');
  643. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  644. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  645. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  646. }
  647. /**
  648. * Ensure that test baking can be disabled.
  649. *
  650. * @return void
  651. */
  652. public function testBakeTestDisabled() {
  653. $this->Task->params['no-test'] = true;
  654. $this->Task->plugin = 'TestPlugin';
  655. $this->Task->Test->expects($this->never())
  656. ->method('bake');
  657. $this->Task->bakeTest('BakeArticle');
  658. }
  659. /**
  660. * test baking validation
  661. *
  662. * @return void
  663. */
  664. public function testBakeTableValidation() {
  665. $validation = [
  666. 'id' => [
  667. 'valid' => array(
  668. 'allowEmpty' => 'create',
  669. 'rule' => 'numeric',
  670. )
  671. ],
  672. 'name' => [
  673. 'valid' => [
  674. 'allowEmpty' => false,
  675. 'rule' => false,
  676. ]
  677. ],
  678. 'email' => [
  679. 'valid' => [
  680. 'allowEmpty' => true,
  681. 'rule' => 'email'
  682. ],
  683. 'unique' => [
  684. 'rule' => 'validateUnique',
  685. 'provider' => 'table'
  686. ]
  687. ]
  688. ];
  689. $model = TableRegistry::get('BakeArticles');
  690. $result = $this->Task->bakeTable($model, compact('validation'));
  691. $this->assertContains('namespace App\Model\Table;', $result);
  692. $this->assertContains('use Cake\ORM\Table;', $result);
  693. $this->assertContains('use Cake\Validation\Validator;', $result);
  694. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  695. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  696. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  697. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  698. $this->assertContains(
  699. "->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
  700. $result);
  701. $this->assertContains("->allowEmpty('id', 'create')", $result);
  702. $this->assertContains("->allowEmpty('email')", $result);
  703. $this->assertContains("->validatePresence('name', 'create')", $result);
  704. }
  705. /**
  706. * test baking
  707. *
  708. * @return void
  709. */
  710. public function testBakeTableConfig() {
  711. $config = [
  712. 'table' => 'articles',
  713. 'primaryKey' => ['id'],
  714. 'displayField' => 'title',
  715. 'behaviors' => ['Timestamp' => ''],
  716. ];
  717. $model = TableRegistry::get('BakeArticles');
  718. $result = $this->Task->bakeTable($model, $config);
  719. $this->assertContains('public function initialize(array $config) {', $result);
  720. $this->assertContains("this->primaryKey('id');\n", $result);
  721. $this->assertContains("this->displayField('title');\n", $result);
  722. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  723. $this->assertContains("this->table('articles');\n", $result);
  724. $this->assertContains('use Cake\Validation\Validator;', $result);
  725. }
  726. /**
  727. * test baking relations
  728. *
  729. * @return void
  730. */
  731. public function testBakeTableRelations() {
  732. $associations = [
  733. 'belongsTo' => [
  734. [
  735. 'alias' => 'SomethingElse',
  736. 'foreignKey' => 'something_else_id',
  737. ],
  738. [
  739. 'alias' => 'BakeUser',
  740. 'foreignKey' => 'bake_user_id',
  741. ],
  742. ],
  743. 'hasMany' => [
  744. [
  745. 'alias' => 'BakeComment',
  746. 'foreignKey' => 'parent_id',
  747. ],
  748. ],
  749. 'belongsToMany' => [
  750. [
  751. 'alias' => 'BakeTag',
  752. 'foreignKey' => 'bake_article_id',
  753. 'joinTable' => 'bake_articles_bake_tags',
  754. 'targetForeignKey' => 'bake_tag_id',
  755. ],
  756. ]
  757. ];
  758. $model = TableRegistry::get('BakeArticles');
  759. $result = $this->Task->bakeTable($model, compact('associations'));
  760. $this->assertContains("this->hasMany('BakeComment', [", $result);
  761. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  762. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  763. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  764. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  765. }
  766. /**
  767. * test baking an entity class
  768. *
  769. * @return void
  770. */
  771. public function testBakeEntity() {
  772. $config = [
  773. 'fields' => []
  774. ];
  775. $model = TableRegistry::get('BakeArticles');
  776. $result = $this->Task->bakeEntity($model, $config);
  777. $this->assertContains('namespace App\Model\Entity;', $result);
  778. $this->assertContains('use Cake\ORM\Entity;', $result);
  779. $this->assertContains('class BakeArticle extends Entity {', $result);
  780. $this->assertNotContains('$_accessible', $result);
  781. }
  782. /**
  783. * test baking an entity class
  784. *
  785. * @return void
  786. */
  787. public function testBakeEntityFields() {
  788. $config = [
  789. 'fields' => ['title', 'body', 'published']
  790. ];
  791. $model = TableRegistry::get('BakeArticles');
  792. $result = $this->Task->bakeEntity($model, $config);
  793. $this->assertContains("protected \$_accessible = [", $result);
  794. $this->assertContains("'title' => true,", $result);
  795. $this->assertContains("'body' => true,", $result);
  796. $this->assertContains("'published' => true", $result);
  797. $this->assertNotContains("protected \$_hidden", $result);
  798. }
  799. /**
  800. * test baking an entity class sets hidden fields.
  801. *
  802. * @return void
  803. */
  804. public function testBakeEntityHidden() {
  805. $model = TableRegistry::get('BakeUsers');
  806. $config = [
  807. 'hidden' => ['password'],
  808. ];
  809. $result = $this->Task->bakeEntity($model, $config);
  810. $this->assertContains("protected \$_hidden = [", $result);
  811. $this->assertContains("'password'", $result);
  812. $this->assertNotContains("protected \$_accessible", $result);
  813. }
  814. /**
  815. * test bake() with a -plugin param
  816. *
  817. * @return void
  818. */
  819. public function testBakeTableWithPlugin() {
  820. $this->Task->plugin = 'ControllerTest';
  821. // fake plugin path
  822. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  823. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
  824. $this->Task->expects($this->once())->method('createFile')
  825. ->with($path, $this->logicalAnd(
  826. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  827. $this->stringContains('use Cake\\ORM\\Table;'),
  828. $this->stringContains('class BakeArticlesTable extends Table {')
  829. ));
  830. $model = TableRegistry::get('BakeArticles');
  831. $this->Task->bakeTable($model);
  832. }
  833. /**
  834. * test bake() with a -plugin param
  835. *
  836. * @return void
  837. */
  838. public function testBakeEntityWithPlugin() {
  839. $this->Task->plugin = 'ControllerTest';
  840. // fake plugin path
  841. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  842. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  843. $path = $this->_normalizePath($path);
  844. $this->Task->expects($this->once())->method('createFile')
  845. ->with($path, $this->logicalAnd(
  846. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  847. $this->stringContains('use Cake\\ORM\\Entity;'),
  848. $this->stringContains('class BakeArticle extends Entity {')
  849. ));
  850. $model = TableRegistry::get('BakeArticles');
  851. $this->Task->bakeEntity($model);
  852. }
  853. /**
  854. * test that execute with no args
  855. *
  856. * @return void
  857. */
  858. public function testMainNoArgs() {
  859. $this->_useMockedOut();
  860. $this->Task->connection = 'test';
  861. $this->Task->path = '/my/path/';
  862. $this->Task->expects($this->at(0))
  863. ->method('out')
  864. ->with($this->stringContains('Choose a model to bake from the following:'));
  865. $this->Task->main();
  866. }
  867. /**
  868. * test that execute passes runs bake depending with named model.
  869. *
  870. * @return void
  871. */
  872. public function testMainWithNamedModel() {
  873. $this->Task->connection = 'test';
  874. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  875. $this->Task->expects($this->at(0))
  876. ->method('createFile')
  877. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  878. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  879. $this->Task->expects($this->at(1))
  880. ->method('createFile')
  881. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  882. $this->Task->main('BakeArticles');
  883. }
  884. /**
  885. * data provider for testMainWithNamedModelVariations
  886. *
  887. * @return void
  888. */
  889. public static function nameVariations() {
  890. return array(
  891. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  892. );
  893. }
  894. /**
  895. * test that execute passes with different inflections of the same name.
  896. *
  897. * @dataProvider nameVariations
  898. * @return void
  899. */
  900. public function testMainWithNamedModelVariations($name) {
  901. $this->Task->connection = 'test';
  902. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  903. $this->Task->expects($this->at(0))
  904. ->method('createFile')
  905. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  906. $this->Task->main($name);
  907. }
  908. /**
  909. * test that execute runs all() when args[0] = all
  910. *
  911. * @return void
  912. */
  913. public function testMainIntoAll() {
  914. $count = count($this->Task->listAll());
  915. if ($count != count($this->fixtures)) {
  916. $this->markTestSkipped('Additional tables detected.');
  917. }
  918. $this->Task->connection = 'test';
  919. $this->Task->Fixture->expects($this->exactly($count))
  920. ->method('bake');
  921. $this->Task->Test->expects($this->exactly($count))
  922. ->method('bake');
  923. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  924. $this->Task->expects($this->at(0))
  925. ->method('createFile')
  926. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  927. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  928. $this->Task->expects($this->at(1))
  929. ->method('createFile')
  930. ->with($filename, $this->stringContains('class BakeArticle extends'));
  931. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  932. $this->Task->expects($this->at(2))
  933. ->method('createFile')
  934. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  935. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  936. $this->Task->expects($this->at(3))
  937. ->method('createFile')
  938. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  939. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  940. $this->Task->expects($this->at(4))
  941. ->method('createFile')
  942. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  943. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  944. $this->Task->expects($this->at(5))
  945. ->method('createFile')
  946. ->with($filename, $this->stringContains('class BakeComment extends'));
  947. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  948. $this->Task->expects($this->at(6))
  949. ->method('createFile')
  950. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  951. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  952. $this->Task->expects($this->at(7))
  953. ->method('createFile')
  954. ->with($filename, $this->stringContains('class BakeTag extends'));
  955. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  956. $this->Task->expects($this->at(8))
  957. ->method('createFile')
  958. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  959. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  960. $this->Task->expects($this->at(9))
  961. ->method('createFile')
  962. ->with($filename, $this->stringContains('class CategoryThread extends'));
  963. $this->Task->all();
  964. }
  965. /**
  966. * test that skipTables changes how all() works.
  967. *
  968. * @return void
  969. */
  970. public function testSkipTablesAndAll() {
  971. $count = count($this->Task->listAll('test'));
  972. if ($count != count($this->fixtures)) {
  973. $this->markTestSkipped('Additional tables detected.');
  974. }
  975. $this->Task->connection = 'test';
  976. $this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
  977. $this->Task->Fixture->expects($this->exactly(7))
  978. ->method('bake');
  979. $this->Task->Test->expects($this->exactly(7))
  980. ->method('bake');
  981. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  982. $this->Task->expects($this->at(1))
  983. ->method('createFile')
  984. ->with($filename);
  985. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  986. $this->Task->expects($this->at(3))
  987. ->method('createFile')
  988. ->with($filename);
  989. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  990. $this->Task->expects($this->at(5))
  991. ->method('createFile')
  992. ->with($filename);
  993. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  994. $this->Task->expects($this->at(7))
  995. ->method('createFile')
  996. ->with($filename);
  997. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  998. $this->Task->expects($this->at(9))
  999. ->method('createFile')
  1000. ->with($filename);
  1001. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  1002. $this->Task->expects($this->at(11))
  1003. ->method('createFile')
  1004. ->with($filename);
  1005. $this->Task->all();
  1006. }
  1007. }