FixtureTaskTest.php 12 KB

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