PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/console/libs/tasks/fixture.test.php

https://github.com/cgajardo/repositorium
PHP | 373 lines | 205 code | 49 blank | 119 comment | 2 complexity | c94839c3852771479b373ccd1b4ecf78 MD5 | raw file
  1. <?php
  2. /**
  3. * FixtureTask Test case
  4. *
  5. * PHP versions 4 and 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
  16. * @subpackage cake.tests.cases.console.libs.tasks
  17. * @since CakePHP(tm) v 1.3
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::import('Shell', 'Shell', false);
  21. if (!defined('DISABLE_AUTO_DISPATCH')) {
  22. define('DISABLE_AUTO_DISPATCH', true);
  23. }
  24. if (!class_exists('ShellDispatcher')) {
  25. ob_start();
  26. $argv = false;
  27. require CAKE . 'console' . DS . 'cake.php';
  28. ob_end_clean();
  29. }
  30. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
  31. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
  32. Mock::generatePartial(
  33. 'ShellDispatcher', 'TestFixtureTaskMockShellDispatcher',
  34. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  35. );
  36. Mock::generatePartial(
  37. 'FixtureTask', 'MockFixtureTask',
  38. array('in', 'out', 'err', 'createFile', '_stop')
  39. );
  40. Mock::generatePartial(
  41. 'Shell', 'MockFixtureModelTask',
  42. array('in', 'out', 'err', 'createFile', '_stop', 'getName', 'getTable', 'listAll')
  43. );
  44. /**
  45. * FixtureTaskTest class
  46. *
  47. * @package cake
  48. * @subpackage cake.tests.cases.console.libs.tasks
  49. */
  50. class FixtureTaskTest extends CakeTestCase {
  51. /**
  52. * fixtures
  53. *
  54. * @var array
  55. * @access public
  56. */
  57. var $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test');
  58. /**
  59. * startTest method
  60. *
  61. * @return void
  62. * @access public
  63. */
  64. function startTest() {
  65. $this->Dispatcher =& new TestFixtureTaskMockShellDispatcher();
  66. $this->Task =& new MockFixtureTask();
  67. $this->Task->Model =& new MockFixtureModelTask();
  68. $this->Task->Dispatch =& $this->Dispatcher;
  69. $this->Task->Template =& new TemplateTask($this->Task->Dispatch);
  70. $this->Task->Dispatch->shellPaths = App::path('shells');
  71. $this->Task->Template->initialize();
  72. }
  73. /**
  74. * endTest method
  75. *
  76. * @return void
  77. * @access public
  78. */
  79. function endTest() {
  80. unset($this->Task, $this->Dispatcher);
  81. ClassRegistry::flush();
  82. }
  83. /**
  84. * test that initialize sets the path
  85. *
  86. * @return void
  87. * @access public
  88. */
  89. function testConstruct() {
  90. $this->Dispatch->params['working'] = DS . 'my' . DS . 'path';
  91. $Task =& new FixtureTask($this->Dispatch);
  92. $expected = DS . 'my' . DS . 'path' . DS . 'tests' . DS . 'fixtures' . DS;
  93. $this->assertEqual($Task->path, $expected);
  94. }
  95. /**
  96. * test import option array generation
  97. *
  98. * @return void
  99. * @access public
  100. */
  101. function testImportOptions() {
  102. $this->Task->setReturnValueAt(0, 'in', 'y');
  103. $this->Task->setReturnValueAt(1, 'in', 'y');
  104. $result = $this->Task->importOptions('Article');
  105. $expected = array('schema' => 'Article', 'records' => true);
  106. $this->assertEqual($result, $expected);
  107. $this->Task->setReturnValueAt(2, 'in', 'n');
  108. $this->Task->setReturnValueAt(3, 'in', 'n');
  109. $this->Task->setReturnValueAt(4, 'in', 'n');
  110. $result = $this->Task->importOptions('Article');
  111. $expected = array();
  112. $this->assertEqual($result, $expected);
  113. $this->Task->setReturnValueAt(5, 'in', 'n');
  114. $this->Task->setReturnValueAt(6, 'in', 'n');
  115. $this->Task->setReturnValueAt(7, 'in', 'y');
  116. $result = $this->Task->importOptions('Article');
  117. $expected = array('fromTable' => true);
  118. $this->assertEqual($result, $expected);
  119. }
  120. /**
  121. * test that connection gets set to the import options when a different connection is used.
  122. *
  123. * @return void
  124. */
  125. function testImportOptionsAlternateConnection() {
  126. $this->Task->connection = 'test';
  127. $result = $this->Task->bake('Article', false, array('schema' => 'Article'));
  128. $this->assertPattern("/'connection' => 'test'/", $result);
  129. }
  130. /**
  131. * test generating a fixture with database conditions.
  132. *
  133. * @return void
  134. * @access public
  135. */
  136. function testImportRecordsFromDatabaseWithConditions() {
  137. $this->Task->interactive = true;
  138. $this->Task->setReturnValueAt(0, 'in', 'WHERE 1=1 LIMIT 10');
  139. $this->Task->connection = 'test_suite';
  140. $this->Task->path = '/my/path/';
  141. $result = $this->Task->bake('Article', false, array('fromTable' => true, 'schema' => 'Article', 'records' => false));
  142. $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
  143. $this->assertPattern('/var \$records/', $result);
  144. $this->assertPattern('/var \$import/', $result);
  145. $this->assertPattern("/'title' => 'First Article'/", $result, 'Missing import data %s');
  146. $this->assertPattern('/Second Article/', $result, 'Missing import data %s');
  147. $this->assertPattern('/Third Article/', $result, 'Missing import data %s');
  148. }
  149. /**
  150. * test that execute passes runs bake depending with named model.
  151. *
  152. * @return void
  153. * @access public
  154. */
  155. function testExecuteWithNamedModel() {
  156. $this->Task->connection = 'test_suite';
  157. $this->Task->path = '/my/path/';
  158. $this->Task->args = array('article');
  159. $filename = '/my/path/article_fixture.php';
  160. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  161. $this->Task->execute();
  162. }
  163. /**
  164. * test that execute passes runs bake depending with named model.
  165. *
  166. * @return void
  167. * @access public
  168. */
  169. function testExecuteWithNamedModelVariations() {
  170. $this->Task->connection = 'test_suite';
  171. $this->Task->path = '/my/path/';
  172. $this->Task->args = array('article');
  173. $filename = '/my/path/article_fixture.php';
  174. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  175. $this->Task->execute();
  176. $this->Task->args = array('articles');
  177. $filename = '/my/path/article_fixture.php';
  178. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  179. $this->Task->execute();
  180. $this->Task->args = array('Articles');
  181. $filename = '/my/path/article_fixture.php';
  182. $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  183. $this->Task->execute();
  184. $this->Task->args = array('Article');
  185. $filename = '/my/path/article_fixture.php';
  186. $this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  187. $this->Task->execute();
  188. }
  189. /**
  190. * test that execute runs all() when args[0] = all
  191. *
  192. * @return void
  193. * @access public
  194. */
  195. function testExecuteIntoAll() {
  196. $this->Task->connection = 'test_suite';
  197. $this->Task->path = '/my/path/';
  198. $this->Task->args = array('all');
  199. $this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
  200. $filename = '/my/path/article_fixture.php';
  201. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  202. $this->Task->execute();
  203. $filename = '/my/path/comment_fixture.php';
  204. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class CommentFixture/')));
  205. $this->Task->execute();
  206. }
  207. /**
  208. * test using all() with -count and -records
  209. *
  210. * @return void
  211. * @access public
  212. */
  213. function testAllWithCountAndRecordsFlags() {
  214. $this->Task->connection = 'test_suite';
  215. $this->Task->path = '/my/path/';
  216. $this->Task->args = array('all');
  217. $this->Task->params = array('count' => 10, 'records' => true);
  218. $this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
  219. $filename = '/my/path/article_fixture.php';
  220. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/title\' => \'Third Article\'/')));
  221. $filename = '/my/path/comment_fixture.php';
  222. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/comment\' => \'First Comment for First Article/')));
  223. $this->Task->expectCallCount('createFile', 2);
  224. $this->Task->all();
  225. }
  226. /**
  227. * test interactive mode of execute
  228. *
  229. * @return void
  230. * @access public
  231. */
  232. function testExecuteInteractive() {
  233. $this->Task->connection = 'test_suite';
  234. $this->Task->path = '/my/path/';
  235. $this->Task->setReturnValue('in', 'y');
  236. $this->Task->Model->setReturnValue('getName', 'Article');
  237. $this->Task->Model->setReturnValue('getTable', 'articles', array('Article'));
  238. $filename = '/my/path/article_fixture.php';
  239. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
  240. $this->Task->execute();
  241. }
  242. /**
  243. * Test that bake works
  244. *
  245. * @return void
  246. * @access public
  247. */
  248. function testBake() {
  249. $this->Task->connection = 'test_suite';
  250. $this->Task->path = '/my/path/';
  251. $result = $this->Task->bake('Article');
  252. $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
  253. $this->assertPattern('/var \$fields/', $result);
  254. $this->assertPattern('/var \$records/', $result);
  255. $this->assertNoPattern('/var \$import/', $result);
  256. $result = $this->Task->bake('Article', 'comments');
  257. $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
  258. $this->assertPattern('/var \$name \= \'Article\';/', $result);
  259. $this->assertPattern('/var \$table \= \'comments\';/', $result);
  260. $this->assertPattern('/var \$fields = array\(/', $result);
  261. $result = $this->Task->bake('Article', 'comments', array('records' => true));
  262. $this->assertPattern(
  263. "/var \\\$import \= array\('records' \=\> true, 'connection' => 'test_suite'\);/",
  264. $result
  265. );
  266. $this->assertNoPattern('/var \$records/', $result);
  267. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
  268. $this->assertPattern(
  269. "/var \\\$import \= array\('model' \=\> 'Article', 'connection' => 'test_suite'\);/",
  270. $result
  271. );
  272. $this->assertNoPattern('/var \$fields/', $result);
  273. $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
  274. $this->assertPattern(
  275. "/var \\\$import \= array\('model' \=\> 'Article'\, 'records' \=\> true, 'connection' => 'test_suite'\);/",
  276. $result
  277. );
  278. $this->assertNoPattern('/var \$fields/', $result);
  279. $this->assertNoPattern('/var \$records/', $result);
  280. }
  281. /**
  282. * test record generation with float and binary types
  283. *
  284. * @return void
  285. * @access public
  286. */
  287. function testRecordGenerationForBinaryAndFloat() {
  288. $this->Task->connection = 'test_suite';
  289. $this->Task->path = '/my/path/';
  290. $result = $this->Task->bake('Article', 'datatypes');
  291. $this->assertPattern("/'float_field' => 1/", $result);
  292. $result = $this->Task->bake('Article', 'binary_tests');
  293. $this->assertPattern("/'data' => 'Lorem ipsum dolor sit amet'/", $result);
  294. }
  295. /**
  296. * Test that file generation includes headers and correct path for plugins.
  297. *
  298. * @return void
  299. * @access public
  300. */
  301. function testGenerateFixtureFile() {
  302. $this->Task->connection = 'test_suite';
  303. $this->Task->path = '/my/path/';
  304. $filename = '/my/path/article_fixture.php';
  305. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
  306. $result = $this->Task->generateFixtureFile('Article', array());
  307. $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/\<\?php(.*)\?\>/ms')));
  308. $result = $this->Task->generateFixtureFile('Article', array());
  309. }
  310. /**
  311. * test generating files into plugins.
  312. *
  313. * @return void
  314. * @access public
  315. */
  316. function testGeneratePluginFixtureFile() {
  317. $this->Task->connection = 'test_suite';
  318. $this->Task->path = '/my/path/';
  319. $this->Task->plugin = 'TestFixture';
  320. $filename = APP . 'plugins' . DS . 'test_fixture' . DS . 'tests' . DS . 'fixtures' . DS . 'article_fixture.php';
  321. $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
  322. $result = $this->Task->generateFixtureFile('Article', array());
  323. }
  324. }