FixtureTaskTest.php 11 KB

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