FixtureTaskTest.php 12 KB

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