FixtureTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * FixtureTask Test case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.tests.cases.console.libs.tasks
  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('FixtureTask', 'Console/Command/Task');
  24. App::uses('TemplateTask', 'Console/Command/Task');
  25. App::uses('DbConfigTask', 'Console/Command/Task');
  26. /**
  27. * FixtureTaskTest class
  28. *
  29. * @package cake.tests.cases.console.libs.tasks
  30. */
  31. class FixtureTaskTest extends CakeTestCase {
  32. /**
  33. * fixtures
  34. *
  35. * @var array
  36. * @access public
  37. */
  38. public $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test');
  39. /**
  40. * setUp method
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  47. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  48. $this->Task = $this->getMock('FixtureTask',
  49. array('in', 'err', 'createFile', '_stop', 'clear'),
  50. array($out, $out, $in)
  51. );
  52. $this->Task->Model = $this->getMock('Shell',
  53. array('in', 'out', 'error', 'createFile', 'getName', 'getTable', 'listAll'),
  54. array($out, $out, $in)
  55. );
  56. $this->Task->Template = new TemplateTask($out, $out, $in);
  57. $this->Task->DbConfig = $this->getMock('DbConfigTask', array(), array($out, $out, $in));
  58. $this->Task->Template->initialize();
  59. }
  60. /**
  61. * tearDown method
  62. *
  63. * @return void
  64. */
  65. public function tearDown() {
  66. parent::tearDown();
  67. unset($this->Task);
  68. }
  69. /**
  70. * test that initialize sets the path
  71. *
  72. * @return void
  73. */
  74. public function testConstruct() {
  75. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  76. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  77. $Task = new FixtureTask($out, $out, $in);
  78. $this->assertEqual($Task->path, APP . 'tests' . DS . 'Fixture' . DS);
  79. }
  80. /**
  81. * test import option array generation
  82. *
  83. * @return void
  84. */
  85. public function testImportOptionsSchemaRecords() {
  86. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  87. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  88. $result = $this->Task->importOptions('Article');
  89. $expected = array('schema' => 'Article', 'records' => true);
  90. $this->assertEqual($result, $expected);
  91. }
  92. /**
  93. * test importOptions choosing nothing.
  94. *
  95. * @return void
  96. */
  97. public function testImportOptionsNothing() {
  98. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  99. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  100. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n'));
  101. $result = $this->Task->importOptions('Article');
  102. $expected = array();
  103. $this->assertEqual($result, $expected);
  104. }
  105. /**
  106. * test importOptions choosing from Table.
  107. *
  108. * @return void
  109. */
  110. public function testImportOptionsTable() {
  111. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  112. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  113. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
  114. $result = $this->Task->importOptions('Article');
  115. $expected = array('fromTable' => true);
  116. $this->assertEqual($result, $expected);
  117. }
  118. /**
  119. * test generating a fixture with database conditions.
  120. *
  121. * @return void
  122. */
  123. public function testImportRecordsFromDatabaseWithConditionsPoo() {
  124. $this->Task->interactive = true;
  125. $this->Task->expects($this->at(0))->method('in')
  126. ->will($this->returnValue('WHERE 1=1 LIMIT 10'));
  127. $this->Task->connection = 'test';
  128. $this->Task->path = '/my/path/';
  129. $result = $this->Task->bake('Article', false, array(
  130. 'fromTable' => true, 'schema' => 'Article', 'records' => false
  131. ));
  132. $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
  133. $this->assertPattern('/public \$records/', $result);
  134. $this->assertPattern('/public \$import/', $result);
  135. $this->assertPattern("/'title' => 'First Article'/", $result, 'Missing import data %s');
  136. $this->assertPattern('/Second Article/', $result, 'Missing import data %s');
  137. $this->assertPattern('/Third Article/', $result, 'Missing import data %s');
  138. }
  139. /**
  140. * test that connection gets set to the import options when a different connection is used.
  141. *
  142. * @return void
  143. */
  144. function testImportOptionsAlternateConnection() {
  145. $this->Task->connection = 'test';
  146. $result = $this->Task->bake('Article', false, array('schema' => 'Article'));
  147. $this->assertPattern("/'connection' => 'test'/", $result);
  148. }
  149. /**
  150. * test that execute passes runs bake depending with named model.
  151. *
  152. * @return void
  153. */
  154. public function testExecuteWithNamedModel() {
  155. $this->Task->connection = 'test';
  156. $this->Task->path = '/my/path/';
  157. $this->Task->args = array('article');
  158. $filename = '/my/path/ArticleFixture.php';
  159. $this->Task->expects($this->at(0))->method('createFile')
  160. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));
  161. $this->Task->execute();
  162. }
  163. /**
  164. * data provider for model name variations.
  165. *
  166. * @return array
  167. */
  168. public static function modelNameProvider() {
  169. return array(
  170. array('article'), array('articles'), array('Articles'), array('Article')
  171. );
  172. }
  173. /**
  174. * test that execute passes runs bake depending with named model.
  175. *
  176. * @dataProvider modelNameProvider
  177. * @return void
  178. */
  179. public function testExecuteWithNamedModelVariations($modelName) {
  180. $this->Task->connection = 'test';
  181. $this->Task->path = '/my/path/';
  182. $this->Task->args = array($modelName);
  183. $filename = '/my/path/ArticleFixture.php';
  184. $this->Task->expects($this->once())->method('createFile')
  185. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));
  186. $this->Task->execute();
  187. }
  188. /**
  189. * test that execute runs all() when args[0] = all
  190. *
  191. * @return void
  192. */
  193. public function testExecuteIntoAll() {
  194. $this->Task->connection = 'test';
  195. $this->Task->path = '/my/path/';
  196. $this->Task->args = array('all');
  197. $this->Task->Model->expects($this->any())->method('listAll')
  198. ->will($this->returnValue(array('articles', 'comments')));
  199. $filename = '/my/path/ArticleFixture.php';
  200. $this->Task->expects($this->at(0))->method('createFile')
  201. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));
  202. $filename = '/my/path/CommentFixture.php';
  203. $this->Task->expects($this->at(1))->method('createFile')
  204. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class CommentFixture/'));
  205. $this->Task->execute();
  206. }
  207. /**
  208. * test using all() with -count and -records
  209. *
  210. * @return void
  211. */
  212. public function testAllWithCountAndRecordsFlags() {
  213. $this->Task->connection = 'test';
  214. $this->Task->path = '/my/path/';
  215. $this->Task->args = array('all');
  216. $this->Task->params = array('count' => 10, 'records' => true);
  217. $this->Task->Model->expects($this->any())->method('listAll')
  218. ->will($this->returnValue(array('articles', 'comments')));
  219. $filename = '/my/path/ArticleFixture.php';
  220. $this->Task->expects($this->at(0))->method('createFile')
  221. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/title\' => \'Third Article\'/'));
  222. $filename = '/my/path/CommentFixture.php';
  223. $this->Task->expects($this->at(1))->method('createFile')
  224. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/comment\' => \'First Comment for First Article/'));
  225. $this->Task->expects($this->exactly(2))->method('createFile');
  226. $this->Task->all();
  227. }
  228. /**
  229. * test interactive mode of execute
  230. *
  231. * @return void
  232. */
  233. public function testExecuteInteractive() {
  234. $this->Task->connection = 'test';
  235. $this->Task->path = '/my/path/';
  236. $this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
  237. $this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
  238. $this->Task->Model->expects($this->any())->method('getTable')
  239. ->with('Article')
  240. ->will($this->returnValue('articles'));
  241. $filename = '/my/path/ArticleFixture.php';
  242. $this->Task->expects($this->once())->method('createFile')
  243. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));
  244. $this->Task->execute();
  245. }
  246. /**
  247. * Test that bake works
  248. *
  249. * @return void
  250. */
  251. public function testBake() {
  252. $this->Task->connection = 'test';
  253. $this->Task->path = '/my/path/';
  254. $result = $this->Task->bake('Article');
  255. $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
  256. $this->assertPattern('/public \$fields/', $result);
  257. $this->assertPattern('/public \$records/', $result);
  258. $this->assertNoPattern('/public \$import/', $result);
  259. $result = $this->Task->bake('Article', 'comments');
  260. $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
  261. $this->assertPattern('/public \$table \= \'comments\';/', $result);
  262. $this->assertPattern('/public \$fields = array\(/', $result);
  263. $result = $this->Task->bake('Article', 'comments', array('records' => true));
  264. $this->assertPattern("/public \\\$import \= array\('records' \=\> true, 'connection' => 'test'\);/", $result);
  265. $this->assertNoPattern('/public \$records/', $result);
  266. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
  267. $this->assertPattern("/public \\\$import \= array\('model' \=\> 'Article'\, 'connection' => 'test'\);/", $result);
  268. $this->assertNoPattern('/public \$fields/', $result);
  269. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
  270. $this->assertPattern("/public \\\$import \= array\('model' \=\> 'Article'\, 'records' \=\> true\, 'connection' => 'test'\);/", $result);
  271. $this->assertNoPattern('/public \$fields/', $result);
  272. $this->assertNoPattern('/public \$records/', $result);
  273. }
  274. /**
  275. * test record generation with float and binary types
  276. *
  277. * @return void
  278. */
  279. public function testRecordGenerationForBinaryAndFloat() {
  280. $this->Task->connection = 'test';
  281. $this->Task->path = '/my/path/';
  282. $result = $this->Task->bake('Article', 'datatypes');
  283. $this->assertPattern("/'float_field' => 1/", $result);
  284. $result = $this->Task->bake('Article', 'binary_tests');
  285. $this->assertPattern("/'data' => 'Lorem ipsum dolor sit amet'/", $result);
  286. }
  287. /**
  288. * Test that file generation includes headers and correct path for plugins.
  289. *
  290. * @return void
  291. */
  292. public function testGenerateFixtureFile() {
  293. $this->Task->connection = 'test';
  294. $this->Task->path = '/my/path/';
  295. $filename = '/my/path/ArticleFixture.php';
  296. $this->Task->expects($this->at(0))->method('createFile')
  297. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/ArticleFixture/'));
  298. $this->Task->expects($this->at(1))->method('createFile')
  299. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/\<\?php/ms'));
  300. $result = $this->Task->generateFixtureFile('Article', array());
  301. $result = $this->Task->generateFixtureFile('Article', array());
  302. }
  303. /**
  304. * test generating files into plugins.
  305. *
  306. * @return void
  307. */
  308. public function testGeneratePluginFixtureFile() {
  309. $this->Task->connection = 'test';
  310. $this->Task->path = '/my/path/';
  311. $this->Task->plugin = 'TestFixture';
  312. $filename = APP . 'plugins' . DS . 'TestFixture' . DS . 'tests' . DS . 'Fixture' . DS . 'ArticleFixture.php';
  313. //fake plugin path
  314. CakePlugin::load('TestFixture', array('path' => APP . 'plugins' . DS . 'TestFixture' . DS));
  315. $this->Task->expects($this->at(0))->method('createFile')
  316. ->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/Article/'));
  317. $result = $this->Task->generateFixtureFile('Article', array());
  318. CakePlugin::unload();
  319. }
  320. }