mdb2schemareader.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright (c) 2013 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\MySqlPlatform;
  10. class MDB2SchemaReader extends \Test\TestCase {
  11. /**
  12. * @var \OC\DB\MDB2SchemaReader $reader
  13. */
  14. protected $reader;
  15. /**
  16. * @return \OC\Config
  17. */
  18. protected function getConfig() {
  19. $config = $this->getMockBuilder('\OCP\IConfig')
  20. ->disableOriginalConstructor()
  21. ->getMock();
  22. $config->expects($this->any())
  23. ->method('getSystemValue')
  24. ->will($this->returnValueMap(array(
  25. array('dbname', 'owncloud', 'testDB'),
  26. array('dbtableprefix', 'oc_', 'test_')
  27. )));
  28. return $config;
  29. }
  30. public function testRead() {
  31. $reader = new \OC\DB\MDB2SchemaReader($this->getConfig(), new MySqlPlatform());
  32. $schema = $reader->loadSchemaFromFile(__DIR__ . '/testschema.xml');
  33. $this->assertCount(1, $schema->getTables());
  34. $table = $schema->getTable('test_table');
  35. $this->assertCount(8, $table->getColumns());
  36. $this->assertEquals(4, $table->getColumn('integerfield')->getLength());
  37. $this->assertTrue($table->getColumn('integerfield')->getAutoincrement());
  38. $this->assertNull($table->getColumn('integerfield')->getDefault());
  39. $this->assertTrue($table->getColumn('integerfield')->getNotnull());
  40. $this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $table->getColumn('integerfield')->getType());
  41. $this->assertSame(10, $table->getColumn('integerfield_default')->getDefault());
  42. $this->assertEquals(32, $table->getColumn('textfield')->getLength());
  43. $this->assertFalse($table->getColumn('textfield')->getAutoincrement());
  44. $this->assertSame('foo', $table->getColumn('textfield')->getDefault());
  45. $this->assertTrue($table->getColumn('textfield')->getNotnull());
  46. $this->assertInstanceOf('Doctrine\DBAL\Types\StringType', $table->getColumn('textfield')->getType());
  47. $this->assertNull($table->getColumn('clobfield')->getLength());
  48. $this->assertFalse($table->getColumn('clobfield')->getAutoincrement());
  49. $this->assertNull($table->getColumn('clobfield')->getDefault());
  50. $this->assertTrue($table->getColumn('clobfield')->getNotnull());
  51. $this->assertInstanceOf('Doctrine\DBAL\Types\TextType', $table->getColumn('clobfield')->getType());
  52. $this->assertNull($table->getColumn('booleanfield')->getLength());
  53. $this->assertFalse($table->getColumn('booleanfield')->getAutoincrement());
  54. $this->assertNull($table->getColumn('booleanfield')->getDefault());
  55. $this->assertInstanceOf('Doctrine\DBAL\Types\BooleanType', $table->getColumn('booleanfield')->getType());
  56. $this->assertTrue($table->getColumn('booleanfield_true')->getDefault());
  57. $this->assertFalse($table->getColumn('booleanfield_false')->getDefault());
  58. $this->assertEquals(12, $table->getColumn('decimalfield_precision_scale')->getPrecision());
  59. $this->assertEquals(2, $table->getColumn('decimalfield_precision_scale')->getScale());
  60. $this->assertCount(2, $table->getIndexes());
  61. $this->assertEquals(array('integerfield'), $table->getIndex('primary')->getUnquotedColumns());
  62. $this->assertTrue($table->getIndex('primary')->isPrimary());
  63. $this->assertTrue($table->getIndex('primary')->isUnique());
  64. $this->assertEquals(array('booleanfield'), $table->getIndex('index_boolean')->getUnquotedColumns());
  65. $this->assertFalse($table->getIndex('index_boolean')->isPrimary());
  66. $this->assertFalse($table->getIndex('index_boolean')->isUnique());
  67. }
  68. }