ModelTaskTest.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. */
  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.category_thread', 'core.number_tree'
  37. );
  38. /**
  39. * setUp method
  40. *
  41. * @return void
  42. */
  43. public function setUp() {
  44. parent::setUp();
  45. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  46. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  47. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  48. array('in', 'err', 'createFile', '_stop', '_checkUnitTest'),
  49. array($out, $out, $in)
  50. );
  51. $this->Task->connection = 'test';
  52. $this->_setupOtherMocks();
  53. }
  54. /**
  55. * Setup a mock that has out mocked. Normally this is not used as it makes $this->at() really tricky.
  56. *
  57. * @return void
  58. */
  59. protected function _useMockedOut() {
  60. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  61. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  62. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  63. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
  64. array($out, $out, $in)
  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. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  75. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  76. $this->Task->Fixture = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$out, $out, $in]);
  77. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$out, $out, $in]);
  78. $this->Task->Template = new TemplateTask($out, $out, $in);
  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. }
  90. /**
  91. * Test that listAll uses the connection property
  92. *
  93. * @return void
  94. */
  95. public function testListAllConnection() {
  96. $this->_useMockedOut();
  97. $this->Task->connection = 'test';
  98. $result = $this->Task->listAll();
  99. $this->assertContains('bake_articles', $result);
  100. $this->assertContains('bake_articles_bake_tags', $result);
  101. $this->assertContains('bake_tags', $result);
  102. $this->assertContains('bake_comments', $result);
  103. $this->assertContains('category_threads', $result);
  104. }
  105. /**
  106. * Test getName() method.
  107. *
  108. * @return void
  109. */
  110. public function testGetTable() {
  111. $this->Task->args[0] = 'BakeArticle';
  112. $result = $this->Task->getTable();
  113. $this->assertEquals('bake_articles', $result);
  114. $this->Task->args[0] = 'BakeArticles';
  115. $result = $this->Task->getTable();
  116. $this->assertEquals('bake_articles', $result);
  117. $this->Task->args[0] = 'Article';
  118. $this->Task->params['table'] = 'bake_articles';
  119. $result = $this->Task->getTable();
  120. $this->assertEquals('bake_articles', $result);
  121. }
  122. /**
  123. * Test getting the a table class.
  124. *
  125. * @return void
  126. */
  127. public function testGetTableObject() {
  128. $result = $this->Task->getTableObject('Article', 'bake_articles');
  129. $this->assertInstanceOf('Cake\ORM\Table', $result);
  130. $this->assertEquals('bake_articles', $result->table());
  131. $this->assertEquals('Article', $result->alias());
  132. }
  133. /**
  134. * Test getAssociations with off flag.
  135. *
  136. * @return void
  137. */
  138. public function testGetAssociationsNoFlag() {
  139. $this->Task->params['no-associations'] = true;
  140. $articles = TableRegistry::get('BakeArticle');
  141. $this->assertEquals([], $this->Task->getAssociations($articles));
  142. }
  143. /**
  144. * Test getAssociations
  145. *
  146. * @return void
  147. */
  148. public function testGetAssociations() {
  149. $articles = TableRegistry::get('BakeArticles');
  150. $result = $this->Task->getAssociations($articles);
  151. $expected = [
  152. 'belongsTo' => [
  153. [
  154. 'alias' => 'BakeUsers',
  155. 'className' => 'BakeUsers',
  156. 'foreignKey' => 'bake_user_id',
  157. ],
  158. ],
  159. 'hasMany' => [
  160. [
  161. 'alias' => 'BakeComments',
  162. 'className' => 'BakeComments',
  163. 'foreignKey' => 'bake_article_id',
  164. ],
  165. ],
  166. 'belongsToMany' => [
  167. [
  168. 'alias' => 'BakeTags',
  169. 'className' => 'BakeTags',
  170. 'foreignKey' => 'bake_article_id',
  171. 'joinTable' => 'bake_articles_bake_tags',
  172. 'targetForeignKey' => 'bake_tag_id',
  173. ],
  174. ],
  175. ];
  176. $this->assertEquals($expected, $result);
  177. }
  178. /**
  179. * test that belongsTo generation works.
  180. *
  181. * @return void
  182. */
  183. public function testBelongsToGeneration() {
  184. $model = TableRegistry::get('BakeComments');
  185. $result = $this->Task->findBelongsTo($model, []);
  186. $expected = [
  187. 'belongsTo' => [
  188. [
  189. 'alias' => 'BakeArticles',
  190. 'className' => 'BakeArticles',
  191. 'foreignKey' => 'bake_article_id',
  192. ],
  193. [
  194. 'alias' => 'BakeUsers',
  195. 'className' => 'BakeUsers',
  196. 'foreignKey' => 'bake_user_id',
  197. ],
  198. ]
  199. ];
  200. $this->assertEquals($expected, $result);
  201. $model = TableRegistry::get('CategoryThreads');
  202. $result = $this->Task->findBelongsTo($model, array());
  203. $expected = [
  204. 'belongsTo' => [
  205. [
  206. 'alias' => 'ParentCategoryThreads',
  207. 'className' => 'CategoryThreads',
  208. 'foreignKey' => 'parent_id',
  209. ],
  210. ]
  211. ];
  212. $this->assertEquals($expected, $result);
  213. }
  214. /**
  215. * test that hasOne and/or hasMany relations are generated properly.
  216. *
  217. * @return void
  218. */
  219. public function testHasManyGeneration() {
  220. $this->Task->connection = 'test';
  221. $model = TableRegistry::get('BakeArticles');
  222. $result = $this->Task->findHasMany($model, []);
  223. $expected = [
  224. 'hasMany' => [
  225. [
  226. 'alias' => 'BakeComments',
  227. 'className' => 'BakeComments',
  228. 'foreignKey' => 'bake_article_id',
  229. ],
  230. ],
  231. ];
  232. $this->assertEquals($expected, $result);
  233. $model = TableRegistry::get('CategoryThreads');
  234. $result = $this->Task->findHasMany($model, []);
  235. $expected = [
  236. 'hasMany' => [
  237. [
  238. 'alias' => 'ChildCategoryThreads',
  239. 'className' => 'CategoryThreads',
  240. 'foreignKey' => 'parent_id',
  241. ],
  242. ]
  243. ];
  244. $this->assertEquals($expected, $result);
  245. }
  246. /**
  247. * Test that HABTM generation works
  248. *
  249. * @return void
  250. */
  251. public function testHasAndBelongsToManyGeneration() {
  252. $this->Task->connection = 'test';
  253. $model = TableRegistry::get('BakeArticles');
  254. $result = $this->Task->findBelongsToMany($model, []);
  255. $expected = [
  256. 'belongsToMany' => [
  257. [
  258. 'alias' => 'BakeTags',
  259. 'className' => 'BakeTags',
  260. 'foreignKey' => 'bake_article_id',
  261. 'joinTable' => 'bake_articles_bake_tags',
  262. 'targetForeignKey' => 'bake_tag_id',
  263. ],
  264. ],
  265. ];
  266. $this->assertEquals($expected, $result);
  267. }
  268. /**
  269. * test that initializing the validations works.
  270. *
  271. * @return void
  272. */
  273. public function testInitValidations() {
  274. $this->markTestIncomplete('Not done here yet');
  275. $result = $this->Task->initValidations();
  276. $this->assertTrue(in_array('notEmpty', $result));
  277. }
  278. /**
  279. * test that individual field validation works, with interactive = false
  280. * tests the guessing features of validation
  281. *
  282. * @return void
  283. */
  284. public function testFieldValidationGuessing() {
  285. $this->markTestIncomplete('Not done here yet');
  286. $this->Task->interactive = false;
  287. $this->Task->initValidations();
  288. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  289. $expected = array('notEmpty' => 'notEmpty');
  290. $this->assertEquals($expected, $result);
  291. $result = $this->Task->fieldValidation('text', array('type' => 'date', 'length' => 10, 'null' => false));
  292. $expected = array('date' => 'date');
  293. $this->assertEquals($expected, $result);
  294. $result = $this->Task->fieldValidation('text', array('type' => 'time', 'length' => 10, 'null' => false));
  295. $expected = array('time' => 'time');
  296. $this->assertEquals($expected, $result);
  297. $result = $this->Task->fieldValidation('email', array('type' => 'string', 'length' => 10, 'null' => false));
  298. $expected = array('email' => 'email');
  299. $this->assertEquals($expected, $result);
  300. $result = $this->Task->fieldValidation('test', array('type' => 'integer', 'length' => 10, 'null' => false));
  301. $expected = array('numeric' => 'numeric');
  302. $this->assertEquals($expected, $result);
  303. $result = $this->Task->fieldValidation('test', array('type' => 'boolean', 'length' => 10, 'null' => false));
  304. $expected = array('boolean' => 'boolean');
  305. $this->assertEquals($expected, $result);
  306. }
  307. /**
  308. * test that interactive field validation works and returns multiple validators.
  309. *
  310. * @return void
  311. */
  312. public function testInteractiveFieldValidation() {
  313. $this->markTestIncomplete('Not done here yet');
  314. $this->Task->initValidations();
  315. $this->Task->interactive = true;
  316. $this->Task->expects($this->any())->method('in')
  317. ->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
  318. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  319. $expected = array('notEmpty' => 'notEmpty', 'maxLength' => 'maxLength');
  320. $this->assertEquals($expected, $result);
  321. }
  322. /**
  323. * test that a bogus response doesn't cause errors to bubble up.
  324. *
  325. * @return void
  326. */
  327. public function testInteractiveFieldValidationWithBogusResponse() {
  328. $this->markTestIncomplete('Not done here yet');
  329. $this->_useMockedOut();
  330. $this->Task->initValidations();
  331. $this->Task->interactive = true;
  332. $this->Task->expects($this->any())->method('in')
  333. ->will($this->onConsecutiveCalls('999999', '24', 'n'));
  334. $this->Task->expects($this->at(10))->method('out')
  335. ->with($this->stringContains('make a valid'));
  336. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  337. $expected = array('notEmpty' => 'notEmpty');
  338. $this->assertEquals($expected, $result);
  339. }
  340. /**
  341. * test that a regular expression can be used for validation.
  342. *
  343. * @return void
  344. */
  345. public function testInteractiveFieldValidationWithRegexp() {
  346. $this->markTestIncomplete('Not done here yet');
  347. $this->Task->initValidations();
  348. $this->Task->interactive = true;
  349. $this->Task->expects($this->any())->method('in')
  350. ->will($this->onConsecutiveCalls('/^[a-z]{0,9}$/', 'n'));
  351. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  352. $expected = array('a_z_0_9' => '/^[a-z]{0,9}$/');
  353. $this->assertEquals($expected, $result);
  354. }
  355. /**
  356. * Test that skipping fields during rule choice works when doing interactive field validation.
  357. *
  358. * @return void
  359. */
  360. public function testSkippingChoiceInteractiveFieldValidation() {
  361. $this->markTestIncomplete('Not done here yet');
  362. $this->Task->initValidations();
  363. $this->Task->interactive = true;
  364. $this->Task->expects($this->any())->method('in')
  365. ->will($this->onConsecutiveCalls('24', 'y', 's'));
  366. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  367. $expected = array('notEmpty' => 'notEmpty', '_skipFields' => true);
  368. $this->assertEquals($expected, $result);
  369. }
  370. /**
  371. * Test that skipping fields after rule choice works when doing interactive field validation.
  372. *
  373. * @return void
  374. */
  375. public function testSkippingAnotherInteractiveFieldValidation() {
  376. $this->markTestIncomplete('Not done here yet');
  377. $this->Task->initValidations();
  378. $this->Task->interactive = true;
  379. $this->Task->expects($this->any())->method('in')
  380. ->will($this->onConsecutiveCalls('24', 's'));
  381. $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
  382. $expected = array('notEmpty' => 'notEmpty', '_skipFields' => true);
  383. $this->assertEquals($expected, $result);
  384. }
  385. /**
  386. * Test the validation generation routine with skipping the rest of the fields
  387. * when doing interactive field validation.
  388. *
  389. * @return void
  390. */
  391. public function testInteractiveDoValidationWithSkipping() {
  392. $this->markTestIncomplete('Not done here yet');
  393. $this->Task->expects($this->any())
  394. ->method('in')
  395. ->will($this->onConsecutiveCalls('35', '24', 'n', '11', 's'));
  396. $this->Task->interactive = true;
  397. $Model = $this->getMock('Model');
  398. $Model->primaryKey = 'id';
  399. $Model->expects($this->any())
  400. ->method('schema')
  401. ->will($this->returnValue(array(
  402. 'id' => array(
  403. 'type' => 'integer',
  404. 'length' => 11,
  405. 'null' => false,
  406. 'key' => 'primary',
  407. ),
  408. 'name' => array(
  409. 'type' => 'string',
  410. 'length' => 20,
  411. 'null' => false,
  412. ),
  413. 'email' => array(
  414. 'type' => 'string',
  415. 'length' => 255,
  416. 'null' => false,
  417. ),
  418. 'some_date' => array(
  419. 'type' => 'date',
  420. 'length' => '',
  421. 'null' => false,
  422. ),
  423. 'some_time' => array(
  424. 'type' => 'time',
  425. 'length' => '',
  426. 'null' => false,
  427. ),
  428. 'created' => array(
  429. 'type' => 'datetime',
  430. 'length' => '',
  431. 'null' => false,
  432. )
  433. )
  434. ));
  435. $result = $this->Task->doValidation($Model);
  436. $expected = array(
  437. 'name' => array(
  438. 'notEmpty' => 'notEmpty'
  439. ),
  440. 'email' => array(
  441. 'email' => 'email',
  442. ),
  443. );
  444. $this->assertEquals($expected, $result);
  445. }
  446. /**
  447. * test the validation Generation routine
  448. *
  449. * @return void
  450. */
  451. public function testNonInteractiveDoValidation() {
  452. $this->markTestIncomplete('Not done here yet');
  453. $Model = $this->getMock('Model');
  454. $Model->primaryKey = 'id';
  455. $Model->expects($this->any())
  456. ->method('schema')
  457. ->will($this->returnValue(array(
  458. 'id' => array(
  459. 'type' => 'integer',
  460. 'length' => 11,
  461. 'null' => false,
  462. 'key' => 'primary',
  463. ),
  464. 'name' => array(
  465. 'type' => 'string',
  466. 'length' => 20,
  467. 'null' => false,
  468. ),
  469. 'email' => array(
  470. 'type' => 'string',
  471. 'length' => 255,
  472. 'null' => false,
  473. ),
  474. 'some_date' => array(
  475. 'type' => 'date',
  476. 'length' => '',
  477. 'null' => false,
  478. ),
  479. 'some_time' => array(
  480. 'type' => 'time',
  481. 'length' => '',
  482. 'null' => false,
  483. ),
  484. 'created' => array(
  485. 'type' => 'datetime',
  486. 'length' => '',
  487. 'null' => false,
  488. )
  489. )
  490. ));
  491. $this->Task->interactive = false;
  492. $result = $this->Task->doValidation($Model);
  493. $expected = array(
  494. 'name' => array(
  495. 'notEmpty' => 'notEmpty'
  496. ),
  497. 'email' => array(
  498. 'email' => 'email',
  499. ),
  500. 'some_date' => array(
  501. 'date' => 'date'
  502. ),
  503. 'some_time' => array(
  504. 'time' => 'time'
  505. ),
  506. );
  507. $this->assertEquals($expected, $result);
  508. }
  509. /**
  510. * test non interactive doAssociations
  511. *
  512. * @return void
  513. */
  514. public function testDoAssociationsNonInteractive() {
  515. $this->markTestIncomplete('Not done here yet');
  516. $this->Task->connection = 'test';
  517. $this->Task->interactive = false;
  518. $model = new Model(array('ds' => 'test', 'name' => 'BakeArticle'));
  519. $result = $this->Task->doAssociations($model);
  520. $expected = array(
  521. 'belongsTo' => array(
  522. array(
  523. 'alias' => 'BakeUser',
  524. 'className' => 'BakeUser',
  525. 'foreignKey' => 'bake_user_id',
  526. ),
  527. ),
  528. 'hasMany' => array(
  529. array(
  530. 'alias' => 'BakeComment',
  531. 'className' => 'BakeComment',
  532. 'foreignKey' => 'bake_article_id',
  533. ),
  534. ),
  535. 'hasAndBelongsToMany' => array(
  536. array(
  537. 'alias' => 'BakeTag',
  538. 'className' => 'BakeTag',
  539. 'foreignKey' => 'bake_article_id',
  540. 'joinTable' => 'bake_articles_bake_tags',
  541. 'associationForeignKey' => 'bake_tag_id',
  542. ),
  543. ),
  544. );
  545. $this->assertEquals($expected, $result);
  546. }
  547. /**
  548. * test non interactive doActsAs
  549. *
  550. * @return void
  551. */
  552. public function testDoActsAs() {
  553. $this->markTestIncomplete('Not done here yet');
  554. $this->Task->connection = 'test';
  555. $this->Task->interactive = false;
  556. $model = new Model(array('ds' => 'test', 'name' => 'NumberTree'));
  557. $result = $this->Task->doActsAs($model);
  558. $this->assertEquals(array('Tree'), $result);
  559. }
  560. /**
  561. * Ensure that the fixture object is correctly called.
  562. *
  563. * @return void
  564. */
  565. public function testBakeFixture() {
  566. $this->markTestIncomplete('Not done here yet');
  567. $this->Task->plugin = 'TestPlugin';
  568. $this->Task->interactive = true;
  569. $this->Task->Fixture->expects($this->at(0))->method('bake')->with('BakeArticle', 'bake_articles');
  570. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  571. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  572. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  573. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  574. }
  575. /**
  576. * Ensure that the test object is correctly called.
  577. *
  578. * @return void
  579. */
  580. public function testBakeTest() {
  581. $this->markTestIncomplete('Not done here yet');
  582. $this->Task->plugin = 'TestPlugin';
  583. $this->Task->interactive = true;
  584. $this->Task->Test->expects($this->at(0))->method('bake')->with('Model', 'BakeArticle');
  585. $this->Task->bakeTest('BakeArticle');
  586. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  587. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  588. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  589. }
  590. /**
  591. * test confirming of associations, and that when an association is hasMany
  592. * a question for the hasOne is also not asked.
  593. *
  594. * @return void
  595. */
  596. public function testConfirmAssociations() {
  597. $this->markTestIncomplete('Not done here yet');
  598. $associations = array(
  599. 'hasOne' => array(
  600. array(
  601. 'alias' => 'ChildCategoryThread',
  602. 'className' => 'CategoryThread',
  603. 'foreignKey' => 'parent_id',
  604. ),
  605. ),
  606. 'hasMany' => array(
  607. array(
  608. 'alias' => 'ChildCategoryThread',
  609. 'className' => 'CategoryThread',
  610. 'foreignKey' => 'parent_id',
  611. ),
  612. ),
  613. 'belongsTo' => array(
  614. array(
  615. 'alias' => 'User',
  616. 'className' => 'User',
  617. 'foreignKey' => 'user_id',
  618. ),
  619. )
  620. );
  621. $model = new Model(array('ds' => 'test', 'name' => 'CategoryThread'));
  622. $this->Task->expects($this->any())->method('in')
  623. ->will($this->onConsecutiveCalls('n', 'y', 'n', 'n', 'n'));
  624. $result = $this->Task->confirmAssociations($model, $associations);
  625. $this->assertTrue(empty($result['hasOne']));
  626. $result = $this->Task->confirmAssociations($model, $associations);
  627. $this->assertTrue(empty($result['hasMany']));
  628. $this->assertTrue(empty($result['hasOne']));
  629. }
  630. /**
  631. * test that inOptions generates questions and only accepts a valid answer
  632. *
  633. * @return void
  634. */
  635. public function testInOptions() {
  636. $this->markTestIncomplete('Not done here yet');
  637. $this->_useMockedOut();
  638. $options = array('one', 'two', 'three');
  639. $this->Task->expects($this->at(0))->method('out')->with('1. one');
  640. $this->Task->expects($this->at(1))->method('out')->with('2. two');
  641. $this->Task->expects($this->at(2))->method('out')->with('3. three');
  642. $this->Task->expects($this->at(3))->method('in')->will($this->returnValue(10));
  643. $this->Task->expects($this->at(4))->method('out')->with('1. one');
  644. $this->Task->expects($this->at(5))->method('out')->with('2. two');
  645. $this->Task->expects($this->at(6))->method('out')->with('3. three');
  646. $this->Task->expects($this->at(7))->method('in')->will($this->returnValue(2));
  647. $result = $this->Task->inOptions($options, 'Pick a number');
  648. $this->assertEquals(1, $result);
  649. }
  650. /**
  651. * test baking validation
  652. *
  653. * @return void
  654. */
  655. public function testBakeValidation() {
  656. $this->markTestIncomplete('Not done here yet');
  657. $validate = array(
  658. 'name' => array(
  659. 'notempty' => 'notEmpty'
  660. ),
  661. 'email' => array(
  662. 'email' => 'email',
  663. ),
  664. 'some_date' => array(
  665. 'date' => 'date'
  666. ),
  667. 'some_time' => array(
  668. 'time' => 'time'
  669. )
  670. );
  671. $result = $this->Task->bake('BakeArticle', compact('validate'));
  672. $this->assertRegExp('/class BakeArticle extends AppModel \{/', $result);
  673. $this->assertRegExp('/\$validate \= array\(/', $result);
  674. $expected = <<< STRINGEND
  675. array(
  676. 'notempty' => array(
  677. 'rule' => array('notEmpty'),
  678. //'message' => 'Your custom message here',
  679. //'allowEmpty' => false,
  680. //'required' => false,
  681. //'last' => false, // Stop validation after this rule
  682. //'on' => 'create', // Limit validation to 'create' or 'update' operations
  683. ),
  684. STRINGEND;
  685. $this->assertRegExp('/' . preg_quote(str_replace("\r\n", "\n", $expected), '/') . '/', $result);
  686. }
  687. /**
  688. * test baking relations
  689. *
  690. * @return void
  691. */
  692. public function testBakeRelations() {
  693. $this->markTestIncomplete('Not done here yet');
  694. $associations = array(
  695. 'belongsTo' => array(
  696. array(
  697. 'alias' => 'SomethingElse',
  698. 'className' => 'SomethingElse',
  699. 'foreignKey' => 'something_else_id',
  700. ),
  701. array(
  702. 'alias' => 'BakeUser',
  703. 'className' => 'BakeUser',
  704. 'foreignKey' => 'bake_user_id',
  705. ),
  706. ),
  707. 'hasOne' => array(
  708. array(
  709. 'alias' => 'OtherModel',
  710. 'className' => 'OtherModel',
  711. 'foreignKey' => 'other_model_id',
  712. ),
  713. ),
  714. 'hasMany' => array(
  715. array(
  716. 'alias' => 'BakeComment',
  717. 'className' => 'BakeComment',
  718. 'foreignKey' => 'parent_id',
  719. ),
  720. ),
  721. 'hasAndBelongsToMany' => array(
  722. array(
  723. 'alias' => 'BakeTag',
  724. 'className' => 'BakeTag',
  725. 'foreignKey' => 'bake_article_id',
  726. 'joinTable' => 'bake_articles_bake_tags',
  727. 'associationForeignKey' => 'bake_tag_id',
  728. ),
  729. )
  730. );
  731. $result = $this->Task->bake('BakeArticle', compact('associations'));
  732. $this->assertContains(' * @property BakeUser $BakeUser', $result);
  733. $this->assertContains(' * @property OtherModel $OtherModel', $result);
  734. $this->assertContains(' * @property BakeComment $BakeComment', $result);
  735. $this->assertContains(' * @property BakeTag $BakeTag', $result);
  736. $this->assertRegExp('/\$hasAndBelongsToMany \= array\(/', $result);
  737. $this->assertRegExp('/\$hasMany \= array\(/', $result);
  738. $this->assertRegExp('/\$belongsTo \= array\(/', $result);
  739. $this->assertRegExp('/\$hasOne \= array\(/', $result);
  740. $this->assertRegExp('/BakeTag/', $result);
  741. $this->assertRegExp('/OtherModel/', $result);
  742. $this->assertRegExp('/SomethingElse/', $result);
  743. $this->assertRegExp('/BakeComment/', $result);
  744. }
  745. /**
  746. * test bake() with a -plugin param
  747. *
  748. * @return void
  749. */
  750. public function testBakeWithPlugin() {
  751. $this->markTestIncomplete('Not done here yet');
  752. $this->Task->plugin = 'ControllerTest';
  753. //fake plugin path
  754. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  755. $path = APP . 'Plugin/ControllerTest/Model/BakeArticle.php';
  756. $this->Task->expects($this->once())->method('createFile')
  757. ->with($path, $this->stringContains('BakeArticle extends ControllerTestAppModel'));
  758. $result = $this->Task->bake('BakeArticle', array(), array());
  759. $this->assertContains("App::uses('ControllerTestAppModel', 'ControllerTest.Model');", $result);
  760. $this->assertEquals(count(ClassRegistry::keys()), 0);
  761. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  762. }
  763. /**
  764. * test bake() for models with behaviors
  765. *
  766. * @return void
  767. */
  768. public function testBakeWithBehaviors() {
  769. $this->markTestIncomplete('Not done here yet');
  770. $result = $this->Task->bake('NumberTree', array('actsAs' => array('Tree', 'PluginName.Sluggable')));
  771. $expected = <<<TEXT
  772. /**
  773. * Behaviors
  774. *
  775. * @var array
  776. */
  777. public \$actsAs = array(
  778. 'Tree',
  779. 'PluginName.Sluggable',
  780. );
  781. TEXT;
  782. $this->assertTextContains($expected, $result);
  783. }
  784. /**
  785. * test that execute passes runs bake depending with named model.
  786. *
  787. * @return void
  788. */
  789. public function testExecuteWithNamedModel() {
  790. $this->markTestIncomplete('Not done here yet');
  791. $this->Task->connection = 'test';
  792. $this->Task->path = '/my/path/';
  793. $this->Task->args = array('BakeArticle');
  794. $filename = '/my/path/BakeArticle.php';
  795. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  796. $this->Task->expects($this->once())->method('createFile')
  797. ->with($filename, $this->stringContains('class BakeArticle extends AppModel'));
  798. $this->Task->execute();
  799. $this->assertEquals(count(ClassRegistry::keys()), 0);
  800. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  801. }
  802. /**
  803. * data provider for testExecuteWithNamedModelVariations
  804. *
  805. * @return void
  806. */
  807. public static function nameVariations() {
  808. return array(
  809. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  810. );
  811. }
  812. /**
  813. * test that execute passes with different inflections of the same name.
  814. *
  815. * @dataProvider nameVariations
  816. * @return void
  817. */
  818. public function testExecuteWithNamedModelVariations($name) {
  819. $this->markTestIncomplete('Not done here yet');
  820. $this->Task->connection = 'test';
  821. $this->Task->path = '/my/path/';
  822. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  823. $this->Task->args = array($name);
  824. $filename = '/my/path/BakeArticle.php';
  825. $this->Task->expects($this->at(0))->method('createFile')
  826. ->with($filename, $this->stringContains('class BakeArticle extends AppModel'));
  827. $this->Task->execute();
  828. }
  829. /**
  830. * test that execute with a model name picks up hasMany associations.
  831. *
  832. * @return void
  833. */
  834. public function testExecuteWithNamedModelHasManyCreated() {
  835. $this->markTestIncomplete('Not done here yet');
  836. $this->Task->connection = 'test';
  837. $this->Task->path = '/my/path/';
  838. $this->Task->args = array('BakeArticle');
  839. $filename = '/my/path/BakeArticle.php';
  840. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(1));
  841. $this->Task->expects($this->at(0))->method('createFile')
  842. ->with($filename, $this->stringContains("'BakeComment' => array("));
  843. $this->Task->execute();
  844. }
  845. /**
  846. * test that execute runs all() when args[0] = all
  847. *
  848. * @return void
  849. */
  850. public function testExecuteIntoAll() {
  851. $this->markTestIncomplete('Not done here yet');
  852. $count = count($this->Task->listAll('test'));
  853. if ($count != count($this->fixtures)) {
  854. $this->markTestSkipped('Additional tables detected.');
  855. }
  856. $this->Task->connection = 'test';
  857. $this->Task->path = '/my/path/';
  858. $this->Task->args = array('all');
  859. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  860. $this->Task->Fixture->expects($this->exactly(5))->method('bake');
  861. $this->Task->Test->expects($this->exactly(5))->method('bake');
  862. $filename = '/my/path/BakeArticle.php';
  863. $this->Task->expects($this->at(1))->method('createFile')
  864. ->with($filename, $this->stringContains('class BakeArticle'));
  865. $filename = '/my/path/BakeArticlesBakeTag.php';
  866. $this->Task->expects($this->at(2))->method('createFile')
  867. ->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
  868. $filename = '/my/path/BakeComment.php';
  869. $this->Task->expects($this->at(3))->method('createFile')
  870. ->with($filename, $this->stringContains('class BakeComment'));
  871. $filename = '/my/path/BakeComment.php';
  872. $this->Task->expects($this->at(3))->method('createFile')
  873. ->with($filename, $this->stringContains('public $primaryKey = \'otherid\';'));
  874. $filename = '/my/path/BakeTag.php';
  875. $this->Task->expects($this->at(4))->method('createFile')
  876. ->with($filename, $this->stringContains('class BakeTag'));
  877. $filename = '/my/path/BakeTag.php';
  878. $this->Task->expects($this->at(4))->method('createFile')
  879. ->with($filename, $this->logicalNot($this->stringContains('public $primaryKey')));
  880. $filename = '/my/path/CategoryThread.php';
  881. $this->Task->expects($this->at(5))->method('createFile')
  882. ->with($filename, $this->stringContains('class CategoryThread'));
  883. $this->Task->execute();
  884. $this->assertEquals(count(ClassRegistry::keys()), 0);
  885. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  886. }
  887. /**
  888. * test that odd tablenames aren't inflected back from modelname
  889. *
  890. * @return void
  891. */
  892. public function testExecuteIntoAllOddTables() {
  893. $this->markTestIncomplete('Not done here yet');
  894. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  895. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  896. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  897. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
  898. array($out, $out, $in)
  899. );
  900. $this->_setupOtherMocks();
  901. $this->Task->connection = 'test';
  902. $this->Task->path = '/my/path/';
  903. $this->Task->args = array('all');
  904. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  905. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
  906. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  907. $this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
  908. $this->Task->expects($this->at(3))->method('bake')->with($object, false)->will($this->returnValue(true));
  909. $this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
  910. $this->Task->execute();
  911. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  912. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  913. $this->Task = $this->getMock('ModelTask',
  914. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'doActsAs', 'createFile'),
  915. array($out, $out, $in)
  916. );
  917. $this->_setupOtherMocks();
  918. $this->Task->connection = 'test';
  919. $this->Task->path = '/my/path/';
  920. $this->Task->args = array('all');
  921. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  922. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
  923. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  924. $this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
  925. $this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
  926. $this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
  927. $this->Task->expects($this->once())->method('doActsAs')->will($this->returnValue(array()));
  928. $filename = '/my/path/BakeOdd.php';
  929. $this->Task->expects($this->once())->method('createFile')
  930. ->with($filename, $this->stringContains('class BakeOdd'));
  931. $filename = '/my/path/BakeOdd.php';
  932. $this->Task->expects($this->once())->method('createFile')
  933. ->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
  934. $this->Task->execute();
  935. }
  936. /**
  937. * test that odd tablenames aren't inflected back from modelname
  938. *
  939. * @return void
  940. */
  941. public function testExecuteIntoBakeOddTables() {
  942. $this->markTestIncomplete('Not done here yet');
  943. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  944. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  945. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  946. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
  947. array($out, $out, $in)
  948. );
  949. $this->_setupOtherMocks();
  950. $this->Task->connection = 'test';
  951. $this->Task->path = '/my/path/';
  952. $this->Task->args = array('BakeOdd');
  953. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  954. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  955. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  956. $this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
  957. $this->Task->expects($this->once())->method('bake')->with($object, false)->will($this->returnValue(true));
  958. $this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
  959. $this->Task->execute();
  960. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  961. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  962. $this->Task = $this->getMock('ModelTask',
  963. array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'doActsAs', 'createFile'),
  964. array($out, $out, $in)
  965. );
  966. $this->_setupOtherMocks();
  967. $this->Task->connection = 'test';
  968. $this->Task->path = '/my/path/';
  969. $this->Task->args = array('BakeOdd');
  970. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  971. $this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
  972. $object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
  973. $this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
  974. $this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
  975. $this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
  976. $this->Task->expects($this->once())->method('doActsAs')->will($this->returnValue(array()));
  977. $filename = '/my/path/BakeOdd.php';
  978. $this->Task->expects($this->once())->method('createFile')
  979. ->with($filename, $this->stringContains('class BakeOdd'));
  980. $filename = '/my/path/BakeOdd.php';
  981. $this->Task->expects($this->once())->method('createFile')
  982. ->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
  983. $this->Task->execute();
  984. }
  985. /**
  986. * test that skipTables changes how all() works.
  987. *
  988. * @return void
  989. */
  990. public function testSkipTablesAndAll() {
  991. $this->markTestIncomplete('Not done here yet');
  992. $count = count($this->Task->listAll('test'));
  993. if ($count != count($this->fixtures)) {
  994. $this->markTestSkipped('Additional tables detected.');
  995. }
  996. $this->Task->connection = 'test';
  997. $this->Task->path = '/my/path/';
  998. $this->Task->args = array('all');
  999. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  1000. $this->Task->skipTables = array('bake_tags');
  1001. $this->Task->Fixture->expects($this->exactly(4))->method('bake');
  1002. $this->Task->Test->expects($this->exactly(4))->method('bake');
  1003. $filename = '/my/path/BakeArticle.php';
  1004. $this->Task->expects($this->at(1))->method('createFile')
  1005. ->with($filename, $this->stringContains('class BakeArticle'));
  1006. $filename = '/my/path/BakeArticlesBakeTag.php';
  1007. $this->Task->expects($this->at(2))->method('createFile')
  1008. ->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
  1009. $filename = '/my/path/BakeComment.php';
  1010. $this->Task->expects($this->at(3))->method('createFile')
  1011. ->with($filename, $this->stringContains('class BakeComment'));
  1012. $filename = '/my/path/CategoryThread.php';
  1013. $this->Task->expects($this->at(4))->method('createFile')
  1014. ->with($filename, $this->stringContains('class CategoryThread'));
  1015. $this->Task->execute();
  1016. }
  1017. /**
  1018. * test the interactive side of bake.
  1019. *
  1020. * @return void
  1021. */
  1022. public function testExecuteIntoInteractive() {
  1023. $this->markTestIncomplete('Not done here yet');
  1024. $tables = $this->Task->listAll('test');
  1025. $article = array_search('bake_articles', $tables) + 1;
  1026. $this->Task->connection = 'test';
  1027. $this->Task->path = '/my/path/';
  1028. $this->Task->interactive = true;
  1029. $this->Task->expects($this->any())->method('in')
  1030. ->will($this->onConsecutiveCalls(
  1031. $article, // article
  1032. 'n', // no validation
  1033. 'y', // associations
  1034. 'y', // comment relation
  1035. 'y', // user relation
  1036. 'y', // tag relation
  1037. 'n', // additional assocs
  1038. 'y' // looks good?
  1039. ));
  1040. $this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
  1041. $this->Task->Test->expects($this->once())->method('bake');
  1042. $this->Task->Fixture->expects($this->once())->method('bake');
  1043. $filename = '/my/path/BakeArticle.php';
  1044. $this->Task->expects($this->once())->method('createFile')
  1045. ->with($filename, $this->stringContains('class BakeArticle'));
  1046. $this->Task->execute();
  1047. $this->assertEquals(count(ClassRegistry::keys()), 0);
  1048. $this->assertEquals(count(ClassRegistry::mapKeys()), 0);
  1049. }
  1050. /**
  1051. * test using bake interactively with a table that does not exist.
  1052. *
  1053. * @return void
  1054. */
  1055. public function testExecuteWithNonExistantTableName() {
  1056. $this->markTestIncomplete('Not done here yet');
  1057. $this->Task->connection = 'test';
  1058. $this->Task->path = '/my/path/';
  1059. $this->Task->expects($this->any())->method('in')
  1060. ->will($this->onConsecutiveCalls(
  1061. 'Foobar', // Or type in the name of the model
  1062. 'y', // Do you want to use this table
  1063. 'n' // Doesn't exist, continue anyway?
  1064. ));
  1065. $this->Task->execute();
  1066. }
  1067. /**
  1068. * test using bake interactively with a table that does not exist.
  1069. *
  1070. * @return void
  1071. */
  1072. public function testForcedExecuteWithNonExistantTableName() {
  1073. $this->markTestIncomplete('Not done here yet');
  1074. $this->Task->connection = 'test';
  1075. $this->Task->path = '/my/path/';
  1076. $this->Task->expects($this->any())->method('in')
  1077. ->will($this->onConsecutiveCalls(
  1078. 'Foobar', // Or type in the name of the model
  1079. 'y', // Do you want to use this table
  1080. 'y', // Doesn't exist, continue anyway?
  1081. 'id', // Primary key
  1082. 'y' // Looks good?
  1083. ));
  1084. $this->Task->execute();
  1085. }
  1086. }