FixtureTaskTest.php 12 KB

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