FixtureTaskTest.php 14 KB

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