FixtureTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * FixtureTask Test case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, 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($Task->path, APP . 'Test' . DS . 'Fixture' . DS);
  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. function testImportRecordsNoEscaping() {
  161. $Article = ClassRegistry::init('Article');
  162. $Article->updateAll(array('body' => "'Body \"value\"'"));
  163. $this->Task->interactive = true;
  164. $this->Task->expects($this->at(0))
  165. ->method('in')
  166. ->will($this->returnValue('WHERE 1=1 LIMIT 10'));
  167. $this->Task->connection = 'test';
  168. $this->Task->path = '/my/path/';
  169. $result = $this->Task->bake('Article', false, array(
  170. 'fromTable' => true,
  171. 'schema' => 'Article',
  172. 'records' => false
  173. ));
  174. $this->assertContains("'body' => 'Body \"value\"'", $result, 'Data has bad escaping');
  175. }
  176. /**
  177. * test that execute passes runs bake depending with named model.
  178. *
  179. *
  180. * @return void
  181. */
  182. public function testExecuteWithNamedModel() {
  183. $this->Task->connection = 'test';
  184. $this->Task->path = '/my/path/';
  185. $this->Task->args = array('article');
  186. $filename = '/my/path/ArticleFixture.php';
  187. $this->Task->expects($this->at(0))->method('createFile')
  188. ->with($filename, $this->stringContains('class ArticleFixture'));
  189. $this->Task->execute();
  190. }
  191. /**
  192. * test that execute runs all() when args[0] = all
  193. *
  194. * @return void
  195. */
  196. public function testExecuteIntoAll() {
  197. $this->Task->connection = 'test';
  198. $this->Task->path = '/my/path/';
  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 = '/my/path/ArticleFixture.php';
  204. $this->Task->expects($this->at(0))
  205. ->method('createFile')
  206. ->with($filename, $this->stringContains('class ArticleFixture'));
  207. $filename = '/my/path/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->path = '/my/path/';
  221. $this->Task->args = array('all');
  222. $this->Task->params = array('count' => 10, 'records' => true);
  223. $this->Task->Model->expects($this->any())->method('listAll')
  224. ->will($this->returnValue(array('Articles', 'comments')));
  225. $filename = '/my/path/ArticleFixture.php';
  226. $this->Task->expects($this->at(0))->method('createFile')
  227. ->with($filename, $this->stringContains("'title' => 'Third Article'"));
  228. $filename = '/my/path/CommentFixture.php';
  229. $this->Task->expects($this->at(1))->method('createFile')
  230. ->with($filename, $this->stringContains("'comment' => 'First Comment for First Article'"));
  231. $this->Task->expects($this->exactly(2))->method('createFile');
  232. $this->Task->all();
  233. }
  234. /**
  235. * test interactive mode of execute
  236. *
  237. * @return void
  238. */
  239. public function testExecuteInteractive() {
  240. $this->Task->connection = 'test';
  241. $this->Task->path = '/my/path/';
  242. $this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
  243. $this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
  244. $this->Task->Model->expects($this->any())->method('getTable')
  245. ->with('Article')
  246. ->will($this->returnValue('articles'));
  247. $filename = '/my/path/ArticleFixture.php';
  248. $this->Task->expects($this->once())->method('createFile')
  249. ->with($filename, $this->stringContains('class ArticleFixture'));
  250. $this->Task->execute();
  251. }
  252. /**
  253. * Test that bake works
  254. *
  255. * @return void
  256. */
  257. public function testBake() {
  258. $this->Task->connection = 'test';
  259. $this->Task->path = '/my/path/';
  260. $result = $this->Task->bake('Article');
  261. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  262. $this->assertContains('public $fields', $result);
  263. $this->assertContains('public $records', $result);
  264. $this->assertNotContains('public $import', $result);
  265. $result = $this->Task->bake('Article', 'comments');
  266. $this->assertContains('class ArticleFixture extends CakeTestFixture', $result);
  267. $this->assertContains('public $table = \'comments\';', $result);
  268. $this->assertContains('public $fields = array(', $result);
  269. $result = $this->Task->bake('Article', 'comments', array('records' => true));
  270. $this->assertContains("public \$import = array('records' => true, 'connection' => 'test');", $result);
  271. $this->assertNotContains('public $records', $result);
  272. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
  273. $this->assertContains("public \$import = array('model' => 'Article', 'connection' => 'test');", $result);
  274. $this->assertNotContains('public $fields', $result);
  275. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
  276. $this->assertContains("public \$import = array('model' => 'Article', 'records' => true, 'connection' => 'test');", $result);
  277. $this->assertNotContains('public $fields', $result);
  278. $this->assertNotContains('public $records', $result);
  279. }
  280. /**
  281. * test record generation with float and binary types
  282. *
  283. * @return void
  284. */
  285. public function testRecordGenerationForBinaryAndFloat() {
  286. $this->Task->connection = 'test';
  287. $this->Task->path = '/my/path/';
  288. $result = $this->Task->bake('Article', 'datatypes');
  289. $this->assertContains("'float_field' => 1", $result);
  290. $this->assertContains("'bool' => 1", $result);
  291. $result = $this->Task->bake('Article', 'binary_tests');
  292. $this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);
  293. }
  294. /**
  295. * Test that file generation includes headers and correct path for plugins.
  296. *
  297. * @return void
  298. */
  299. public function testGenerateFixtureFile() {
  300. $this->Task->connection = 'test';
  301. $this->Task->path = '/my/path/';
  302. $filename = '/my/path/ArticleFixture.php';
  303. $this->Task->expects($this->at(0))->method('createFile')
  304. ->with($filename, $this->stringContains('ArticleFixture'));
  305. $this->Task->expects($this->at(1))->method('createFile')
  306. ->with($filename, $this->stringContains('<?php'));
  307. $result = $this->Task->generateFixtureFile('Article', array());
  308. $result = $this->Task->generateFixtureFile('Article', array());
  309. }
  310. /**
  311. * test generating files into plugins.
  312. *
  313. * @return void
  314. */
  315. public function testGeneratePluginFixtureFile() {
  316. $this->Task->connection = 'test';
  317. $this->Task->path = '/my/path/';
  318. $this->Task->plugin = 'TestFixture';
  319. $filename = APP . 'Plugin' . DS . 'TestFixture' . DS . 'Test' . DS . 'Fixture' . DS . 'ArticleFixture.php';
  320. //fake plugin path
  321. CakePlugin::load('TestFixture', array('path' => APP . 'Plugin' . DS . 'TestFixture' . DS));
  322. $this->Task->expects($this->at(0))->method('createFile')
  323. ->with($filename, $this->stringContains('class Article'));
  324. $result = $this->Task->generateFixtureFile('Article', array());
  325. CakePlugin::unload();
  326. }
  327. }