Failed Conditions
Pull Request — master (#2846)
by Grégoire
13:02
created

testSettingUnknownOptionIsStillSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Exception\InvalidArgumentException;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Types\Type;
8
9
class ColumnTest extends \PHPUnit\Framework\TestCase
10
{
11
    public function testGet()
12
    {
13
        $column = $this->createColumn();
14
15
        self::assertEquals("foo", $column->getName());
16
        self::assertSame(Type::getType('string'), $column->getType());
17
18
        self::assertEquals(200, $column->getLength());
19
        self::assertEquals(5, $column->getPrecision());
20
        self::assertEquals(2, $column->getScale());
21
        self::assertTrue($column->getUnsigned());
22
        self::assertFalse($column->getNotNull());
23
        self::assertTrue($column->getFixed());
24
        self::assertEquals("baz", $column->getDefault());
25
26
        self::assertEquals(array('foo' => 'bar'), $column->getPlatformOptions());
27
        self::assertTrue($column->hasPlatformOption('foo'));
28
        self::assertEquals('bar', $column->getPlatformOption('foo'));
29
        self::assertFalse($column->hasPlatformOption('bar'));
30
31
        self::assertEquals(array('bar' => 'baz'), $column->getCustomSchemaOptions());
32
        self::assertTrue($column->hasCustomSchemaOption('bar'));
33
        self::assertEquals('baz', $column->getCustomSchemaOption('bar'));
34
        self::assertFalse($column->hasCustomSchemaOption('foo'));
35
    }
36
37
    public function testToArray()
38
    {
39
        $expected = array(
40
            'name' => 'foo',
41
            'type' => Type::getType('string'),
42
            'default' => 'baz',
43
            'notnull' => false,
44
            'length' => 200,
45
            'precision' => 5,
46
            'scale' => 2,
47
            'fixed' => true,
48
            '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" option is not supported, setting it is deprecated and will cause an exception in 3.0
62
     * @doesNotPerformAssertions
63
     */
64
    public function testSettingUnknownOptionIsStillSupported() : void
65
    {
66
        new Column('foo', $this->createMock(Type::class), ['unknown_option' => 'bar']);
67
    }
68
69
    /**
70
     * @return Column
71
     */
72
    public function createColumn()
73
    {
74
        $options = array(
75
            'length' => 200,
76
            'precision' => 5,
77
            'scale' => 2,
78
            'unsigned' => true,
79
            'notnull' => false,
80
            'fixed' => true,
81
            'default' => 'baz',
82
            'platformOptions' => array('foo' => 'bar'),
83
            'customSchemaOptions' => array('bar' => 'baz'),
84
        );
85
86
        $string = Type::getType('string');
87
        return new Column("foo", $string, $options);
88
    }
89
90
    /**
91
     * @group DBAL-64
92
     * @group DBAL-830
93
     */
94
    public function testQuotedColumnName()
95
    {
96
        $string = Type::getType('string');
97
        $column = new Column("`bar`", $string, array());
98
99
        $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
100
        $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
101
102
        self::assertEquals('bar', $column->getName());
103
        self::assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
104
        self::assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
105
106
        $column = new Column("[bar]", $string);
107
108
        $sqlServerPlatform = new \Doctrine\DBAL\Platforms\SQLServerPlatform();
109
110
        self::assertEquals('bar', $column->getName());
111
        self::assertEquals('[bar]', $column->getQuotedName($sqlServerPlatform));
112
    }
113
114
    /**
115
     * @dataProvider getIsQuoted
116
     * @group DBAL-830
117
     */
118
    public function testIsQuoted($columnName, $isQuoted)
119
    {
120
        $type = Type::getType('string');
121
        $column = new Column($columnName, $type);
122
123
        self::assertSame($isQuoted, $column->isQuoted());
124
    }
125
126
    public function getIsQuoted()
127
    {
128
        return array(
129
            array('bar', false),
130
            array('`bar`', true),
131
            array('"bar"', true),
132
            array('[bar]', true),
133
        );
134
    }
135
136
    /**
137
     * @group DBAL-42
138
     */
139
    public function testColumnComment()
140
    {
141
        $column = new Column("bar", Type::getType('string'));
142
        self::assertNull($column->getComment());
143
144
        $column->setComment("foo");
145
        self::assertEquals("foo", $column->getComment());
146
147
        $columnArray = $column->toArray();
148
        self::assertArrayHasKey('comment', $columnArray);
149
        self::assertEquals('foo', $columnArray['comment']);
150
    }
151
}
152