ColumnTest.php 5.27 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Schema;
4

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

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

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

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

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

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

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

63 64
    /**
     * @group legacy
65
     * @expectedDeprecation The "unknown_option" column option is not supported, setting it is deprecated and will cause an error in Doctrine DBAL 3.0
66 67 68
     */
    public function testSettingUnknownOptionIsStillSupported() : void
    {
69 70
        $this->expectNotToPerformAssertions();

71 72 73
        new Column('foo', $this->createMock(Type::class), ['unknown_option' => 'bar']);
    }

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

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

87
    public function createColumn() : Column
88
    {
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');
102

Sergei Morozov's avatar
Sergei Morozov committed
103
        return new Column('foo', $string, $options);
104
    }
105 106 107

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

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

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

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

124
        $sqlServerPlatform = new SQLServer2012Platform();
125

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

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

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

142 143 144 145
    /**
     * @return mixed[][]
     */
    public static function getIsQuoted() : iterable
146
    {
Sergei Morozov's avatar
Sergei Morozov committed
147 148 149 150 151 152
        return [
            ['bar', false],
            ['`bar`', true],
            ['"bar"', true],
            ['[bar]', true],
        ];
153
    }
154 155 156 157

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

Sergei Morozov's avatar
Sergei Morozov committed
163 164
        $column->setComment('foo');
        self::assertEquals('foo', $column->getComment());
165 166

        $columnArray = $column->toArray();
167 168
        self::assertArrayHasKey('comment', $columnArray);
        self::assertEquals('foo', $columnArray['comment']);
169
    }
Luís Cobucci's avatar
Luís Cobucci committed
170
}