mdb2schemareader.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 \PHPUnit_Framework_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('\OC\Config')
  20. ->disableOriginalConstructor()
  21. ->getMock();
  22. $config->expects($this->any())
  23. ->method('getValue')
  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(7, $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->assertCount(2, $table->getIndexes());
  59. $this->assertEquals(array('integerfield'), $table->getIndex('primary')->getUnquotedColumns());
  60. $this->assertTrue($table->getIndex('primary')->isPrimary());
  61. $this->assertTrue($table->getIndex('primary')->isUnique());
  62. $this->assertEquals(array('booleanfield'), $table->getIndex('index_boolean')->getUnquotedColumns());
  63. $this->assertFalse($table->getIndex('index_boolean')->isPrimary());
  64. $this->assertFalse($table->getIndex('index_boolean')->isUnique());
  65. }
  66. }