Completed
Pull Request — master (#2825)
by Sébastien
04:34
created

MySqlSchemaManagerTest::getColumnDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 64
rs 9.3956
cc 1
eloc 47
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Configuration;
7
use Doctrine\DBAL\Schema\MySqlSchemaManager;
8
9
class MySqlSchemaManagerTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     *
13
     * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
14
     */
15
    private $manager;
16
17
    protected function setUp()
18
    {
19
        $eventManager = new EventManager();
20
        $driverMock = $this->createMock('Doctrine\DBAL\Driver');
21
        $platform = $this->createMock('Doctrine\DBAL\Platforms\MySqlPlatform');
22
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
0 ignored issues
show
Bug introduced by
The property conn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
            ->setMethods(array('fetchAll'))
24
            ->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager))
25
            ->getMock();
26
        $this->manager = new MySqlSchemaManager($this->conn);
27
    }
28
29
    public function testCompositeForeignKeys()
30
    {
31
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getFKDefinition()));
32
        $fkeys = $this->manager->listTableForeignKeys('dummy');
33
        $this->assertEquals(1, count($fkeys), "Table has to have one foreign key.");
34
35
        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
36
        $this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
37
        $this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getForeignColumns()));
38
    }
39
40
    public function testPortableTableColumnDefinition()
41
    {
42
        $eventManager = new EventManager();
43
        $driverMock = $this->createMock('Doctrine\DBAL\Driver');
44
        $platform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
45
        $conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
46
            ->setMethods(array('fetchAll'))
47
            ->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager))
48
            ->getMock();
49
        $this->manager = new MySqlSchemaManager($conn);
50
51
52
        $conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getColumnDefinition()));
53
        $columns = $this->manager->listTableColumns('dummy');
54
        $this->assertEquals(count($this->getColumnDefinition()), count($columns));
55
        $this->assertTrue($columns['id']->getNotnull());
56
        $this->assertNull($columns['id']->getDefault());
57
        $this->assertFalse($columns['updated_at']->getNotnull());
58
        $this->assertNull($columns['updated_at']->getDefault());
59
        $this->assertFalse($columns['created_by']->getNotnull());
60
        $this->assertNull($columns['created_by']->getDefault());
61
        $this->assertEquals(null, $columns['text_2']->getDefault());
62
        $this->assertEquals("'NULL'", $columns['text_1']->getDefault());
63
64
    }
65
66
    public function getColumnDefinition()
67
    {
68
        return [
69
            [
70
                'Field'   => 'id',
71
                'Type'    => 'int(10) unsigned',
72
                'Null'    => 'NO',
73
                'Key'     => 'PRI',
74
                'Default' => null,
75
                'Extra'   => 'auto_increment',
76
                'Comment' => '',
77
                'CharacterSet' => null,
78
                'Collation' => null
79
            ],[
80
                'Field'=> 'created_by',
81
                'Type'=> 'varchar(40)',
82
                'Null'=> 'YES',
83
                'Key'=>  '',
84
                'Default'=> null, // MySQL 5.1 - 5.7 sends: null
85
                'Extra' => '',
86
                'Comment' => 'Creator name',
87
                'CharacterSet' => 'utf8',
88
                'Collation'=> 'utf8_unicode_ci'
89
            ],
90
            // Mariadb since 10.2.7, see https://jira.mariadb.org/browse/MDEV-13341
91
            [
92
                'Field'   => 'updated_at',
93
                'Type'    => 'datetime',
94
                'Null'    => 'YES',
95
                'Key'     => '',
96
                'Default' => 'NULL', // MariaDB 10.2.7 return 'NULL'
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
                'Extra'   => '',
98
                'Comment' => 'Record last update timestamp',
99
                'CharacterSet' => null,
100
                'Collation' => null
101
            ],
102
            // Mariadb 10.2 test with defaults that could be
103
            // ambiguous (see text_1 and text_2 Default)
104
            [
105
                'Field'   => 'text_1',
106
                'Type'    => 'varchar(50)',
107
                'Null'    => 'YES',
108
                'Key'     => '',
109
                'Default' => "'NULL'",
110
                'Extra'   => '',
111
                'Comment' => '',
112
                'CharacterSet' => 'utf8',
113
                'Collation'=> 'utf8_unicode_ci'
114
            ],[
115
                'Field'   => 'text_2',
116
                'Type'    => 'varchar(50)',
117
                'Null'    => 'YES',
118
                'Key'     => '',
119
                'Default' => 'NULL',
120
                'Extra'   => '',
121
                'Comment' => '',
122
                'CharacterSet' => 'utf8',
123
                'Collation'=> 'utf8_unicode_ci'
124
            ],
125
126
127
        ];
128
129
    }
130
131
132
    public function getFKDefinition()
133
    {
134
        return array(
135
            array(
136
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
137
                "COLUMN_NAME" => "column_1",
138
                "REFERENCED_TABLE_NAME" => "dummy",
139
                "REFERENCED_COLUMN_NAME" => "column_1",
140
                "update_rule" => "RESTRICT",
141
                "delete_rule" => "RESTRICT",
142
            ),
143
            array(
144
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
145
                "COLUMN_NAME" => "column_2",
146
                "REFERENCED_TABLE_NAME" => "dummy",
147
                "REFERENCED_COLUMN_NAME" => "column_2",
148
                "update_rule" => "RESTRICT",
149
                "delete_rule" => "RESTRICT",
150
            ),
151
            array(
152
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
153
                "COLUMN_NAME" => "column_3",
154
                "REFERENCED_TABLE_NAME" => "dummy",
155
                "REFERENCED_COLUMN_NAME" => "column_3",
156
                "update_rule" => "RESTRICT",
157
                "delete_rule" => "RESTRICT",
158
            )
159
        );
160
    }
161
}
162