Failed Conditions
Pull Request — master (#2825)
by Sébastien
07:34
created

MySqlSchemaManagerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 10
dl 0
loc 134
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testCompositeForeignKeys() 0 10 1
B testPortableTableColumnDefinition() 0 31 2
B getColumnDefinition() 0 39 3
B getFKDefinition() 0 29 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Configuration;
7
use Doctrine\DBAL\Platforms\MariaDb102Platform;
8
use Doctrine\DBAL\Schema\MySqlSchemaManager;
9
10
class MySqlSchemaManagerTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     *
14
     * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
15
     */
16
    private $manager;
17
18
    protected function setUp()
19
    {
20
        $eventManager = new EventManager();
21
        $driverMock = $this->createMock('Doctrine\DBAL\Driver');
22
        $platform = $this->createMock('Doctrine\DBAL\Platforms\MySqlPlatform');
23
        $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...
24
            ->setMethods(array('fetchAll'))
25
            ->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager))
26
            ->getMock();
27
        $this->manager = new MySqlSchemaManager($this->conn);
28
    }
29
30
    public function testCompositeForeignKeys()
31
    {
32
        $this->conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getFKDefinition()));
33
        $fkeys = $this->manager->listTableForeignKeys('dummy');
34
        $this->assertEquals(1, count($fkeys), "Table has to have one foreign key.");
35
36
        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
37
        $this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
38
        $this->assertEquals(array('column_1', 'column_2', 'column_3'), array_map('strtolower', $fkeys[0]->getForeignColumns()));
39
    }
40
41
    public function testPortableTableColumnDefinition()
42
    {
43
        $eventManager = new EventManager();
44
        $driverMock = $this->createMock('Doctrine\DBAL\Driver');
45
46
        $platforms = [
47
            'mysql' => new \Doctrine\DBAL\Platforms\MySqlPlatform(),
48
            'mariadb10.2' => new \Doctrine\DBAL\Platforms\MariaDb102Platform()
49
        ];
50
51
        foreach ($platforms as $platformName => $platform) {
52
53
            $conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
54
                ->setMethods(array('fetchAll'))
55
                ->setConstructorArgs(array(array('platform' => $platform), $driverMock, new Configuration(), $eventManager))
56
                ->getMock();
57
            $this->manager = new MySqlSchemaManager($conn);
58
59
            $conn->expects($this->once())->method('fetchAll')->will($this->returnValue($this->getColumnDefinition($platform)));
60
            $columns = $this->manager->listTableColumns('dummy');
61
62
            $this->assertFalse($columns['id']->getNotnull());
63
            $this->assertNull($columns['id']->getDefault());
64
65
            $this->assertTrue($columns['created_by']->getNotnull());
66
            $this->assertNull($columns['created_by']->getDefault());
67
68
            $this->assertFalse($columns['text_1']->getNotnull());
69
            $this->assertEquals('NULL', $columns['text_1']->getDefault());
70
        }
71
    }
72
73
    public function getColumnDefinition($platform)
74
    {
75
        $mariaDb102 = ($platform instanceof MariaDb102Platform);
76
        return [
77
            [
78
                'Field'   => 'id',
79
                'Type'    => 'int(10) unsigned',
80
                'Null'    => 'YES',
81
                'Key'     => '',
82
                'Default' => $mariaDb102 ? 'NULL' : null,
83
                'Extra'   => 'auto_increment',
84
                'Comment' => '',
85
                'CharacterSet' => null,
86
                'Collation' => null
87
            ],[
88
                'Field'=> 'created_by',
89
                'Type'=> 'varchar(40)',
90
                'Null'=> 'NO',
91
                'Key'=>  '',
92
                'Default'=> null, // both mysql and mariadb will return null (not null column)
93
                'Extra' => '',
94
                'Comment' => 'Creator name',
95
                'CharacterSet' => 'utf8',
96
                'Collation'=> 'utf8_unicode_ci'
97
            ],
98
            [
99
                'Field'   => 'text_1',
100
                'Type'    => 'varchar(50)',
101
                'Null'    => 'YES',
102
                'Key'     => '',
103
                'Default' => $mariaDb102 ? "'NULL'" : 'NULL',
104
                'Extra'   => '',
105
                'Comment' => '',
106
                'CharacterSet' => 'utf8',
107
                'Collation'=> 'utf8_unicode_ci'
108
            ]
109
        ];
110
111
    }
112
113
114
    public function getFKDefinition()
115
    {
116
        return array(
117
            array(
118
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
119
                "COLUMN_NAME" => "column_1",
120
                "REFERENCED_TABLE_NAME" => "dummy",
121
                "REFERENCED_COLUMN_NAME" => "column_1",
122
                "update_rule" => "RESTRICT",
123
                "delete_rule" => "RESTRICT",
124
            ),
125
            array(
126
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
127
                "COLUMN_NAME" => "column_2",
128
                "REFERENCED_TABLE_NAME" => "dummy",
129
                "REFERENCED_COLUMN_NAME" => "column_2",
130
                "update_rule" => "RESTRICT",
131
                "delete_rule" => "RESTRICT",
132
            ),
133
            array(
134
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
135
                "COLUMN_NAME" => "column_3",
136
                "REFERENCED_TABLE_NAME" => "dummy",
137
                "REFERENCED_COLUMN_NAME" => "column_3",
138
                "update_rule" => "RESTRICT",
139
                "delete_rule" => "RESTRICT",
140
            )
141
        );
142
    }
143
}
144