SchemaShellTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. /**
  3. * SchemaShellTest Test file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, 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-2012, 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. * tearDown 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(Inflector::camelize(Inflector::slug(APP_DIR)), $this->Shell->Schema->name);
  127. $this->assertEquals('schema.php', $this->Shell->Schema->file);
  128. $this->Shell->Schema = null;
  129. $this->Shell->params = array(
  130. 'name' => 'TestSchema'
  131. );
  132. $this->Shell->startup();
  133. $this->assertEquals('TestSchema', $this->Shell->Schema->name);
  134. $this->assertEquals('test_schema.php', $this->Shell->Schema->file);
  135. $this->assertEquals('default', $this->Shell->Schema->connection);
  136. $this->assertEquals(APP . 'Config' . DS . 'Schema', $this->Shell->Schema->path);
  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(Inflector::camelize(Inflector::slug(APP_DIR)), $this->Shell->Schema->name);
  145. $this->assertEquals('other_file.php', $this->Shell->Schema->file);
  146. $this->assertEquals('test', $this->Shell->Schema->connection);
  147. $this->assertEquals('/test/path', $this->Shell->Schema->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. 'Plugin' => 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->atLeastOnce())->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. 'Plugin' => 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. $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. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  302. ), App::RESET);
  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 generate with specific models
  326. *
  327. * @return void
  328. */
  329. public function testGenerateModels() {
  330. App::build(array(
  331. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  332. ), App::RESET);
  333. CakePlugin::load('TestPlugin');
  334. $this->db->cacheSources = false;
  335. $this->Shell->params = array(
  336. 'plugin' => 'TestPlugin',
  337. 'connection' => 'test',
  338. 'models' => 'TestPluginComment',
  339. 'force' => false,
  340. 'overwrite' => true
  341. );
  342. $this->Shell->startup();
  343. $this->Shell->Schema->path = TMP . 'tests' . DS;
  344. $this->Shell->generate();
  345. $this->file = new File(TMP . 'tests' . DS . 'schema.php');
  346. $contents = $this->file->read();
  347. $this->assertRegExp('/class TestPluginSchema/', $contents);
  348. $this->assertRegExp('/public \$test_plugin_comments/', $contents);
  349. $this->assertNotRegExp('/public \$authors/', $contents);
  350. $this->assertNotRegExp('/public \$auth_users/', $contents);
  351. $this->assertNotRegExp('/public \$posts/', $contents);
  352. CakePlugin::unload();
  353. }
  354. /**
  355. * Test schema run create with no table args.
  356. *
  357. * @return void
  358. */
  359. public function testCreateNoArgs() {
  360. $this->Shell->params = array(
  361. 'connection' => 'test'
  362. );
  363. $this->Shell->args = array('i18n');
  364. $this->Shell->startup();
  365. $this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
  366. $this->Shell->create();
  367. $db = ConnectionManager::getDataSource('test');
  368. $db->cacheSources = false;
  369. $sources = $db->listSources();
  370. $this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources));
  371. $schema = new i18nSchema();
  372. $db->execute($db->dropSchema($schema));
  373. }
  374. /**
  375. * Test schema run create with no table args.
  376. *
  377. * @return void
  378. */
  379. public function testCreateWithTableArgs() {
  380. $db = ConnectionManager::getDataSource('test');
  381. $sources = $db->listSources();
  382. if (in_array('acos', $sources)) {
  383. $this->markTestSkipped('acos table already exists, cannot try to create it again.');
  384. }
  385. $this->Shell->params = array(
  386. 'connection' => 'test',
  387. 'name' => 'DbAcl',
  388. 'path' => APP . 'Config' . DS . 'Schema'
  389. );
  390. $this->Shell->args = array('DbAcl', 'acos');
  391. $this->Shell->startup();
  392. $this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
  393. $this->Shell->create();
  394. $db = ConnectionManager::getDataSource('test');
  395. $db->cacheSources = false;
  396. $sources = $db->listSources();
  397. $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources), 'acos should be present.');
  398. $this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources), 'aros should not be found.');
  399. $this->assertFalse(in_array('aros_acos', $sources), 'aros_acos should not be found.');
  400. $schema = new DbAclSchema();
  401. $db->execute($db->dropSchema($schema, 'acos'));
  402. }
  403. /**
  404. * test run update with a table arg.
  405. *
  406. * @return void
  407. */
  408. public function testUpdateWithTable() {
  409. $this->Shell = $this->getMock(
  410. 'SchemaShell',
  411. array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'),
  412. array(&$this->Dispatcher)
  413. );
  414. $this->Shell->params = array(
  415. 'connection' => 'test',
  416. 'force' => true
  417. );
  418. $this->Shell->args = array('SchemaShellTest', 'articles');
  419. $this->Shell->startup();
  420. $this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
  421. $this->Shell->expects($this->once())->method('_run')
  422. ->with($this->arrayHasKey('articles'), 'update', $this->isInstanceOf('CakeSchema'));
  423. $this->Shell->update();
  424. }
  425. /**
  426. * test that the plugin param creates the correct path in the schema object.
  427. *
  428. * @return void
  429. */
  430. public function testPluginParam() {
  431. App::build(array(
  432. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  433. ));
  434. CakePlugin::load('TestPlugin');
  435. $this->Shell->params = array(
  436. 'plugin' => 'TestPlugin',
  437. 'connection' => 'test'
  438. );
  439. $this->Shell->startup();
  440. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Config' . DS . 'Schema';
  441. $this->assertEquals($expected, $this->Shell->Schema->path);
  442. CakePlugin::unload();
  443. }
  444. /**
  445. * test that underscored names also result in CamelCased class names
  446. *
  447. * @return void
  448. */
  449. public function testName() {
  450. App::build(array(
  451. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  452. ));
  453. CakePlugin::load('TestPlugin');
  454. $this->Shell->params = array(
  455. 'plugin' => 'TestPlugin',
  456. 'connection' => 'test',
  457. 'name' => 'custom_name',
  458. 'force' => false,
  459. 'overwrite' => true,
  460. );
  461. $this->Shell->startup();
  462. if (file_exists($this->Shell->Schema->path . DS . 'custom_name.php')) {
  463. unlink($this->Shell->Schema->path . DS . 'custom_name.php');
  464. }
  465. $this->Shell->generate();
  466. $contents = file_get_contents($this->Shell->Schema->path . DS . 'custom_name.php');
  467. $this->assertRegExp('/class CustomNameSchema/', $contents);
  468. unlink($this->Shell->Schema->path . DS . 'custom_name.php');
  469. CakePlugin::unload();
  470. }
  471. /**
  472. * test that using Plugin.name with write.
  473. *
  474. * @return void
  475. */
  476. public function testPluginDotSyntaxWithCreate() {
  477. App::build(array(
  478. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  479. ));
  480. CakePlugin::load('TestPlugin');
  481. $this->Shell->params = array(
  482. 'connection' => 'test'
  483. );
  484. $this->Shell->args = array('TestPlugin.TestPluginApp');
  485. $this->Shell->startup();
  486. $this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
  487. $this->Shell->create();
  488. $db = ConnectionManager::getDataSource('test');
  489. $sources = $db->listSources();
  490. $this->assertTrue(in_array($db->config['prefix'] . 'test_plugin_acos', $sources));
  491. $schema = new TestPluginAppSchema();
  492. $db->execute($db->dropSchema($schema, 'test_plugin_acos'));
  493. CakePlugin::unload();
  494. }
  495. }