ColumnTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Schema;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\DBAL\Schema\Column;
  7. use Doctrine\DBAL\Types\Type;
  8. class ColumnTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function testGet()
  11. {
  12. $column = $this->createColumn();
  13. $this->assertEquals("foo", $column->getName());
  14. $this->assertSame(Type::getType('string'), $column->getType());
  15. $this->assertEquals(200, $column->getLength());
  16. $this->assertEquals(5, $column->getPrecision());
  17. $this->assertEquals(2, $column->getScale());
  18. $this->assertTrue($column->getUnsigned());
  19. $this->assertFalse($column->getNotNull());
  20. $this->assertTrue($column->getFixed());
  21. $this->assertEquals("baz", $column->getDefault());
  22. $this->assertEquals(array('foo' => 'bar'), $column->getPlatformOptions());
  23. $this->assertTrue($column->hasPlatformOption('foo'));
  24. $this->assertEquals('bar', $column->getPlatformOption('foo'));
  25. $this->assertFalse($column->hasPlatformOption('bar'));
  26. $this->assertEquals(array('bar' => 'baz'), $column->getCustomSchemaOptions());
  27. $this->assertTrue($column->hasCustomSchemaOption('bar'));
  28. $this->assertEquals('baz', $column->getCustomSchemaOption('bar'));
  29. $this->assertFalse($column->hasCustomSchemaOption('foo'));
  30. }
  31. public function testToArray()
  32. {
  33. $expected = array(
  34. 'name' => 'foo',
  35. 'type' => Type::getType('string'),
  36. 'default' => 'baz',
  37. 'notnull' => false,
  38. 'length' => 200,
  39. 'precision' => 5,
  40. 'scale' => 2,
  41. 'fixed' => true,
  42. 'unsigned' => true,
  43. 'autoincrement' => false,
  44. 'columnDefinition' => null,
  45. 'comment' => null,
  46. 'foo' => 'bar',
  47. 'bar' => 'baz'
  48. );
  49. $this->assertEquals($expected, $this->createColumn()->toArray());
  50. }
  51. /**
  52. * @return Column
  53. */
  54. public function createColumn()
  55. {
  56. $options = array(
  57. 'length' => 200,
  58. 'precision' => 5,
  59. 'scale' => 2,
  60. 'unsigned' => true,
  61. 'notnull' => false,
  62. 'fixed' => true,
  63. 'default' => 'baz',
  64. 'platformOptions' => array('foo' => 'bar'),
  65. 'customSchemaOptions' => array('bar' => 'baz'),
  66. );
  67. $string = Type::getType('string');
  68. return new Column("foo", $string, $options);
  69. }
  70. /**
  71. * @group DBAL-64
  72. */
  73. public function testQuotedColumnName()
  74. {
  75. $string = Type::getType('string');
  76. $column = new Column("`bar`", $string, array());
  77. $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
  78. $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
  79. $this->assertEquals('bar', $column->getName());
  80. $this->assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
  81. $this->assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
  82. }
  83. /**
  84. * @group DBAL-42
  85. */
  86. public function testColumnComment()
  87. {
  88. $column = new Column("bar", Type::getType('string'));
  89. $this->assertNull($column->getComment());
  90. $column->setComment("foo");
  91. $this->assertEquals("foo", $column->getComment());
  92. $columnArray = $column->toArray();
  93. $this->assertArrayHasKey('comment', $columnArray);
  94. $this->assertEquals('foo', $columnArray['comment']);
  95. }
  96. }