ModelTaskTest.php 40 KB

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