FixtureTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * FixtureTask Test case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Console.Command.Task
  16. * @since CakePHP(tm) v 1.3
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ShellDispatcher', 'Console');
  20. App::uses('Shell', 'Console');
  21. App::uses('ConsoleOutput', 'Console');
  22. App::uses('ConsoleInput', 'Console');
  23. App::uses('ModelTask', 'Console/Command/Task');
  24. App::uses('FixtureTask', 'Console/Command/Task');
  25. App::uses('TemplateTask', 'Console/Command/Task');
  26. App::uses('DbConfigTask', 'Console/Command/Task');
  27. /**
  28. * FixtureTaskTest class
  29. *
  30. * @package Cake.Test.Case.Console.Command.Task
  31. */
  32. class FixtureTaskTest extends CakeTestCase {
  33. /**
  34. * fixtures
  35. *
  36. * @var array
  37. */
  38. public $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test', 'core.user');
  39. /**
  40. * Whether backup global state for each test method or not
  41. *
  42. * @var bool false
  43. */
  44. public $backupGlobals = false;
  45. /**
  46. * setUp method
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  53. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  54. $this->Task = $this->getMock('FixtureTask',
  55. array('in', 'err', 'createFile', '_stop', 'clear'),
  56. array($out, $out, $in)
  57. );
  58. $this->Task->Model = $this->getMock('ModelTask',
  59. array('in', 'out', 'err', 'createFile', 'getName', 'getTable', 'listAll'),
  60. array($out, $out, $in)
  61. );
  62. $this->Task->Template = new TemplateTask($out, $out, $in);
  63. $this->Task->DbConfig = $this->getMock('DbConfigTask', array(), array($out, $out, $in));
  64. $this->Task->Template->initialize();
  65. }
  66. /**
  67. * tearDown method
  68. *
  69. * @return void
  70. */
  71. public function tearDown() {
  72. parent::tearDown();
  73. unset($this->Task);
  74. }
  75. /**
  76. * test that initialize sets the path
  77. *
  78. * @return void
  79. */
  80. public function testConstruct() {
  81. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  82. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  83. $Task = new FixtureTask($out, $out, $in);
  84. $this->assertEquals(APP . 'Test' . DS . 'Fixture' . DS, $Task->path);
  85. }
  86. /**
  87. * test import option array generation
  88. *
  89. * @return void
  90. */
  91. public function testImportOptionsSchemaRecords() {
  92. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  93. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  94. $result = $this->Task->importOptions('Article');
  95. $expected = array('schema' => 'Article', 'records' => true);
  96. $this->assertEquals($expected, $result);
  97. }
  98. /**
  99. * test importOptions choosing nothing.
  100. *
  101. * @return void
  102. */
  103. public function testImportOptionsNothing() {
  104. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  105. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  106. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n'));
  107. $result = $this->Task->importOptions('Article');
  108. $expected = array();
  109. $this->assertEquals($expected, $result);
  110. }
  111. /**
  112. * test importOptions choosing from Table.
  113. *
  114. * @return void
  115. */
  116. public function testImportOptionsTable() {
  117. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  118. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  119. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
  120. $result = $this->Task->importOptions('Article');
  121. $expected = array('fromTable' => true);
  122. $this->assertEquals($expected, $result);
  123. }
  124. /**
  125. * test generating a fixture with database conditions.
  126. *
  127. * @return void
  128. */
  129. public function testImportRecordsFromDatabaseWithConditionsPoo() {
  130. $this->Task->interactive = true;
  131. $this->Task->expects($this->at(0))->method('in')
  132. ->will($this->returnValue('WHERE 1=1'));
  133. $this->Task->connection = 'test';
  134. $this->Task->path = '/my/path/';
  135. $result = $this->Task->bake('Article', false, array(
  136. 'fromTable' => true, 'schema' => 'Article', 'records' => false
  137. ));
  138. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  139. $this->assertContains('public $records', $result);
  140. $this->assertContains('public $import', $result);
  141. $this->assertContains("'title' => 'First Article'", $result, 'Missing import data %s');
  142. $this->assertContains('Second Article', $result, 'Missing import data %s');
  143. $this->assertContains('Third Article', $result, 'Missing import data %s');
  144. }
  145. /**
  146. * test that connection gets set to the import options when a different connection is used.
  147. *
  148. * @return void
  149. */
  150. public function testImportOptionsAlternateConnection() {
  151. $this->Task->connection = 'test';
  152. $result = $this->Task->bake('Article', false, array('schema' => 'Article'));
  153. $this->assertContains("'connection' => 'test'", $result);
  154. }
  155. /**
  156. * Ensure that fixture data doesn't get overly escaped.
  157. *
  158. * @return void
  159. */
  160. public function testImportRecordsNoEscaping() {
  161. $db = ConnectionManager::getDataSource('test');
  162. if ($db instanceof Sqlserver) {
  163. $this->markTestSkipped('This test does not run on SQLServer');
  164. }
  165. $Article = ClassRegistry::init('Article');
  166. $Article->updateAll(array('body' => "'Body \"value\"'"));
  167. $this->Task->interactive = true;
  168. $this->Task->expects($this->at(0))
  169. ->method('in')
  170. ->will($this->returnValue('WHERE 1=1 LIMIT 10'));
  171. $this->Task->connection = 'test';
  172. $this->Task->path = '/my/path/';
  173. $result = $this->Task->bake('Article', false, array(
  174. 'fromTable' => true,
  175. 'schema' => 'Article',
  176. 'records' => false
  177. ));
  178. $this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
  179. }
  180. /**
  181. * test that execute passes runs bake depending with named model.
  182. *
  183. *
  184. * @return void
  185. */
  186. public function testExecuteWithNamedModel() {
  187. $this->Task->connection = 'test';
  188. $this->Task->path = '/my/path/';
  189. $this->Task->args = array('article');
  190. $filename = '/my/path/ArticleFixture.php';
  191. $this->Task->expects($this->at(0))->method('createFile')
  192. ->with($filename, $this->stringContains('class ArticleFixture'));
  193. $this->Task->execute();
  194. }
  195. /**
  196. * test that execute runs all() when args[0] = all
  197. *
  198. * @return void
  199. */
  200. public function testExecuteIntoAll() {
  201. $this->Task->connection = 'test';
  202. $this->Task->path = '/my/path/';
  203. $this->Task->args = array('all');
  204. $this->Task->Model->expects($this->any())
  205. ->method('listAll')
  206. ->will($this->returnValue(array('articles', 'comments')));
  207. $filename = '/my/path/ArticleFixture.php';
  208. $this->Task->expects($this->at(0))
  209. ->method('createFile')
  210. ->with($filename, $this->stringContains('class ArticleFixture'));
  211. $filename = '/my/path/CommentFixture.php';
  212. $this->Task->expects($this->at(1))
  213. ->method('createFile')
  214. ->with($filename, $this->stringContains('class CommentFixture'));
  215. $this->Task->execute();
  216. }
  217. /**
  218. * test using all() with -count and -records
  219. *
  220. * @return void
  221. */
  222. public function testAllWithCountAndRecordsFlags() {
  223. $this->Task->connection = 'test';
  224. $this->Task->path = '/my/path/';
  225. $this->Task->args = array('all');
  226. $this->Task->params = array('count' => 10, 'records' => true);
  227. $this->Task->Model->expects($this->any())->method('listAll')
  228. ->will($this->returnValue(array('Articles', 'comments')));
  229. $filename = '/my/path/ArticleFixture.php';
  230. $this->Task->expects($this->at(0))->method('createFile')
  231. ->with($filename, $this->stringContains("'title' => 'Third Article'"));
  232. $filename = '/my/path/CommentFixture.php';
  233. $this->Task->expects($this->at(1))->method('createFile')
  234. ->with($filename, $this->stringContains("'comment' => 'First Comment for First Article'"));
  235. $this->Task->expects($this->exactly(2))->method('createFile');
  236. $this->Task->all();
  237. }
  238. /**
  239. * test interactive mode of execute
  240. *
  241. * @return void
  242. */
  243. public function testExecuteInteractive() {
  244. $this->Task->connection = 'test';
  245. $this->Task->path = '/my/path/';
  246. $this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
  247. $this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
  248. $this->Task->Model->expects($this->any())->method('getTable')
  249. ->with('Article')
  250. ->will($this->returnValue('articles'));
  251. $filename = '/my/path/ArticleFixture.php';
  252. $this->Task->expects($this->once())->method('createFile')
  253. ->with($filename, $this->stringContains('class ArticleFixture'));
  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. $this->Task->path = '/my/path/';
  264. $result = $this->Task->bake('Article');
  265. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  266. $this->assertContains('public $fields', $result);
  267. $this->assertContains('public $records', $result);
  268. $this->assertNotContains('public $import', $result);
  269. $result = $this->Task->bake('Article', 'comments');
  270. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  271. $this->assertContains('public $table = \'comments\';', $result);
  272. $this->assertContains('public $fields = array(', $result);
  273. $result = $this->Task->bake('Article', 'comments', array('records' => true));
  274. $this->assertContains("public \$import = array('records' => true, 'connection' => 'test');", $result);
  275. $this->assertNotContains('public $records', $result);
  276. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
  277. $this->assertContains("public \$import = array('model' => 'Article', 'connection' => 'test');", $result);
  278. $this->assertNotContains('public $fields', $result);
  279. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
  280. $this->assertContains("public \$import = array('model' => 'Article', 'records' => true, 'connection' => 'test');", $result);
  281. $this->assertNotContains('public $fields', $result);
  282. $this->assertNotContains('public $records', $result);
  283. }
  284. /**
  285. * test record generation with float and binary types
  286. *
  287. * @return void
  288. */
  289. public function testRecordGenerationForBinaryAndFloat() {
  290. $this->Task->connection = 'test';
  291. $this->Task->path = '/my/path/';
  292. $result = $this->Task->bake('Article', 'datatypes');
  293. $this->assertContains("'float_field' => 1", $result);
  294. $this->assertContains("'bool' => 1", $result);
  295. $result = $this->Task->bake('Article', 'binary_tests');
  296. $this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);
  297. }
  298. /**
  299. * Test that file generation includes headers and correct path for plugins.
  300. *
  301. * @return void
  302. */
  303. public function testGenerateFixtureFile() {
  304. $this->Task->connection = 'test';
  305. $this->Task->path = '/my/path/';
  306. $filename = '/my/path/ArticleFixture.php';
  307. $this->Task->expects($this->at(0))->method('createFile')
  308. ->with($filename, $this->stringContains('ArticleFixture'));
  309. $this->Task->expects($this->at(1))->method('createFile')
  310. ->with($filename, $this->stringContains('<?php'));
  311. $result = $this->Task->generateFixtureFile('Article', array());
  312. $result = $this->Task->generateFixtureFile('Article', array());
  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->path = '/my/path/';
  322. $this->Task->plugin = 'TestFixture';
  323. $filename = APP . 'Plugin' . DS . 'TestFixture' . DS . 'Test' . DS . 'Fixture' . DS . 'ArticleFixture.php';
  324. //fake plugin path
  325. CakePlugin::load('TestFixture', array('path' => APP . 'Plugin' . DS . 'TestFixture' . DS));
  326. $this->Task->expects($this->at(0))->method('createFile')
  327. ->with($filename, $this->stringContains('class Article'));
  328. $this->Task->generateFixtureFile('Article', array());
  329. CakePlugin::unload();
  330. }
  331. }