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

namespace Doctrine\Tests\DBAL\Schema;

Sergei Morozov's avatar
Sergei Morozov committed
5 6 7
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
8 9
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;
Sergei Morozov's avatar
Sergei Morozov committed
10
use PHPUnit\Framework\TestCase;
11

Sergei Morozov's avatar
Sergei Morozov committed
12
class ColumnTest extends TestCase
13 14 15
{
    public function testGet()
    {
16
        $column = $this->createColumn();
17

Sergei Morozov's avatar
Sergei Morozov committed
18
        self::assertEquals('foo', $column->getName());
19 20 21 22 23 24 25 26
        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());
Sergei Morozov's avatar
Sergei Morozov committed
27
        self::assertEquals('baz', $column->getDefault());
28

Sergei Morozov's avatar
Sergei Morozov committed
29
        self::assertEquals(['foo' => 'bar'], $column->getPlatformOptions());
30 31 32 33
        self::assertTrue($column->hasPlatformOption('foo'));
        self::assertEquals('bar', $column->getPlatformOption('foo'));
        self::assertFalse($column->hasPlatformOption('bar'));

Sergei Morozov's avatar
Sergei Morozov committed
34
        self::assertEquals(['bar' => 'baz'], $column->getCustomSchemaOptions());
35 36 37
        self::assertTrue($column->hasCustomSchemaOption('bar'));
        self::assertEquals('baz', $column->getCustomSchemaOption('bar'));
        self::assertFalse($column->hasCustomSchemaOption('foo'));
38
    }
39 40 41

    public function testToArray()
    {
Sergei Morozov's avatar
Sergei Morozov committed
42
        $expected = [
43 44 45 46 47 48 49 50 51
            'name' => 'foo',
            'type' => Type::getType('string'),
            'default' => 'baz',
            'notnull' => false,
            'length' => 200,
            'precision' => 5,
            'scale' => 2,
            'fixed' => true,
            'unsigned' => true,
52
            'autoincrement' => false,
53
            'columnDefinition' => null,
54
            'comment' => null,
55
            'foo' => 'bar',
Sergei Morozov's avatar
Sergei Morozov committed
56 57
            'bar' => 'baz',
        ];
58

59
        self::assertEquals($expected, $this->createColumn()->toArray());
60 61
    }

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

71 72 73 74 75 76 77 78 79 80 81 82 83
    /**
     * @group legacy
     * @expectedDeprecation The "unknown_option" column option is not supported, setting it is deprecated and will cause an error in Doctrine 3.0
     */
    public function testOptionsShouldNotBeIgnored() : void
    {
        $col1 = new Column('bar', Type::getType(Type::INTEGER), ['unknown_option' => 'bar', 'notnull' => true]);
        self::assertTrue($col1->getNotnull());

        $col2 = new Column('bar', Type::getType(Type::INTEGER), ['unknown_option' => 'bar', 'notnull' => false]);
        self::assertFalse($col2->getNotnull());
    }

84 85 86 87 88
    /**
     * @return Column
     */
    public function createColumn()
    {
Sergei Morozov's avatar
Sergei Morozov committed
89
        $options = [
90 91 92 93 94 95 96
            'length' => 200,
            'precision' => 5,
            'scale' => 2,
            'unsigned' => true,
            'notnull' => false,
            'fixed' => true,
            'default' => 'baz',
Sergei Morozov's avatar
Sergei Morozov committed
97 98 99
            'platformOptions' => ['foo' => 'bar'],
            'customSchemaOptions' => ['bar' => 'baz'],
        ];
100 101

        $string = Type::getType('string');
Sergei Morozov's avatar
Sergei Morozov committed
102
        return new Column('foo', $string, $options);
103
    }
104 105 106

    /**
     * @group DBAL-64
107
     * @group DBAL-830
108 109 110 111
     */
    public function testQuotedColumnName()
    {
        $string = Type::getType('string');
Sergei Morozov's avatar
Sergei Morozov committed
112
        $column = new Column('`bar`', $string, []);
113

Sergei Morozov's avatar
Sergei Morozov committed
114 115
        $mysqlPlatform  = new MySqlPlatform();
        $sqlitePlatform = new SqlitePlatform();
116

117 118 119
        self::assertEquals('bar', $column->getName());
        self::assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
        self::assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
120

Sergei Morozov's avatar
Sergei Morozov committed
121
        $column = new Column('[bar]', $string);
122

Sergei Morozov's avatar
Sergei Morozov committed
123
        $sqlServerPlatform = new SQLServerPlatform();
124

125 126
        self::assertEquals('bar', $column->getName());
        self::assertEquals('[bar]', $column->getQuotedName($sqlServerPlatform));
127 128 129 130 131 132 133 134
    }

    /**
     * @dataProvider getIsQuoted
     * @group DBAL-830
     */
    public function testIsQuoted($columnName, $isQuoted)
    {
Sergei Morozov's avatar
Sergei Morozov committed
135
        $type   = Type::getType('string');
136 137
        $column = new Column($columnName, $type);

138
        self::assertSame($isQuoted, $column->isQuoted());
139 140 141 142
    }

    public function getIsQuoted()
    {
Sergei Morozov's avatar
Sergei Morozov committed
143 144 145 146 147 148
        return [
            ['bar', false],
            ['`bar`', true],
            ['"bar"', true],
            ['[bar]', true],
        ];
149
    }
150 151 152 153 154 155

    /**
     * @group DBAL-42
     */
    public function testColumnComment()
    {
Sergei Morozov's avatar
Sergei Morozov committed
156
        $column = new Column('bar', Type::getType('string'));
157
        self::assertNull($column->getComment());
158

Sergei Morozov's avatar
Sergei Morozov committed
159 160
        $column->setComment('foo');
        self::assertEquals('foo', $column->getComment());
161 162

        $columnArray = $column->toArray();
163 164
        self::assertArrayHasKey('comment', $columnArray);
        self::assertEquals('foo', $columnArray['comment']);
165
    }
Luís Cobucci's avatar
Luís Cobucci committed
166
}