FixtureTaskTest.php 12 KB

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