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

MySqlSchemaManagerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
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
62
    }
63
64
    public function getColumnDefinition()
65
    {
66
        return [
67
            [
68
                'Field'   => 'id',
69
                'Type'    => 'int(10) unsigned',
70
                'Null'    => 'NO',
71
                'Key'     => 'PRI',
72
                'Default' => null,
73
                'Extra'   => 'auto_increment',
74
                'Comment' => '',
75
                'CharacterSet' => null,
76
                'Collation' => null
77
            ],
78
            [
79
                'Field'   => 'updated_at',
80
                'Type'    => 'datetime',
81
                'Null'    => 'YES',
82
                'Key'     => '',
83
                '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...
84
                'Extra'   => '',
85
                'Comment' => 'Record last update timestamp',
86
                'CharacterSet' => null,
87
                'Collation' => null
88
            ],[
89
                'Field'=> 'created_by',
90
                'Type'=> 'varchar(40)',
91
                'Null'=> 'YES',
92
                'Key'=>  '',
93
                'Default'=> null, // MySQL 5.1 - 5.7 sends: null
94
                'Extra' => '',
95
                'Comment' => 'Creator name',
96
                'CharacterSet' => 'utf8',
97
                'Collation'=> 'utf8_unicode_ci'
98
            ]
99
        ];
100
101
    }
102
103
104
    public function getFKDefinition()
105
    {
106
        return array(
107
            array(
108
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
109
                "COLUMN_NAME" => "column_1",
110
                "REFERENCED_TABLE_NAME" => "dummy",
111
                "REFERENCED_COLUMN_NAME" => "column_1",
112
                "update_rule" => "RESTRICT",
113
                "delete_rule" => "RESTRICT",
114
            ),
115
            array(
116
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
117
                "COLUMN_NAME" => "column_2",
118
                "REFERENCED_TABLE_NAME" => "dummy",
119
                "REFERENCED_COLUMN_NAME" => "column_2",
120
                "update_rule" => "RESTRICT",
121
                "delete_rule" => "RESTRICT",
122
            ),
123
            array(
124
                "CONSTRAINT_NAME" => "FK_C1B1712387FE737264DE5A5511B8B3E",
125
                "COLUMN_NAME" => "column_3",
126
                "REFERENCED_TABLE_NAME" => "dummy",
127
                "REFERENCED_COLUMN_NAME" => "column_3",
128
                "update_rule" => "RESTRICT",
129
                "delete_rule" => "RESTRICT",
130
            )
131
        );
132
    }
133
}
134