FixtureTaskTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * FixtureTask Test case
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Test.Case.Console.Command.Task
  15. * @since CakePHP(tm) v 1.3
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ShellDispatcher', 'Console');
  19. App::uses('Shell', 'Console');
  20. App::uses('ConsoleOutput', 'Console');
  21. App::uses('ConsoleInput', 'Console');
  22. App::uses('ModelTask', 'Console/Command/Task');
  23. App::uses('FixtureTask', 'Console/Command/Task');
  24. App::uses('TemplateTask', 'Console/Command/Task');
  25. App::uses('DbConfigTask', 'Console/Command/Task');
  26. /**
  27. * FixtureTaskTest class
  28. *
  29. * @package Cake.Test.Case.Console.Command.Task
  30. */
  31. class FixtureTaskTest extends CakeTestCase {
  32. /**
  33. * fixtures
  34. *
  35. * @var array
  36. */
  37. public $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test', 'core.user');
  38. /**
  39. * Whether backup global state for each test method or not
  40. *
  41. * @var boolean
  42. */
  43. public $backupGlobals = false;
  44. /**
  45. * setUp method
  46. *
  47. * @return void
  48. */
  49. public function setUp() {
  50. parent::setUp();
  51. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  52. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  53. $this->Task = $this->getMock('FixtureTask',
  54. array('in', 'err', 'createFile', '_stop', 'clear'),
  55. array($out, $out, $in)
  56. );
  57. $this->Task->Model = $this->getMock('ModelTask',
  58. array('in', 'out', 'err', 'createFile', 'getName', 'getTable', 'listAll'),
  59. array($out, $out, $in)
  60. );
  61. $this->Task->Template = new TemplateTask($out, $out, $in);
  62. $this->Task->DbConfig = $this->getMock('DbConfigTask', array(), array($out, $out, $in));
  63. $this->Task->Template->initialize();
  64. }
  65. /**
  66. * tearDown method
  67. *
  68. * @return void
  69. */
  70. public function tearDown() {
  71. parent::tearDown();
  72. unset($this->Task);
  73. }
  74. /**
  75. * test that initialize sets the path
  76. *
  77. * @return void
  78. */
  79. public function testConstruct() {
  80. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  81. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  82. $Task = new FixtureTask($out, $out, $in);
  83. $this->assertEquals(APP . 'Test' . DS . 'Fixture' . DS, $Task->path);
  84. }
  85. /**
  86. * test import option array generation
  87. *
  88. * @return void
  89. */
  90. public function testImportOptionsSchemaRecords() {
  91. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  92. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  93. $result = $this->Task->importOptions('Article');
  94. $expected = array('schema' => 'Article', 'records' => true);
  95. $this->assertEquals($expected, $result);
  96. }
  97. /**
  98. * test importOptions choosing nothing.
  99. *
  100. * @return void
  101. */
  102. public function testImportOptionsNothing() {
  103. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  104. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  105. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n'));
  106. $result = $this->Task->importOptions('Article');
  107. $expected = array();
  108. $this->assertEquals($expected, $result);
  109. }
  110. /**
  111. * test importOptions with overwriting command line options.
  112. *
  113. * @return void
  114. */
  115. public function testImportOptionsWithCommandLineOptions() {
  116. $this->Task->params = array('schema' => true, 'records' => true);
  117. $result = $this->Task->importOptions('Article');
  118. $expected = array('schema' => 'Article', 'records' => true);
  119. $this->assertEquals($expected, $result);
  120. }
  121. /**
  122. * test importOptions with schema.
  123. *
  124. * @return void
  125. */
  126. public function testImportOptionsWithSchema() {
  127. $this->Task->params = array('schema' => true);
  128. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  129. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  130. $result = $this->Task->importOptions('Article');
  131. $expected = array('schema' => 'Article');
  132. $this->assertEquals($expected, $result);
  133. }
  134. /**
  135. * test importOptions with records.
  136. *
  137. * @return void
  138. */
  139. public function testImportOptionsWithRecords() {
  140. $this->Task->params = array('records' => true);
  141. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  142. $result = $this->Task->importOptions('Article');
  143. $expected = array('records' => true);
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * test importOptions choosing from Table.
  148. *
  149. * @return void
  150. */
  151. public function testImportOptionsTable() {
  152. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  153. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  154. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
  155. $result = $this->Task->importOptions('Article');
  156. $expected = array('fromTable' => true);
  157. $this->assertEquals($expected, $result);
  158. }
  159. /**
  160. * test generating a fixture with database conditions.
  161. *
  162. * @return void
  163. */
  164. public function testImportRecordsFromDatabaseWithConditionsPoo() {
  165. $this->Task->interactive = true;
  166. $this->Task->expects($this->at(0))->method('in')
  167. ->will($this->returnValue('WHERE 1=1'));
  168. $this->Task->connection = 'test';
  169. $this->Task->path = '/my/path/';
  170. $result = $this->Task->bake('Article', false, array(
  171. 'fromTable' => true, 'schema' => 'Article', 'records' => false
  172. ));
  173. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  174. $this->assertContains('public $records', $result);
  175. $this->assertContains('public $import', $result);
  176. $this->assertContains("'title' => 'First Article'", $result, 'Missing import data %s');
  177. $this->assertContains('Second Article', $result, 'Missing import data %s');
  178. $this->assertContains('Third Article', $result, 'Missing import data %s');
  179. }
  180. /**
  181. * test that connection gets set to the import options when a different connection is used.
  182. *
  183. * @return void
  184. */
  185. public function testImportOptionsAlternateConnection() {
  186. $this->Task->connection = 'test';
  187. $result = $this->Task->bake('Article', false, array('schema' => 'Article'));
  188. $this->assertContains("'connection' => 'test'", $result);
  189. }
  190. /**
  191. * Ensure that fixture data doesn't get overly escaped.
  192. *
  193. * @return void
  194. */
  195. public function testImportRecordsNoEscaping() {
  196. $db = ConnectionManager::getDataSource('test');
  197. if ($db instanceof Sqlserver) {
  198. $this->markTestSkipped('This test does not run on SQLServer');
  199. }
  200. $Article = ClassRegistry::init('Article');
  201. $Article->updateAll(array('body' => "'Body \"value\"'"));
  202. $this->Task->interactive = true;
  203. $this->Task->expects($this->at(0))
  204. ->method('in')
  205. ->will($this->returnValue('WHERE 1=1 LIMIT 10'));
  206. $this->Task->connection = 'test';
  207. $this->Task->path = '/my/path/';
  208. $result = $this->Task->bake('Article', false, array(
  209. 'fromTable' => true,
  210. 'schema' => 'Article',
  211. 'records' => false
  212. ));
  213. $this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
  214. }
  215. /**
  216. * test that execute passes runs bake depending with named model.
  217. *
  218. *
  219. * @return void
  220. */
  221. public function testExecuteWithNamedModel() {
  222. $this->Task->connection = 'test';
  223. $this->Task->path = '/my/path/';
  224. $this->Task->args = array('article');
  225. $filename = '/my/path/ArticleFixture.php';
  226. $this->Task->expects($this->at(0))->method('createFile')
  227. ->with($filename, $this->stringContains('class ArticleFixture'));
  228. $this->Task->execute();
  229. }
  230. /**
  231. * test that execute runs all() when args[0] = all
  232. *
  233. * @return void
  234. */
  235. public function testExecuteIntoAll() {
  236. $this->Task->connection = 'test';
  237. $this->Task->path = '/my/path/';
  238. $this->Task->args = array('all');
  239. $this->Task->Model->expects($this->any())
  240. ->method('listAll')
  241. ->will($this->returnValue(array('articles', 'comments')));
  242. $filename = '/my/path/ArticleFixture.php';
  243. $this->Task->expects($this->at(0))
  244. ->method('createFile')
  245. ->with($filename, $this->stringContains('class ArticleFixture'));
  246. $filename = '/my/path/CommentFixture.php';
  247. $this->Task->expects($this->at(1))
  248. ->method('createFile')
  249. ->with($filename, $this->stringContains('class CommentFixture'));
  250. $this->Task->execute();
  251. }
  252. /**
  253. * test using all() with -count and -records
  254. *
  255. * @return void
  256. */
  257. public function testAllWithCountAndRecordsFlags() {
  258. $this->Task->connection = 'test';
  259. $this->Task->path = '/my/path/';
  260. $this->Task->args = array('all');
  261. $this->Task->params = array('count' => 10, 'records' => true);
  262. $this->Task->Model->expects($this->any())->method('listAll')
  263. ->will($this->returnValue(array('Articles', 'comments')));
  264. $filename = '/my/path/ArticleFixture.php';
  265. $this->Task->expects($this->at(0))->method('createFile')
  266. ->with($filename, $this->stringContains("'title' => 'Third Article'"));
  267. $filename = '/my/path/CommentFixture.php';
  268. $this->Task->expects($this->at(1))->method('createFile')
  269. ->with($filename, $this->stringContains("'comment' => 'First Comment for First Article'"));
  270. $this->Task->expects($this->exactly(2))->method('createFile');
  271. $this->Task->all();
  272. }
  273. /**
  274. * test using all() with -schema
  275. *
  276. * @return void
  277. */
  278. public function testAllWithSchemaImport() {
  279. $this->Task->connection = 'test';
  280. $this->Task->path = '/my/path/';
  281. $this->Task->args = array('all');
  282. $this->Task->params = array('schema' => true);
  283. $this->Task->Model->expects($this->any())->method('listAll')
  284. ->will($this->returnValue(array('Articles', 'comments')));
  285. $filename = '/my/path/ArticleFixture.php';
  286. $this->Task->expects($this->at(0))->method('createFile')
  287. ->with($filename, $this->stringContains('public $import = array(\'model\' => \'Article\''));
  288. $filename = '/my/path/CommentFixture.php';
  289. $this->Task->expects($this->at(1))->method('createFile')
  290. ->with($filename, $this->stringContains('public $import = array(\'model\' => \'Comment\''));
  291. $this->Task->expects($this->exactly(2))->method('createFile');
  292. $this->Task->all();
  293. }
  294. /**
  295. * test interactive mode of execute
  296. *
  297. * @return void
  298. */
  299. public function testExecuteInteractive() {
  300. $this->Task->connection = 'test';
  301. $this->Task->path = '/my/path/';
  302. $this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
  303. $this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
  304. $this->Task->Model->expects($this->any())->method('getTable')
  305. ->with('Article')
  306. ->will($this->returnValue('articles'));
  307. $filename = '/my/path/ArticleFixture.php';
  308. $this->Task->expects($this->once())->method('createFile')
  309. ->with($filename, $this->stringContains('class ArticleFixture'));
  310. $this->Task->execute();
  311. }
  312. /**
  313. * Test that bake works
  314. *
  315. * @return void
  316. */
  317. public function testBake() {
  318. $this->Task->connection = 'test';
  319. $this->Task->path = '/my/path/';
  320. $result = $this->Task->bake('Article');
  321. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  322. $this->assertContains('public $fields', $result);
  323. $this->assertContains('public $records', $result);
  324. $this->assertNotContains('public $import', $result);
  325. $result = $this->Task->bake('Article', 'comments');
  326. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  327. $this->assertContains('public $table = \'comments\';', $result);
  328. $this->assertContains('public $fields = array(', $result);
  329. $result = $this->Task->bake('Article', 'comments', array('records' => true));
  330. $this->assertContains("public \$import = array('records' => true, 'connection' => 'test');", $result);
  331. $this->assertNotContains('public $records', $result);
  332. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
  333. $this->assertContains("public \$import = array('model' => 'Article', 'connection' => 'test');", $result);
  334. $this->assertNotContains('public $fields', $result);
  335. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
  336. $this->assertContains("public \$import = array('model' => 'Article', 'records' => true, 'connection' => 'test');", $result);
  337. $this->assertNotContains('public $fields', $result);
  338. $this->assertNotContains('public $records', $result);
  339. }
  340. /**
  341. * test record generation with float and binary types
  342. *
  343. * @return void
  344. */
  345. public function testRecordGenerationForBinaryAndFloat() {
  346. $this->Task->connection = 'test';
  347. $this->Task->path = '/my/path/';
  348. $result = $this->Task->bake('Article', 'datatypes');
  349. $this->assertContains("'float_field' => 1", $result);
  350. $this->assertContains("'bool' => 1", $result);
  351. $result = $this->Task->bake('Article', 'binary_tests');
  352. $this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);
  353. }
  354. /**
  355. * Test that file generation includes headers and correct path for plugins.
  356. *
  357. * @return void
  358. */
  359. public function testGenerateFixtureFile() {
  360. $this->Task->connection = 'test';
  361. $this->Task->path = '/my/path/';
  362. $filename = '/my/path/ArticleFixture.php';
  363. $this->Task->expects($this->at(0))->method('createFile')
  364. ->with($filename, $this->stringContains('ArticleFixture'));
  365. $this->Task->expects($this->at(1))->method('createFile')
  366. ->with($filename, $this->stringContains('<?php'));
  367. $result = $this->Task->generateFixtureFile('Article', array());
  368. $result = $this->Task->generateFixtureFile('Article', array());
  369. }
  370. /**
  371. * test generating files into plugins.
  372. *
  373. * @return void
  374. */
  375. public function testGeneratePluginFixtureFile() {
  376. $this->Task->connection = 'test';
  377. $this->Task->path = '/my/path/';
  378. $this->Task->plugin = 'TestFixture';
  379. $filename = APP . 'Plugin' . DS . 'TestFixture' . DS . 'Test' . DS . 'Fixture' . DS . 'ArticleFixture.php';
  380. //fake plugin path
  381. CakePlugin::load('TestFixture', array('path' => APP . 'Plugin' . DS . 'TestFixture' . DS));
  382. $this->Task->expects($this->at(0))->method('createFile')
  383. ->with($filename, $this->stringContains('class Article'));
  384. $this->Task->generateFixtureFile('Article', array());
  385. CakePlugin::unload();
  386. }
  387. }