PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/cgajardo/repositorium
PHP | 503 lines | 290 code | 56 blank | 157 comment | 3 complexity | 5af773a9f4a60665b5c54e60cc5431d4 MD5 | raw file
  1. <?php
  2. /**
  3. * SchemaShellTest Test file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2006-2010, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
  14. * @link http://cakephp.org CakePHP Project
  15. * @package cake
  16. * @subpackage cake.tests.cases.console.libs.Shells
  17. * @since CakePHP v 1.3
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::import('Shell', 'Shell', false);
  21. App::import('Model', 'CakeSchema', false);
  22. if (!defined('DISABLE_AUTO_DISPATCH')) {
  23. define('DISABLE_AUTO_DISPATCH', true);
  24. }
  25. if (!class_exists('ShellDispatcher')) {
  26. ob_start();
  27. $argv = false;
  28. require CAKE . 'console' . DS . 'cake.php';
  29. ob_end_clean();
  30. }
  31. if (!class_exists('SchemaShell')) {
  32. require CAKE . 'console' . DS . 'libs' . DS . 'schema.php';
  33. }
  34. Mock::generatePartial(
  35. 'ShellDispatcher', 'TestSchemaShellMockShellDispatcher',
  36. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  37. );
  38. Mock::generatePartial(
  39. 'SchemaShell', 'MockSchemaShell',
  40. array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop')
  41. );
  42. Mock::generate('CakeSchema', 'MockSchemaCakeSchema');
  43. /**
  44. * Test for Schema database management
  45. *
  46. * @package cake
  47. * @subpackage cake.tests.cases.libs
  48. */
  49. class SchemaShellTestSchema extends CakeSchema {
  50. /**
  51. * name property
  52. *
  53. * @var string 'MyApp'
  54. * @access public
  55. */
  56. var $name = 'SchemaShellTest';
  57. /**
  58. * connection property
  59. *
  60. * @var string 'test_suite'
  61. * @access public
  62. */
  63. var $connection = 'test_suite';
  64. /**
  65. * comments property
  66. *
  67. * @var array
  68. * @access public
  69. */
  70. var $comments = array(
  71. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  72. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  73. 'user_id' => array('type' => 'integer', 'null' => false),
  74. 'title' => array('type' => 'string', 'null' => false, 'length' => 100),
  75. 'comment' => array('type' => 'text', 'null' => false, 'default' => null),
  76. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  77. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  78. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  79. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  80. );
  81. /**
  82. * posts property
  83. *
  84. * @var array
  85. * @access public
  86. */
  87. var $articles = array(
  88. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  89. 'user_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
  90. 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
  91. 'body' => array('type' => 'text', 'null' => true, 'default' => null),
  92. 'summary' => array('type' => 'text', 'null' => true),
  93. 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
  94. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  95. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  96. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  97. );
  98. }
  99. /**
  100. * SchemaShellTest class
  101. *
  102. * @package cake
  103. * @subpackage cake.tests.cases.console.libs.Shells
  104. */
  105. class SchemaShellTest extends CakeTestCase {
  106. /**
  107. * Fixtures
  108. *
  109. * @var array
  110. * @access public
  111. */
  112. var $fixtures = array('core.article', 'core.user', 'core.post', 'core.auth_user', 'core.author',
  113. 'core.comment', 'core.test_plugin_comment'
  114. );
  115. /**
  116. * startTest method
  117. *
  118. * @return void
  119. * @access public
  120. */
  121. function startTest() {
  122. $this->Dispatcher =& new TestSchemaShellMockShellDispatcher();
  123. $this->Shell =& new MockSchemaShell($this->Dispatcher);
  124. $this->Shell->Dispatch =& $this->Dispatcher;
  125. }
  126. /**
  127. * endTest method
  128. *
  129. * @return void
  130. * @access public
  131. */
  132. function endTest() {
  133. ClassRegistry::flush();
  134. }
  135. /**
  136. * test startup method
  137. *
  138. * @return void
  139. * @access public
  140. */
  141. function testStartup() {
  142. $this->Shell->startup();
  143. $this->assertTrue(isset($this->Shell->Schema));
  144. $this->assertTrue(is_a($this->Shell->Schema, 'CakeSchema'));
  145. $this->assertEqual(strtolower($this->Shell->Schema->name), strtolower(APP_DIR));
  146. $this->assertEqual($this->Shell->Schema->file, 'schema.php');
  147. unset($this->Shell->Schema);
  148. $this->Shell->params = array(
  149. 'name' => 'TestSchema'
  150. );
  151. $this->Shell->startup();
  152. $this->assertEqual($this->Shell->Schema->name, 'TestSchema');
  153. $this->assertEqual($this->Shell->Schema->file, 'test_schema.php');
  154. $this->assertEqual($this->Shell->Schema->connection, 'default');
  155. $this->assertEqual($this->Shell->Schema->path, APP . 'config' . DS . 'schema');
  156. unset($this->Shell->Schema);
  157. $this->Shell->params = array(
  158. 'file' => 'other_file.php',
  159. 'connection' => 'test_suite',
  160. 'path' => '/test/path'
  161. );
  162. $this->Shell->startup();
  163. $this->assertEqual(strtolower($this->Shell->Schema->name), strtolower(APP_DIR));
  164. $this->assertEqual($this->Shell->Schema->file, 'other_file.php');
  165. $this->assertEqual($this->Shell->Schema->connection, 'test_suite');
  166. $this->assertEqual($this->Shell->Schema->path, '/test/path');
  167. }
  168. /**
  169. * Test View - and that it dumps the schema file to stdout
  170. *
  171. * @return void
  172. * @access public
  173. */
  174. function testView() {
  175. $this->Shell->startup();
  176. $this->Shell->Schema->path = APP . 'config' . DS . 'schema';
  177. $this->Shell->params['file'] = 'i18n.php';
  178. $this->Shell->expectOnce('_stop');
  179. $this->Shell->expectOnce('out');
  180. $this->Shell->view();
  181. }
  182. /**
  183. * test that view() can find plugin schema files.
  184. *
  185. * @return void
  186. * @access public
  187. */
  188. function testViewWithPlugins() {
  189. App::build(array(
  190. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  191. ));
  192. $this->Shell->args = array('TestPlugin.schema');
  193. $this->Shell->startup();
  194. $this->Shell->expectCallCount('_stop', 2);
  195. $this->Shell->expectCallCount('out', 2);
  196. $this->Shell->view();
  197. $this->Shell->args = array();
  198. $this->Shell->params = array('plugin' => 'TestPlugin');
  199. $this->Shell->startup();
  200. $this->Shell->view();
  201. App::build();
  202. }
  203. /**
  204. * test dump() with sql file generation
  205. *
  206. * @return void
  207. * @access public
  208. */
  209. function testDumpWithFileWriting() {
  210. $this->Shell->params = array(
  211. 'name' => 'i18n',
  212. 'write' => TMP . 'tests' . DS . 'i18n.sql'
  213. );
  214. $this->Shell->expectOnce('_stop');
  215. $this->Shell->startup();
  216. $this->Shell->dump();
  217. $sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
  218. $contents = $sql->read();
  219. $this->assertPattern('/DROP TABLE/', $contents);
  220. $this->assertPattern('/CREATE TABLE `i18n`/', $contents);
  221. $this->assertPattern('/id/', $contents);
  222. $this->assertPattern('/model/', $contents);
  223. $this->assertPattern('/field/', $contents);
  224. $this->assertPattern('/locale/', $contents);
  225. $this->assertPattern('/foreign_key/', $contents);
  226. $this->assertPattern('/content/', $contents);
  227. $sql->delete();
  228. }
  229. /**
  230. * test that dump() can find and work with plugin schema files.
  231. *
  232. * @return void
  233. * @access public
  234. */
  235. function testDumpFileWritingWithPlugins() {
  236. App::build(array(
  237. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  238. ));
  239. $this->Shell->args = array('TestPlugin.TestPluginApp');
  240. $this->Shell->params = array(
  241. 'connection' => 'test_suite',
  242. 'write' => TMP . 'tests' . DS . 'dump_test.sql'
  243. );
  244. $this->Shell->startup();
  245. $this->Shell->expectOnce('_stop');
  246. $this->Shell->dump();
  247. $file =& new File(TMP . 'tests' . DS . 'dump_test.sql');
  248. $contents = $file->read();
  249. $this->assertPattern('/CREATE TABLE `acos`/', $contents);
  250. $this->assertPattern('/id/', $contents);
  251. $this->assertPattern('/model/', $contents);
  252. $file->delete();
  253. App::build();
  254. }
  255. /**
  256. * test generate with snapshot generation
  257. *
  258. * @return void
  259. * @access public
  260. */
  261. function testGenerateSnaphot() {
  262. $this->Shell->path = TMP;
  263. $this->Shell->params['file'] = 'schema.php';
  264. $this->Shell->args = array('snapshot');
  265. $this->Shell->Schema =& new MockSchemaCakeSchema();
  266. $this->Shell->Schema->setReturnValue('read', array('schema data'));
  267. $this->Shell->Schema->setReturnValue('write', true);
  268. $this->Shell->Schema->expectOnce('read');
  269. $this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema_1.php')));
  270. $this->Shell->generate();
  271. }
  272. /**
  273. * test generate without a snapshot.
  274. *
  275. * @return void
  276. * @access public
  277. */
  278. function testGenerateNoOverwrite() {
  279. touch(TMP . 'schema.php');
  280. $this->Shell->params['file'] = 'schema.php';
  281. $this->Shell->args = array();
  282. $this->Shell->setReturnValue('in', 'q');
  283. $this->Shell->Schema =& new MockSchemaCakeSchema();
  284. $this->Shell->Schema->path = TMP;
  285. $this->Shell->Schema->expectNever('read');
  286. $result = $this->Shell->generate();
  287. unlink(TMP . 'schema.php');
  288. }
  289. /**
  290. * test generate with overwriting of the schema files.
  291. *
  292. * @return void
  293. * @access public
  294. */
  295. function testGenerateOverwrite() {
  296. touch(TMP . 'schema.php');
  297. $this->Shell->params['file'] = 'schema.php';
  298. $this->Shell->args = array();
  299. $this->Shell->setReturnValue('in', 'o');
  300. $this->Shell->expectAt(1, 'out', array(new PatternExpectation('/Schema file:\s[a-z\.]+\sgenerated/')));
  301. $this->Shell->Schema =& new MockSchemaCakeSchema();
  302. $this->Shell->Schema->path = TMP;
  303. $this->Shell->Schema->setReturnValue('read', array('schema data'));
  304. $this->Shell->Schema->setReturnValue('write', true);
  305. $this->Shell->Schema->expectOnce('read');
  306. $this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema.php')));
  307. $this->Shell->generate();
  308. unlink(TMP . 'schema.php');
  309. }
  310. /**
  311. * test that generate() can read plugin dirs and generate schema files for the models
  312. * in a plugin.
  313. *
  314. * @return void
  315. * @access public
  316. */
  317. function testGenerateWithPlugins() {
  318. App::build(array(
  319. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  320. ));
  321. $this->Shell->params = array(
  322. 'plugin' => 'TestPlugin',
  323. 'connection' => 'test_suite'
  324. );
  325. $this->Shell->startup();
  326. $this->Shell->Schema->path = TMP . 'tests' . DS;
  327. $this->Shell->generate();
  328. $file =& new File(TMP . 'tests' . DS . 'schema.php');
  329. $contents = $file->read();
  330. $this->assertPattern('/class TestPluginSchema/', $contents);
  331. $this->assertPattern('/var \$posts/', $contents);
  332. $this->assertPattern('/var \$auth_users/', $contents);
  333. $this->assertPattern('/var \$authors/', $contents);
  334. $this->assertPattern('/var \$test_plugin_comments/', $contents);
  335. $this->assertNoPattern('/var \$users/', $contents);
  336. $this->assertNoPattern('/var \$articles/', $contents);
  337. $file->delete();
  338. App::build();
  339. }
  340. /**
  341. * Test schema run create with no table args.
  342. *
  343. * @return void
  344. * @access public
  345. */
  346. function testCreateNoArgs() {
  347. $this->Shell->params = array(
  348. 'connection' => 'test_suite',
  349. 'path' => APP . 'config' . DS . 'sql'
  350. );
  351. $this->Shell->args = array('i18n');
  352. $this->Shell->startup();
  353. $this->Shell->setReturnValue('in', 'y');
  354. $this->Shell->create();
  355. $db =& ConnectionManager::getDataSource('test_suite');
  356. $sources = $db->listSources();
  357. $this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources));
  358. $schema =& new i18nSchema();
  359. $db->execute($db->dropSchema($schema));
  360. }
  361. /**
  362. * Test schema run create with no table args.
  363. *
  364. * @return void
  365. * @access public
  366. */
  367. function testCreateWithTableArgs() {
  368. $this->Shell->params = array(
  369. 'connection' => 'test_suite',
  370. 'name' => 'DbAcl',
  371. 'path' => APP . 'config' . DS . 'schema'
  372. );
  373. $this->Shell->args = array('DbAcl', 'acos');
  374. $this->Shell->startup();
  375. $this->Shell->setReturnValue('in', 'y');
  376. $this->Shell->create();
  377. $db =& ConnectionManager::getDataSource('test_suite');
  378. $sources = $db->listSources();
  379. $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
  380. $this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources));
  381. $this->assertFalse(in_array('aros_acos', $sources));
  382. $db->execute('DROP TABLE ' . $db->config['prefix'] . 'acos');
  383. }
  384. /**
  385. * test run update with a table arg.
  386. *
  387. * @return void
  388. * @access public
  389. */
  390. function testUpdateWithTable() {
  391. $this->Shell->params = array(
  392. 'connection' => 'test_suite',
  393. 'f' => true
  394. );
  395. $this->Shell->args = array('SchemaShellTest', 'articles');
  396. $this->Shell->startup();
  397. $this->Shell->setReturnValue('in', 'y');
  398. $this->Shell->update();
  399. $article =& new Model(array('name' => 'Article', 'ds' => 'test_suite'));
  400. $fields = $article->schema();
  401. $this->assertTrue(isset($fields['summary']));
  402. $this->_fixtures['core.article']->drop($this->db);
  403. $this->_fixtures['core.article']->create($this->db);
  404. }
  405. /**
  406. * test that the plugin param creates the correct path in the schema object.
  407. *
  408. * @return void
  409. * @access public
  410. */
  411. function testPluginParam() {
  412. App::build(array(
  413. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  414. ));
  415. $this->Shell->params = array(
  416. 'plugin' => 'TestPlugin',
  417. 'connection' => 'test_suite'
  418. );
  419. $this->Shell->startup();
  420. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'config' . DS . 'schema';
  421. $this->assertEqual($this->Shell->Schema->path, $expected);
  422. App::build();
  423. }
  424. /**
  425. * test that using Plugin.name with write.
  426. *
  427. * @return void
  428. * @access public
  429. */
  430. function testPluginDotSyntaxWithCreate() {
  431. App::build(array(
  432. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  433. ));
  434. $this->Shell->params = array(
  435. 'connection' => 'test_suite'
  436. );
  437. $this->Shell->args = array('TestPlugin.TestPluginApp');
  438. $this->Shell->startup();
  439. $this->Shell->setReturnValue('in', 'y');
  440. $this->Shell->create();
  441. $db =& ConnectionManager::getDataSource('test_suite');
  442. $sources = $db->listSources();
  443. $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
  444. $db->execute('DROP TABLE ' . $db->config['prefix'] . 'acos');
  445. App::build();
  446. }
  447. }