PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Console/Command/Task/FixtureTaskTest.php

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