PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/console/libs/schema.test.php

https://bitbucket.org/webpolis/liiv
PHP | 353 lines | 202 code | 39 blank | 112 comment | 3 complexity | 5f62db60bf084699b26a3ebfe94c365a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SchemaShellTest Test file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP : Rapid Development Framework (http://www.cakephp.org)
  8. * Copyright 2006-2009, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @filesource
  14. * @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
  15. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
  16. * @package cake
  17. * @subpackage cake.tests.cases.console.libs.Shells
  18. * @since CakePHP v 1.3
  19. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  20. */
  21. App::import('Shell', 'Shell', false);
  22. App::import('Model', 'Schema', false);
  23. if (!defined('DISABLE_AUTO_DISPATCH')) {
  24. define('DISABLE_AUTO_DISPATCH', true);
  25. }
  26. if (!class_exists('ShellDispatcher')) {
  27. ob_start();
  28. $argv = false;
  29. require CAKE . 'console' . DS . 'cake.php';
  30. ob_end_clean();
  31. }
  32. if (!class_exists('SchemaShell')) {
  33. require CAKE . 'console' . DS . 'libs' . DS . 'schema.php';
  34. }
  35. Mock::generatePartial(
  36. 'ShellDispatcher', 'TestSchemaShellMockShellDispatcher',
  37. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  38. );
  39. Mock::generatePartial(
  40. 'SchemaShell', 'MockSchemaShell',
  41. array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop')
  42. );
  43. Mock::generate('CakeSchema', 'MockSchemaCakeSchema');
  44. /**
  45. * Test for Schema database management
  46. *
  47. * @package cake
  48. * @subpackage cake.tests.cases.libs
  49. */
  50. class SchemaShellTestSchema extends CakeSchema {
  51. /**
  52. * name property
  53. *
  54. * @var string 'MyApp'
  55. * @access public
  56. */
  57. var $name = 'SchemaShellTest';
  58. /**
  59. * connection property
  60. *
  61. * @var string 'test_suite'
  62. * @access public
  63. */
  64. var $connection = 'test_suite';
  65. /**
  66. * comments property
  67. *
  68. * @var array
  69. * @access public
  70. */
  71. var $comments = array(
  72. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  73. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  74. 'user_id' => array('type' => 'integer', 'null' => false),
  75. 'title' => array('type' => 'string', 'null' => false, 'length' => 100),
  76. 'comment' => array('type' => 'text', 'null' => false, 'default' => null),
  77. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  78. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  79. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  80. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  81. );
  82. /**
  83. * posts property
  84. *
  85. * @var array
  86. * @access public
  87. */
  88. var $articles = array(
  89. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  90. 'user_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
  91. 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
  92. 'body' => array('type' => 'text', 'null' => true, 'default' => null),
  93. 'summary' => array('type' => 'text', 'null' => true),
  94. 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
  95. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  96. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  97. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  98. );
  99. }
  100. /**
  101. * SchemaShellTest class
  102. *
  103. * @package cake
  104. * @subpackage cake.tests.cases.console.libs.Shells
  105. */
  106. class SchemaShellTest extends CakeTestCase {
  107. var $fixtures = array('core.article', 'core.user');
  108. /**
  109. * startTest method
  110. *
  111. * @return void
  112. * @access public
  113. */
  114. function startTest() {
  115. $this->Dispatcher =& new TestSchemaShellMockShellDispatcher();
  116. $this->Shell =& new MockSchemaShell($this->Dispatcher);
  117. $this->Shell->Dispatch =& $this->Dispatcher;
  118. }
  119. /**
  120. * endTest method
  121. *
  122. * @return void
  123. * @access public
  124. */
  125. function endTest() {
  126. ClassRegistry::flush();
  127. }
  128. /**
  129. * test startup method
  130. *
  131. * @return void
  132. **/
  133. function testStartup() {
  134. $this->Shell->startup();
  135. $this->assertTrue(isset($this->Shell->Schema));
  136. $this->assertTrue(is_a($this->Shell->Schema, 'CakeSchema'));
  137. $this->assertEqual(strtolower($this->Shell->Schema->name), APP_DIR);
  138. $this->assertEqual($this->Shell->Schema->file, 'schema.php');
  139. unset($this->Shell->Schema);
  140. $this->Shell->params = array(
  141. 'name' => 'TestSchema'
  142. );
  143. $this->Shell->startup();
  144. $this->assertEqual($this->Shell->Schema->name, 'TestSchema');
  145. $this->assertEqual($this->Shell->Schema->file, 'test_schema.php');
  146. $this->assertEqual($this->Shell->Schema->connection, 'default');
  147. $this->assertEqual($this->Shell->Schema->path, APP . 'config' . DS . 'sql');
  148. unset($this->Shell->Schema);
  149. $this->Shell->params = array(
  150. 'file' => 'other_file.php',
  151. 'connection' => 'test_suite',
  152. 'path' => '/test/path'
  153. );
  154. $this->Shell->startup();
  155. $this->assertEqual(strtolower($this->Shell->Schema->name), APP_DIR);
  156. $this->assertEqual($this->Shell->Schema->file, 'other_file.php');
  157. $this->assertEqual($this->Shell->Schema->connection, 'test_suite');
  158. $this->assertEqual($this->Shell->Schema->path, '/test/path');
  159. }
  160. /**
  161. * Test View - and that it dumps the schema file to stdout
  162. *
  163. * @return void
  164. **/
  165. function testView() {
  166. $this->Shell->startup();
  167. $this->Shell->Schema->path = APP . 'config' . DS . 'schema';
  168. $this->Shell->params['file'] = 'i18n.php';
  169. $this->Shell->expectOnce('_stop');
  170. $this->Shell->view();
  171. }
  172. /**
  173. * test dump() with sql file generation
  174. *
  175. * @return void
  176. **/
  177. function testDumpWithFileWriting() {
  178. $file =& new File(APP . 'config' . DS . 'sql' . DS . 'i18n.php');
  179. $contents = $file->read();
  180. $file =& new File(TMP . 'tests' . DS . 'i18n.php');
  181. $file->write($contents);
  182. $this->Shell->params = array('name' => 'i18n');
  183. $this->Shell->args = array('write');
  184. $this->Shell->startup();
  185. $this->Shell->Schema->path = TMP . 'tests';
  186. $this->Shell->dump();
  187. $sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
  188. $contents = $sql->read();
  189. $this->assertPattern('/DROP TABLE/', $contents);
  190. $this->assertPattern('/CREATE TABLE `i18n`/', $contents);
  191. $this->assertPattern('/id/', $contents);
  192. $this->assertPattern('/model/', $contents);
  193. $this->assertPattern('/field/', $contents);
  194. $this->assertPattern('/locale/', $contents);
  195. $this->assertPattern('/foreign_key/', $contents);
  196. $this->assertPattern('/content/', $contents);
  197. $sql->delete();
  198. $file->delete();
  199. }
  200. /**
  201. * test generate with snapshot generation
  202. *
  203. * @return void
  204. */
  205. function testGenerateSnaphot() {
  206. $this->Shell->path = TMP;
  207. $this->Shell->params['file'] = 'schema.php';
  208. $this->Shell->args = array('snapshot');
  209. $this->Shell->Schema =& new MockSchemaCakeSchema();
  210. $this->Shell->Schema->setReturnValue('read', array('schema data'));
  211. $this->Shell->Schema->setReturnValue('write', true);
  212. $this->Shell->Schema->expectOnce('read');
  213. $this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema_1.php')));
  214. $this->Shell->generate();
  215. }
  216. /**
  217. * test generate without a snapshot.
  218. *
  219. * @return void
  220. **/
  221. function testGenerateNoOverwrite() {
  222. touch(TMP . 'schema.php');
  223. $this->Shell->params['file'] = 'schema.php';
  224. $this->Shell->args = array();
  225. $this->Shell->setReturnValue('in', 'q');
  226. $this->Shell->Schema =& new MockSchemaCakeSchema();
  227. $this->Shell->Schema->path = TMP;
  228. $this->Shell->Schema->expectNever('read');
  229. $result = $this->Shell->generate();
  230. unlink(TMP . 'schema.php');
  231. }
  232. /**
  233. * test generate with overwriting of the schema files.
  234. *
  235. * @return void
  236. **/
  237. function testGenerateOverwrite() {
  238. touch(TMP . 'schema.php');
  239. $this->Shell->params['file'] = 'schema.php';
  240. $this->Shell->args = array();
  241. $this->Shell->setReturnValue('in', 'o');
  242. $this->Shell->expectAt(1, 'out', array(new PatternExpectation('/Schema file:\s[a-z\.]+\sgenerated/')));
  243. $this->Shell->Schema =& new MockSchemaCakeSchema();
  244. $this->Shell->Schema->path = TMP;
  245. $this->Shell->Schema->setReturnValue('read', array('schema data'));
  246. $this->Shell->Schema->setReturnValue('write', true);
  247. $this->Shell->Schema->expectOnce('read');
  248. $this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema.php')));
  249. $this->Shell->generate();
  250. unlink(TMP . 'schema.php');
  251. }
  252. /**
  253. * Test schema run create with no table args.
  254. *
  255. * @return void
  256. **/
  257. function testRunCreateNoArgs() {
  258. $this->Shell->params = array(
  259. 'connection' => 'test_suite',
  260. 'name' => 'i18n',
  261. 'path' => APP . 'config' . DS . 'sql'
  262. );
  263. $this->Shell->args = array('create');
  264. $this->Shell->startup();
  265. $this->Shell->setReturnValue('in', 'y');
  266. $this->Shell->run();
  267. $db =& ConnectionManager::getDataSource('test_suite');
  268. $sources = $db->listSources();
  269. $this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources));
  270. }
  271. /**
  272. * Test schema run create with no table args.
  273. *
  274. * @return void
  275. **/
  276. function testRunCreateWithTableArgs() {
  277. $this->Shell->params = array(
  278. 'connection' => 'test_suite',
  279. 'name' => 'DbAcl',
  280. 'path' => APP . 'config' . DS . 'sql'
  281. );
  282. $this->Shell->args = array('create', 'acos');
  283. $this->Shell->startup();
  284. $this->Shell->setReturnValue('in', 'y');
  285. $this->Shell->run();
  286. $db =& ConnectionManager::getDataSource('test_suite');
  287. $sources = $db->listSources();
  288. $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
  289. $this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources));
  290. $this->assertFalse(in_array('aros_acos', $sources));
  291. }
  292. /**
  293. * test run update with a table arg.
  294. *
  295. * @return void
  296. **/
  297. function testRunUpdateWithTable() {
  298. $this->Shell->params = array(
  299. 'name' => 'SchemaShellTest',
  300. 'connection' => 'test_suite',
  301. 'f' => true
  302. );
  303. $this->Shell->args = array('update', 'articles');
  304. $this->Shell->startup();
  305. $this->Shell->setReturnValue('in', 'y');
  306. $this->Shell->run();
  307. $article =& new Model(array('name' => 'Article', 'ds' => 'test_suite'));
  308. $fields = $article->schema();
  309. $this->assertTrue(isset($fields['summary']));
  310. $this->_fixtures['core.article']->drop($this->db);
  311. $this->_fixtures['core.article']->create($this->db);
  312. }
  313. }
  314. ?>