1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
6
|
|
|
use RDV\Bundle\MigrationBundle\Migration\ArrayLogger; |
7
|
|
|
use RDV\Bundle\MigrationBundle\Migration\SqlMigrationQuery; |
8
|
|
|
|
9
|
|
|
class SqlMigrationQueryTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
12
|
|
|
protected $connection; |
13
|
|
|
|
14
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$this->connection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
17
|
|
|
->disableOriginalConstructor() |
18
|
|
|
->getMock(); |
19
|
|
|
$this->connection->expects($this->any()) |
20
|
|
|
->method('getDatabasePlatform') |
21
|
|
|
->will($this->returnValue(new MySqlPlatform())); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testConstructorWithString() |
25
|
|
|
{ |
26
|
|
|
$query = new SqlMigrationQuery( |
27
|
|
|
'INSERT INTO test_table (name) VALUES (:name)' |
28
|
|
|
); |
29
|
|
|
$query->setConnection($this->connection); |
30
|
|
|
|
31
|
|
|
$this->assertEquals( |
32
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
33
|
|
|
$query->getDescription() |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testConstructorWithArray() |
38
|
|
|
{ |
39
|
|
|
$query = new SqlMigrationQuery( |
40
|
|
|
[ |
41
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
42
|
|
|
'INSERT INTO test_table (test) VALUES (1)' |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
$query->setConnection($this->connection); |
46
|
|
|
|
47
|
|
|
$this->assertEquals( |
48
|
|
|
[ |
49
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
50
|
|
|
'INSERT INTO test_table (test) VALUES (1)' |
51
|
|
|
], |
52
|
|
|
$query->getDescription() |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetDescriptionWithoutSql() |
57
|
|
|
{ |
58
|
|
|
$query = new SqlMigrationQuery(); |
59
|
|
|
$query->setConnection($this->connection); |
60
|
|
|
|
61
|
|
|
$this->assertEquals('', $query->getDescription()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetDescription() |
65
|
|
|
{ |
66
|
|
|
$query = new SqlMigrationQuery(); |
67
|
|
|
$query->setConnection($this->connection); |
68
|
|
|
|
69
|
|
|
$this->connection->expects($this->never()) |
70
|
|
|
->method('executeUpdate'); |
71
|
|
|
|
72
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (\'name\')'); |
73
|
|
|
$this->assertEquals( |
74
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')', |
75
|
|
|
$query->getDescription() |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$query->addSql('INSERT INTO test_table (test) VALUES (1)'); |
79
|
|
|
$this->assertEquals( |
80
|
|
|
[ |
81
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')', |
82
|
|
|
'INSERT INTO test_table (test) VALUES (1)', |
83
|
|
|
], |
84
|
|
|
$query->getDescription() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testExecute() |
89
|
|
|
{ |
90
|
|
|
$query = new SqlMigrationQuery(); |
91
|
|
|
$query->setConnection($this->connection); |
92
|
|
|
|
93
|
|
|
$logger = new ArrayLogger(); |
94
|
|
|
|
95
|
|
|
$this->connection->expects($this->at(0)) |
96
|
|
|
->method('executeUpdate') |
97
|
|
|
->with('INSERT INTO test_table (name) VALUES (\'name\')'); |
98
|
|
|
$this->connection->expects($this->at(1)) |
99
|
|
|
->method('executeUpdate') |
100
|
|
|
->with('INSERT INTO test_table (name) VALUES (\'name\')'); |
101
|
|
|
$this->connection->expects($this->at(2)) |
102
|
|
|
->method('executeUpdate') |
103
|
|
|
->with('INSERT INTO test_table (test) VALUES (1)'); |
104
|
|
|
|
105
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (\'name\')'); |
106
|
|
|
$query->execute($logger); |
107
|
|
|
$this->assertEquals( |
108
|
|
|
[ |
109
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')' |
110
|
|
|
], |
111
|
|
|
$logger->getMessages() |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$query->addSql('INSERT INTO test_table (test) VALUES (1)'); |
115
|
|
|
$query->execute($logger); |
116
|
|
|
$this->assertEquals( |
117
|
|
|
[ |
118
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')', |
119
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')', |
120
|
|
|
'INSERT INTO test_table (test) VALUES (1)', |
121
|
|
|
], |
122
|
|
|
$logger->getMessages() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.