SchemaShellTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. /**
  3. * SchemaShellTest Test file
  4. *
  5. * PHP 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.tests.cases.console.libs.Shells
  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.tests.cases.libs
  29. */
  30. class SchemaShellTestSchema extends CakeSchema {
  31. /**
  32. * name property
  33. *
  34. * @var string 'MyApp'
  35. * @access public
  36. */
  37. public $name = 'SchemaShellTest';
  38. /**
  39. * connection property
  40. *
  41. * @var string 'test'
  42. * @access public
  43. */
  44. public $connection = 'test';
  45. /**
  46. * comments property
  47. *
  48. * @var array
  49. * @access public
  50. */
  51. public $comments = array(
  52. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  53. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  54. 'user_id' => array('type' => 'integer', 'null' => false),
  55. 'title' => array('type' => 'string', 'null' => false, 'length' => 100),
  56. 'comment' => array('type' => 'text', 'null' => false, 'default' => null),
  57. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  58. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  59. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  60. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  61. );
  62. /**
  63. * posts property
  64. *
  65. * @var array
  66. * @access public
  67. */
  68. public $articles = array(
  69. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  70. 'user_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
  71. 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
  72. 'body' => array('type' => 'text', 'null' => true, 'default' => null),
  73. 'summary' => array('type' => 'text', 'null' => true),
  74. 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
  75. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  76. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  77. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  78. );
  79. }
  80. /**
  81. * SchemaShellTest class
  82. *
  83. * @package cake.tests.cases.console.libs.Shells
  84. */
  85. class SchemaShellTest extends CakeTestCase {
  86. /**
  87. * Fixtures
  88. *
  89. * @var array
  90. * @access public
  91. */
  92. public $fixtures = array('core.article', 'core.user', 'core.post', 'core.auth_user', 'core.author',
  93. 'core.comment', 'core.test_plugin_comment'
  94. );
  95. /**
  96. * setup method
  97. *
  98. * @return void
  99. */
  100. public function setUp() {
  101. parent::setUp();
  102. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  103. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  104. $this->Shell = $this->getMock(
  105. 'SchemaShell',
  106. array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop'),
  107. array($out, $out, $in)
  108. );
  109. }
  110. /**
  111. * endTest method
  112. *
  113. * @return void
  114. */
  115. public function tearDown() {
  116. parent::tearDown();
  117. if (!empty($this->file) && $this->file instanceof File) {
  118. $this->file->delete();
  119. unset($this->file);
  120. }
  121. }
  122. /**
  123. * test startup method
  124. *
  125. * @return void
  126. */
  127. public function testStartup() {
  128. $this->Shell->startup();
  129. $this->assertTrue(isset($this->Shell->Schema));
  130. $this->assertTrue(is_a($this->Shell->Schema, 'CakeSchema'));
  131. $this->assertEqual(strtolower($this->Shell->Schema->name), strtolower(APP_DIR));
  132. $this->assertEqual($this->Shell->Schema->file, 'schema.php');
  133. $this->Shell->Schema = null;
  134. $this->Shell->params = array(
  135. 'name' => 'TestSchema'
  136. );
  137. $this->Shell->startup();
  138. $this->assertEqual($this->Shell->Schema->name, 'TestSchema');
  139. $this->assertEqual($this->Shell->Schema->file, 'test_schema.php');
  140. $this->assertEqual($this->Shell->Schema->connection, 'default');
  141. $this->assertEqual($this->Shell->Schema->path, APP . 'config' . DS . 'schema');
  142. $this->Shell->Schema = null;
  143. $this->Shell->params = array(
  144. 'file' => 'other_file.php',
  145. 'connection' => 'test',
  146. 'path' => '/test/path'
  147. );
  148. $this->Shell->startup();
  149. $this->assertEqual(strtolower($this->Shell->Schema->name), strtolower(APP_DIR));
  150. $this->assertEqual($this->Shell->Schema->file, 'other_file.php');
  151. $this->assertEqual($this->Shell->Schema->connection, 'test');
  152. $this->assertEqual($this->Shell->Schema->path, '/test/path');
  153. }
  154. /**
  155. * Test View - and that it dumps the schema file to stdout
  156. *
  157. * @return void
  158. */
  159. public function testView() {
  160. $this->Shell->startup();
  161. $this->Shell->Schema->path = APP . 'config' . DS . 'schema';
  162. $this->Shell->params['file'] = 'i18n.php';
  163. $this->Shell->expects($this->once())->method('_stop');
  164. $this->Shell->expects($this->once())->method('out');
  165. $this->Shell->view();
  166. }
  167. /**
  168. * test that view() can find plugin schema files.
  169. *
  170. * @return void
  171. */
  172. public function testViewWithPlugins() {
  173. App::build(array(
  174. 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  175. ));
  176. CakePlugin::load('TestPlugin');
  177. $this->Shell->args = array('TestPlugin.schema');
  178. $this->Shell->startup();
  179. $this->Shell->expects($this->exactly(2))->method('_stop');
  180. $this->Shell->expects($this->exactly(2))->method('out');
  181. $this->Shell->view();
  182. $this->Shell->args = array();
  183. $this->Shell->params = array('plugin' => 'TestPlugin');
  184. $this->Shell->startup();
  185. $this->Shell->view();
  186. App::build();
  187. CakePlugin::unload();
  188. }
  189. /**
  190. * test dump() with sql file generation
  191. *
  192. * @return void
  193. */
  194. public function testDumpWithFileWriting() {
  195. $this->Shell->params = array(
  196. 'name' => 'i18n',
  197. 'write' => TMP . 'tests' . DS . 'i18n.sql'
  198. );
  199. $this->Shell->expects($this->once())->method('_stop');
  200. $this->Shell->startup();
  201. $this->Shell->dump();
  202. $this->file = new File(TMP . 'tests' . DS . 'i18n.sql');
  203. $contents = $this->file->read();
  204. $this->assertPattern('/DROP TABLE/', $contents);
  205. $this->assertPattern('/CREATE TABLE.*?i18n/', $contents);
  206. $this->assertPattern('/id/', $contents);
  207. $this->assertPattern('/model/', $contents);
  208. $this->assertPattern('/field/', $contents);
  209. $this->assertPattern('/locale/', $contents);
  210. $this->assertPattern('/foreign_key/', $contents);
  211. $this->assertPattern('/content/', $contents);
  212. }
  213. /**
  214. * test that dump() can find and work with plugin schema files.
  215. *
  216. * @return void
  217. */
  218. public function testDumpFileWritingWithPlugins() {
  219. App::build(array(
  220. 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  221. ));
  222. CakePlugin::load('TestPlugin');
  223. $this->Shell->args = array('TestPlugin.TestPluginApp');
  224. $this->Shell->params = array(
  225. 'connection' => 'test',
  226. 'write' => TMP . 'tests' . DS . 'dump_test.sql'
  227. );
  228. $this->Shell->startup();
  229. $this->Shell->expects($this->once())->method('_stop');
  230. $this->Shell->dump();
  231. $this->file = new File(TMP . 'tests' . DS . 'dump_test.sql');
  232. $contents = $this->file->read();
  233. $this->assertPattern('/CREATE TABLE.*?test_plugin_acos/', $contents);
  234. $this->assertPattern('/id/', $contents);
  235. $this->assertPattern('/model/', $contents);
  236. $this->file->delete();
  237. App::build();
  238. CakePlugin::unload();
  239. }
  240. /**
  241. * test generate with snapshot generation
  242. *
  243. * @return void
  244. */
  245. public function testGenerateSnapshot() {
  246. $this->Shell->path = TMP;
  247. $this->Shell->params['file'] = 'schema.php';
  248. $this->Shell->args = array('snapshot');
  249. $this->Shell->Schema = $this->getMock('CakeSchema');
  250. $this->Shell->Schema->expects($this->at(0))->method('read')->will($this->returnValue(array('schema data')));
  251. $this->Shell->Schema->expects($this->at(0))->method('write')->will($this->returnValue(true));
  252. $this->Shell->Schema->expects($this->at(1))->method('read');
  253. $this->Shell->Schema->expects($this->at(1))->method('write')->with(array('schema data', 'file' => 'schema_0.php'));
  254. $this->Shell->generate();
  255. }
  256. /**
  257. * test generate without a snapshot.
  258. *
  259. * @return void
  260. */
  261. public function testGenerateNoOverwrite() {
  262. touch(TMP . 'schema.php');
  263. $this->Shell->params['file'] = 'schema.php';
  264. $this->Shell->args = array();
  265. $this->Shell->expects($this->once())->method('in')->will($this->returnValue('q'));
  266. $this->Shell->Schema = $this->getMock('CakeSchema');
  267. $this->Shell->Schema->path = TMP;
  268. $this->Shell->Schema->expects($this->never())->method('read');
  269. $result = $this->Shell->generate();
  270. unlink(TMP . 'schema.php');
  271. }
  272. /**
  273. * test generate with overwriting of the schema files.
  274. *
  275. * @return void
  276. */
  277. public function testGenerateOverwrite() {
  278. touch(TMP . 'schema.php');
  279. $this->Shell->params['file'] = 'schema.php';
  280. $this->Shell->args = array();
  281. $this->Shell->expects($this->once())->method('in')->will($this->returnValue('o'));
  282. $this->Shell->expects($this->at(2))->method('out')
  283. ->with(new PHPUnit_Framework_Constraint_PCREMatch('/Schema file:\s[a-z\.]+\sgenerated/'));
  284. $this->Shell->Schema = $this->getMock('CakeSchema');
  285. $this->Shell->Schema->path = TMP;
  286. $this->Shell->Schema->expects($this->once())->method('read')->will($this->returnValue(array('schema data')));
  287. $this->Shell->Schema->expects($this->once())->method('write')->will($this->returnValue(true));
  288. $this->Shell->Schema->expects($this->once())->method('read');
  289. $this->Shell->Schema->expects($this->once())->method('write')
  290. ->with(array('schema data', 'file' => 'schema.php'));
  291. $this->Shell->generate();
  292. unlink(TMP . 'schema.php');
  293. }
  294. /**
  295. * test that generate() can read plugin dirs and generate schema files for the models
  296. * in a plugin.
  297. *
  298. * @return void
  299. */
  300. public function testGenerateWithPlugins() {
  301. App::build(array(
  302. 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  303. ), true);
  304. CakePlugin::load('TestPlugin');
  305. $this->db->cacheSources = false;
  306. $this->Shell->params = array(
  307. 'plugin' => 'TestPlugin',
  308. 'connection' => 'test'
  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->assertPattern('/class TestPluginSchema/', $contents);
  316. $this->assertPattern('/var \$posts/', $contents);
  317. $this->assertPattern('/var \$auth_users/', $contents);
  318. $this->assertPattern('/var \$authors/', $contents);
  319. $this->assertPattern('/var \$test_plugin_comments/', $contents);
  320. $this->assertNoPattern('/var \$users/', $contents);
  321. $this->assertNoPattern('/var \$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' => CONFIGS . '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(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  403. ));
  404. CakePlugin::load('TestPlugin');
  405. $this->Shell->params = array(
  406. 'plugin' => 'TestPlugin',
  407. 'connection' => 'test'
  408. );
  409. $this->Shell->startup();
  410. $expected = LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'TestPlugin' . DS . 'config' . DS . 'schema';
  411. $this->assertEqual($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(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . 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. }