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