ColumnTest.php 4.57 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Schema;

5
use Doctrine\DBAL\Exception\InvalidArgumentException;
6 7 8
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;

Luís Cobucci's avatar
Luís Cobucci committed
9
class ColumnTest extends \PHPUnit\Framework\TestCase
10 11 12
{
    public function testGet()
    {
13
        $column = $this->createColumn();
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
        self::assertEquals("foo", $column->getName());
        self::assertSame(Type::getType('string'), $column->getType());

        self::assertEquals(200, $column->getLength());
        self::assertEquals(5, $column->getPrecision());
        self::assertEquals(2, $column->getScale());
        self::assertTrue($column->getUnsigned());
        self::assertFalse($column->getNotNull());
        self::assertTrue($column->getFixed());
        self::assertEquals("baz", $column->getDefault());

        self::assertEquals(array('foo' => 'bar'), $column->getPlatformOptions());
        self::assertTrue($column->hasPlatformOption('foo'));
        self::assertEquals('bar', $column->getPlatformOption('foo'));
        self::assertFalse($column->hasPlatformOption('bar'));

        self::assertEquals(array('bar' => 'baz'), $column->getCustomSchemaOptions());
        self::assertTrue($column->hasCustomSchemaOption('bar'));
        self::assertEquals('baz', $column->getCustomSchemaOption('bar'));
        self::assertFalse($column->hasCustomSchemaOption('foo'));
35
    }
36 37 38 39 40 41 42 43 44 45 46 47 48

    public function testToArray()
    {
        $expected = array(
            'name' => 'foo',
            'type' => Type::getType('string'),
            'default' => 'baz',
            'notnull' => false,
            'length' => 200,
            'precision' => 5,
            'scale' => 2,
            'fixed' => true,
            'unsigned' => true,
49
            'autoincrement' => false,
50
            'columnDefinition' => null,
51
            'comment' => null,
52
            'foo' => 'bar',
53
            'bar' => 'baz'
54 55
        );

56
        self::assertEquals($expected, $this->createColumn()->toArray());
57 58
    }

59 60
    /**
     * @group legacy
61
     * @expectedDeprecation The "unknown_option" column option is not supported, setting it is deprecated and will cause an error in Doctrine 3.0
62 63 64 65 66 67
     */
    public function testSettingUnknownOptionIsStillSupported() : void
    {
        new Column('foo', $this->createMock(Type::class), ['unknown_option' => 'bar']);
    }

68 69 70 71 72 73 74 75 76 77 78 79 80 81
    /**
     * @return Column
     */
    public function createColumn()
    {
        $options = array(
            'length' => 200,
            'precision' => 5,
            'scale' => 2,
            'unsigned' => true,
            'notnull' => false,
            'fixed' => true,
            'default' => 'baz',
            'platformOptions' => array('foo' => 'bar'),
82
            'customSchemaOptions' => array('bar' => 'baz'),
83 84 85 86 87
        );

        $string = Type::getType('string');
        return new Column("foo", $string, $options);
    }
88 89 90

    /**
     * @group DBAL-64
91
     * @group DBAL-830
92 93 94 95 96 97 98 99 100
     */
    public function testQuotedColumnName()
    {
        $string = Type::getType('string');
        $column = new Column("`bar`", $string, array());

        $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
        $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();

101 102 103
        self::assertEquals('bar', $column->getName());
        self::assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
        self::assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
104 105 106 107 108

        $column = new Column("[bar]", $string);

        $sqlServerPlatform = new \Doctrine\DBAL\Platforms\SQLServerPlatform();

109 110
        self::assertEquals('bar', $column->getName());
        self::assertEquals('[bar]', $column->getQuotedName($sqlServerPlatform));
111 112 113 114 115 116 117 118 119 120 121
    }

    /**
     * @dataProvider getIsQuoted
     * @group DBAL-830
     */
    public function testIsQuoted($columnName, $isQuoted)
    {
        $type = Type::getType('string');
        $column = new Column($columnName, $type);

122
        self::assertSame($isQuoted, $column->isQuoted());
123 124 125 126 127 128 129 130 131 132
    }

    public function getIsQuoted()
    {
        return array(
            array('bar', false),
            array('`bar`', true),
            array('"bar"', true),
            array('[bar]', true),
        );
133
    }
134 135 136 137 138 139

    /**
     * @group DBAL-42
     */
    public function testColumnComment()
    {
140
        $column = new Column("bar", Type::getType('string'));
141
        self::assertNull($column->getComment());
142 143

        $column->setComment("foo");
144
        self::assertEquals("foo", $column->getComment());
145 146

        $columnArray = $column->toArray();
147 148
        self::assertArrayHasKey('comment', $columnArray);
        self::assertEquals('foo', $columnArray['comment']);
149
    }
Luís Cobucci's avatar
Luís Cobucci committed
150
}