Passed
Pull Request — master (#2846)
by Grégoire
14:16
created

ColumnTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 6
dl 0
loc 140
rs 10
c 0
b 0
f 0

8 Methods

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