FixtureTaskTest.php 14 KB

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