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

ColumnTest::testToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 17
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
     */
62
    public function testSettingUnknownOptionIsStillSupported() : void
63
    {
64
        $this->assertInstanceOf(
65
            Column::class,
66
            new Column('foo', $this->createMock(Type::class), ['unknown_option' => 'bar'])
67
        );
68
    }
69
70
    /**
71
     * @return Column
72
     */
73
    public function createColumn()
74
    {
75
        $options = array(
76
            'length' => 200,
77
            'precision' => 5,
78
            'scale' => 2,
79
            'unsigned' => true,
80
            'notnull' => false,
81
            'fixed' => true,
82
            'default' => 'baz',
83
            'platformOptions' => array('foo' => 'bar'),
84
            'customSchemaOptions' => array('bar' => 'baz'),
85
        );
86
87
        $string = Type::getType('string');
88
        return new Column("foo", $string, $options);
89
    }
90
91
    /**
92
     * @group DBAL-64
93
     * @group DBAL-830
94
     */
95
    public function testQuotedColumnName()
96
    {
97
        $string = Type::getType('string');
98
        $column = new Column("`bar`", $string, array());
99
100
        $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
101
        $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
102
103
        self::assertEquals('bar', $column->getName());
104
        self::assertEquals('`bar`', $column->getQuotedName($mysqlPlatform));
105
        self::assertEquals('"bar"', $column->getQuotedName($sqlitePlatform));
106
107
        $column = new Column("[bar]", $string);
108
109
        $sqlServerPlatform = new \Doctrine\DBAL\Platforms\SQLServerPlatform();
110
111
        self::assertEquals('bar', $column->getName());
112
        self::assertEquals('[bar]', $column->getQuotedName($sqlServerPlatform));
113
    }
114
115
    /**
116
     * @dataProvider getIsQuoted
117
     * @group DBAL-830
118
     */
119
    public function testIsQuoted($columnName, $isQuoted)
120
    {
121
        $type = Type::getType('string');
122
        $column = new Column($columnName, $type);
123
124
        self::assertSame($isQuoted, $column->isQuoted());
125
    }
126
127
    public function getIsQuoted()
128
    {
129
        return array(
130
            array('bar', false),
131
            array('`bar`', true),
132
            array('"bar"', true),
133
            array('[bar]', true),
134
        );
135
    }
136
137
    /**
138
     * @group DBAL-42
139
     */
140
    public function testColumnComment()
141
    {
142
        $column = new Column("bar", Type::getType('string'));
143
        self::assertNull($column->getComment());
144
145
        $column->setComment("foo");
146
        self::assertEquals("foo", $column->getComment());
147
148
        $columnArray = $column->toArray();
149
        self::assertArrayHasKey('comment', $columnArray);
150
        self::assertEquals('foo', $columnArray['comment']);
151
    }
152
}
153