ModelTaskTest.php 40 KB

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