FixtureTaskTest.php 12 KB

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