dbschema.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_DBSchema extends PHPUnit_Framework_TestCase {
  9. protected $schema_file = 'static://test_db_scheme';
  10. protected $schema_file2 = 'static://test_db_scheme2';
  11. protected $table1;
  12. protected $table2;
  13. public function setUp() {
  14. $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
  15. $dbfile2 = OC::$SERVERROOT.'/tests/data/db_structure2.xml';
  16. $r = '_'.OC_Util::generateRandomBytes(4).'_';
  17. $content = file_get_contents( $dbfile );
  18. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  19. file_put_contents( $this->schema_file, $content );
  20. $content = file_get_contents( $dbfile2 );
  21. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  22. file_put_contents( $this->schema_file2, $content );
  23. $this->table1 = $r.'cntcts_addrsbks';
  24. $this->table2 = $r.'cntcts_cards';
  25. }
  26. public function tearDown() {
  27. unlink($this->schema_file);
  28. unlink($this->schema_file2);
  29. }
  30. // everything in one test, they depend on each other
  31. /**
  32. * @medium
  33. */
  34. public function testSchema() {
  35. $this->doTestSchemaCreating();
  36. $this->doTestSchemaChanging();
  37. $this->doTestSchemaDumping();
  38. $this->doTestSchemaRemoving();
  39. }
  40. public function doTestSchemaCreating() {
  41. OC_DB::createDbFromStructure($this->schema_file);
  42. $this->assertTableExist($this->table1);
  43. $this->assertTableExist($this->table2);
  44. }
  45. public function doTestSchemaChanging() {
  46. OC_DB::updateDbFromStructure($this->schema_file2);
  47. $this->assertTableExist($this->table2);
  48. }
  49. public function doTestSchemaDumping() {
  50. $outfile = 'static://db_out.xml';
  51. OC_DB::getDbStructure($outfile);
  52. $content = file_get_contents($outfile);
  53. $this->assertContains($this->table1, $content);
  54. $this->assertContains($this->table2, $content);
  55. }
  56. public function doTestSchemaRemoving() {
  57. OC_DB::removeDBStructure($this->schema_file);
  58. $this->assertTableNotExist($this->table1);
  59. $this->assertTableNotExist($this->table2);
  60. }
  61. /**
  62. * @param string $table
  63. */
  64. public function assertTableExist($table) {
  65. $this->assertTrue(OC_DB::tableExists($table), 'Table ' . $table . ' does not exist');
  66. }
  67. /**
  68. * @param string $table
  69. */
  70. public function assertTableNotExist($table) {
  71. $type=OC_Config::getValue( "dbtype", "sqlite" );
  72. if( $type == 'sqlite' || $type == 'sqlite3' ) {
  73. // sqlite removes the tables after closing the DB
  74. $this->assertTrue(true);
  75. } else {
  76. $this->assertFalse(OC_DB::tableExists($table), 'Table ' . $table . ' exists.');
  77. }
  78. }
  79. }