connection.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\DB;
  9. use Doctrine\DBAL\Platforms\SqlitePlatform;
  10. use OC\DB\MDB2SchemaManager;
  11. class Connection extends \Test\TestCase {
  12. /**
  13. * @var \OCP\IDBConnection
  14. */
  15. private $connection;
  16. public static function setUpBeforeClass()
  17. {
  18. self::dropTestTable();
  19. parent::setUpBeforeClass();
  20. }
  21. public static function tearDownAfterClass()
  22. {
  23. self::dropTestTable();
  24. parent::tearDownAfterClass();
  25. }
  26. protected static function dropTestTable()
  27. {
  28. if (\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite') !== 'oci') {
  29. \OC_DB::dropTable('table');
  30. }
  31. }
  32. public function setUp() {
  33. parent::setUp();
  34. $this->connection = \OC::$server->getDatabaseConnection();
  35. }
  36. /**
  37. * @param string $table
  38. */
  39. public function assertTableExist($table) {
  40. if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
  41. // sqlite removes the tables after closing the DB
  42. $this->assertTrue(true);
  43. } else {
  44. $this->assertTrue($this->connection->tableExists($table), 'Table ' . $table . ' exists.');
  45. }
  46. }
  47. /**
  48. * @param string $table
  49. */
  50. public function assertTableNotExist($table) {
  51. if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
  52. // sqlite removes the tables after closing the DB
  53. $this->assertTrue(true);
  54. } else {
  55. $this->assertFalse($this->connection->tableExists($table), 'Table ' . $table . ' doesnt exists.');
  56. }
  57. }
  58. private function makeTestTable() {
  59. $schemaManager = new MDB2SchemaManager($this->connection);
  60. $schemaManager->createDbFromStructure(__DIR__ . '/testschema.xml');
  61. }
  62. public function testTableExists() {
  63. $this->assertTableNotExist('table');
  64. $this->makeTestTable();
  65. $this->assertTableExist('table');
  66. }
  67. /**
  68. * @depends testTableExists
  69. */
  70. public function testDropTable() {
  71. $this->assertTableExist('table');
  72. $this->connection->dropTable('table');
  73. $this->assertTableNotExist('table');
  74. }
  75. }