FixtureTaskTest.php 14 KB

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