PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/Console/Command/SchemaShellTest.php

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