MDB2SchemaReaderTest.php 3.7 KB

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