FixtureTaskTest.php 12 KB

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