FixtureTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 1.3.0
  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(ROOT . '/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 = ['schema' => true];
  93. $result = $this->Task->importOptions('Articles');
  94. $expected = ['schema' => 'Articles'];
  95. $this->assertEquals($expected, $result);
  96. }
  97. /**
  98. * test importOptions with records.
  99. *
  100. * @return void
  101. */
  102. public function testImportOptionsWithRecords() {
  103. $this->Task->params = array('records' => true);
  104. $result = $this->Task->importOptions('Article');
  105. $expected = array('fromTable' => true, 'records' => true);
  106. $this->assertEquals($expected, $result);
  107. }
  108. /**
  109. * test generating a fixture with database conditions.
  110. *
  111. * @return void
  112. */
  113. public function testImportRecordsFromDatabaseWithConditionsPoo() {
  114. $this->Task->connection = 'test';
  115. $this->Task->path = '/my/path/';
  116. $result = $this->Task->bake('Articles', false, array(
  117. 'fromTable' => true,
  118. 'schema' => 'Articles',
  119. 'records' => false
  120. ));
  121. $this->assertContains('namespace App\Test\Fixture;', $result);
  122. $this->assertContains('use Cake\TestSuite\Fixture\TestFixture;', $result);
  123. $this->assertContains('class ArticleFixture extends TestFixture', $result);
  124. $this->assertContains('public $records', $result);
  125. $this->assertContains('public $import', $result);
  126. $this->assertContains("'title' => 'First Article'", $result, 'Missing import data %s');
  127. $this->assertContains('Second Article', $result, 'Missing import data %s');
  128. $this->assertContains('Third Article', $result, 'Missing import data %s');
  129. }
  130. /**
  131. * test that connection gets set to the import options when a different connection is used.
  132. *
  133. * @return void
  134. */
  135. public function testImportOptionsAlternateConnection() {
  136. $this->Task->connection = 'test';
  137. $result = $this->Task->bake('Article', false, array('schema' => 'Article'));
  138. $this->assertContains("'connection' => 'test'", $result);
  139. }
  140. /**
  141. * Ensure that fixture data doesn't get overly escaped.
  142. *
  143. * @return void
  144. */
  145. public function testImportRecordsNoEscaping() {
  146. $db = ConnectionManager::get('test');
  147. if ($db instanceof Sqlserver) {
  148. $this->markTestSkipped('This test does not run on SQLServer');
  149. }
  150. $articles = TableRegistry::get('Articles');
  151. $articles->updateAll(['body' => "Body \"value\""], []);
  152. $this->Task->connection = 'test';
  153. $this->Task->path = '/my/path/';
  154. $result = $this->Task->bake('Article', false, array(
  155. 'fromTable' => true,
  156. 'schema' => 'Article',
  157. 'records' => false
  158. ));
  159. $this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
  160. }
  161. /**
  162. * test that execute passes runs bake depending with named model.
  163. *
  164. *
  165. * @return void
  166. */
  167. public function testExecuteWithNamedModel() {
  168. $this->Task->connection = 'test';
  169. $this->Task->path = '/my/path/';
  170. $this->Task->args = array('article');
  171. $filename = '/my/path/ArticleFixture.php';
  172. $this->Task->expects($this->at(0))->method('createFile')
  173. ->with($filename, $this->stringContains('class ArticleFixture'));
  174. $this->Task->execute();
  175. }
  176. /**
  177. * test that execute runs all() when args[0] = all
  178. *
  179. * @return void
  180. */
  181. public function testExecuteIntoAll() {
  182. $this->Task->connection = 'test';
  183. $this->Task->path = '/my/path/';
  184. $this->Task->args = array('all');
  185. $this->Task->Model->expects($this->any())
  186. ->method('listAll')
  187. ->will($this->returnValue(array('articles', 'comments')));
  188. $filename = '/my/path/ArticleFixture.php';
  189. $this->Task->expects($this->at(0))
  190. ->method('createFile')
  191. ->with($filename, $this->stringContains('class ArticleFixture'));
  192. $filename = '/my/path/CommentFixture.php';
  193. $this->Task->expects($this->at(1))
  194. ->method('createFile')
  195. ->with($filename, $this->stringContains('class CommentFixture'));
  196. $this->Task->execute();
  197. }
  198. /**
  199. * test using all() with -count and -records
  200. *
  201. * @return void
  202. */
  203. public function testAllWithCountAndRecordsFlags() {
  204. $this->Task->connection = 'test';
  205. $this->Task->path = '/my/path/';
  206. $this->Task->args = ['all'];
  207. $this->Task->params = ['count' => 10, 'records' => true];
  208. $this->Task->Model->expects($this->any())->method('listAll')
  209. ->will($this->returnValue(array('Articles', 'comments')));
  210. $filename = '/my/path/ArticleFixture.php';
  211. $this->Task->expects($this->at(0))
  212. ->method('createFile')
  213. ->with($filename, $this->stringContains("'title' => 'Third Article'"));
  214. $filename = '/my/path/CommentFixture.php';
  215. $this->Task->expects($this->at(1))
  216. ->method('createFile')
  217. ->with($filename, $this->stringContains("'comment' => 'First Comment for First Article'"));
  218. $this->Task->expects($this->exactly(2))->method('createFile');
  219. $this->Task->all();
  220. }
  221. /**
  222. * test using all() with -schema
  223. *
  224. * @return void
  225. */
  226. public function testAllWithSchemaImport() {
  227. $this->Task->connection = 'test';
  228. $this->Task->path = '/my/path/';
  229. $this->Task->args = array('all');
  230. $this->Task->params = array('schema' => true);
  231. $this->Task->Model->expects($this->any())->method('listAll')
  232. ->will($this->returnValue(array('Articles', 'comments')));
  233. $filename = '/my/path/ArticleFixture.php';
  234. $this->Task->expects($this->at(0))->method('createFile')
  235. ->with($filename, $this->stringContains("public \$import = ['model' => 'Articles'"));
  236. $filename = '/my/path/CommentFixture.php';
  237. $this->Task->expects($this->at(1))->method('createFile')
  238. ->with($filename, $this->stringContains("public \$import = ['model' => 'Comments'"));
  239. $this->Task->expects($this->exactly(2))->method('createFile');
  240. $this->Task->all();
  241. }
  242. /**
  243. * test interactive mode of execute
  244. *
  245. * @return void
  246. */
  247. public function testExecuteNoArgs() {
  248. $this->Task->connection = 'test';
  249. $this->Task->path = '/my/path/';
  250. $this->Task->Model->expects($this->any())
  251. ->method('listAll')
  252. ->will($this->returnValue(['articles', 'comments']));
  253. $filename = '/my/path/ArticleFixture.php';
  254. $this->Task->expects($this->never())
  255. ->method('createFile');
  256. $this->Task->execute();
  257. }
  258. /**
  259. * Test that bake works
  260. *
  261. * @return void
  262. */
  263. public function testBake() {
  264. $this->Task->connection = 'test';
  265. $this->Task->path = '/my/path/';
  266. $result = $this->Task->bake('Article');
  267. $this->assertContains('class ArticleFixture extends TestFixture', $result);
  268. $this->assertContains('public $fields', $result);
  269. $this->assertContains('public $records', $result);
  270. $this->assertNotContains('public $import', $result);
  271. $result = $this->Task->bake('Article', 'comments');
  272. $this->assertContains('class ArticleFixture extends TestFixture', $result);
  273. $this->assertContains('public $table = \'comments\';', $result);
  274. $this->assertContains('public $fields = [', $result);
  275. $result = $this->Task->bake('Article', 'comments', array('records' => true));
  276. $this->assertContains("public \$import = ['records' => true, 'connection' => 'test'];", $result);
  277. $this->assertNotContains('public $records', $result);
  278. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
  279. $this->assertContains("public \$import = ['model' => 'Article', 'connection' => 'test'];", $result);
  280. $this->assertNotContains('public $fields', $result);
  281. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
  282. $this->assertContains("public \$import = ['model' => 'Article', 'records' => true, 'connection' => 'test'];", $result);
  283. $this->assertNotContains('public $fields', $result);
  284. $this->assertNotContains('public $records', $result);
  285. }
  286. /**
  287. * test record generation with float and binary types
  288. *
  289. * @return void
  290. */
  291. public function testRecordGenerationForBinaryAndFloat() {
  292. $this->Task->connection = 'test';
  293. $this->Task->path = '/my/path/';
  294. $result = $this->Task->bake('Article', 'datatypes');
  295. $this->assertContains("'float_field' => 1", $result);
  296. $this->assertContains("'bool' => 1", $result);
  297. $result = $this->Task->bake('Article', 'binary_tests');
  298. $this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);
  299. }
  300. /**
  301. * Test that file generation includes headers and correct path for plugins.
  302. *
  303. * @return void
  304. */
  305. public function testGenerateFixtureFile() {
  306. $this->Task->connection = 'test';
  307. $this->Task->path = '/my/path/';
  308. $filename = '/my/path/ArticleFixture.php';
  309. $this->Task->expects($this->at(0))->method('createFile')
  310. ->with($filename, $this->stringContains('ArticleFixture'));
  311. $this->Task->expects($this->at(1))->method('createFile')
  312. ->with($filename, $this->stringContains('<?php'));
  313. $this->Task->generateFixtureFile('Article', array());
  314. $this->Task->generateFixtureFile('Article', array());
  315. }
  316. /**
  317. * test generating files into plugins.
  318. *
  319. * @return void
  320. */
  321. public function testGeneratePluginFixtureFile() {
  322. $this->Task->connection = 'test';
  323. $this->Task->path = '/my/path/';
  324. $this->Task->plugin = 'TestFixture';
  325. $filename = APP . 'Plugin/TestFixture/Test/Fixture/ArticleFixture.php';
  326. //fake plugin path
  327. Plugin::load('TestFixture', array('path' => APP . 'Plugin/TestFixture/'));
  328. $this->Task->expects($this->at(0))->method('createFile')
  329. ->with($filename, $this->stringContains('class Article'));
  330. $this->Task->generateFixtureFile('Article', array());
  331. Plugin::unload();
  332. }
  333. }