ColumnTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. }
  27. public function testToArray()
  28. {
  29. $expected = array(
  30. 'name' => 'foo',
  31. 'type' => Type::getType('string'),
  32. 'default' => 'baz',
  33. 'notnull' => false,
  34. 'length' => 200,
  35. 'precision' => 5,
  36. 'scale' => 2,
  37. 'fixed' => true,
  38. 'unsigned' => true,
  39. 'autoincrement' => false,
  40. 'columnDefinition' => null,
  41. 'comment' => null,
  42. 'foo' => 'bar',
  43. );
  44. $this->assertEquals($expected, $this->createColumn()->toArray());
  45. }
  46. /**
  47. * @return Column
  48. */
  49. public function createColumn()
  50. {
  51. $options = array(
  52. 'length' => 200,
  53. 'precision' => 5,
  54. 'scale' => 2,
  55. 'unsigned' => true,
  56. 'notnull' => false,
  57. 'fixed' => true,
  58. 'default' => 'baz',
  59. 'platformOptions' => array('foo' => 'bar'),
  60. );
  61. $string = Type::getType('string');
  62. return new Column("foo", $string, $options);
  63. }
  64. /**
  65. * @group DBAL-64
  66. */
  67. public function testQuotedColumnName()
  68. {
  69. $string = Type::getType('string');
  70. $column = new Column("`bar`", $string, array());
  71. $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
  72. $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
  73. $this->assertEquals('bar', $column->getName());
  74. $this->assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
  75. $this->assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
  76. }
  77. /**
  78. * @group DBAL-42
  79. */
  80. public function testColumnComment()
  81. {
  82. $column = new Column("bar", Type::getType('string'));
  83. $this->assertNull($column->getComment());
  84. $column->setComment("foo");
  85. $this->assertEquals("foo", $column->getComment());
  86. $columnArray = $column->toArray();
  87. $this->assertArrayHasKey('comment', $columnArray);
  88. $this->assertEquals('foo', $columnArray['comment']);
  89. }
  90. }