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\ParametrizedSqlMigrationQuery; |
8
|
|
|
|
9
|
|
|
class ParametrizedSqlMigrationQueryTest 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 testConstructor() |
25
|
|
|
{ |
26
|
|
|
$query = new ParametrizedSqlMigrationQuery( |
27
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
28
|
|
|
['name' => 'test'], |
29
|
|
|
['name' => 'string'] |
30
|
|
|
); |
31
|
|
|
$query->setConnection($this->connection); |
32
|
|
|
|
33
|
|
|
$this->assertEquals( |
34
|
|
|
[ |
35
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
36
|
|
|
'Parameters:', |
37
|
|
|
'[name] = test', |
38
|
|
|
], |
39
|
|
|
$query->getDescription() |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetDescription() |
44
|
|
|
{ |
45
|
|
|
$query = new ParametrizedSqlMigrationQuery(); |
46
|
|
|
$query->setConnection($this->connection); |
47
|
|
|
|
48
|
|
|
$this->connection->expects($this->never()) |
49
|
|
|
->method('executeUpdate'); |
50
|
|
|
|
51
|
|
|
$this->addSqls($query); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( |
54
|
|
|
$this->getExpectedLogs(), |
55
|
|
|
$query->getDescription() |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExecute() |
60
|
|
|
{ |
61
|
|
|
$query = new ParametrizedSqlMigrationQuery(); |
62
|
|
|
$query->setConnection($this->connection); |
63
|
|
|
|
64
|
|
|
$logger = new ArrayLogger(); |
65
|
|
|
|
66
|
|
|
$this->connection->expects($this->at(0)) |
67
|
|
|
->method('executeUpdate') |
68
|
|
|
->with( |
69
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')' |
70
|
|
|
); |
71
|
|
|
$this->connection->expects($this->at(1)) |
72
|
|
|
->method('executeUpdate') |
73
|
|
|
->with( |
74
|
|
|
'INSERT INTO test_table (name) VALUES (?1)', |
75
|
|
|
['test'] |
76
|
|
|
); |
77
|
|
|
// expects $this->connection->getDatabasePlatform at(2) |
|
|
|
|
78
|
|
|
$this->connection->expects($this->at(3)) |
79
|
|
|
->method('executeUpdate') |
80
|
|
|
->with( |
81
|
|
|
'INSERT INTO test_table (name) VALUES (?1)', |
82
|
|
|
['test'], |
83
|
|
|
['string'] |
84
|
|
|
); |
85
|
|
|
$this->connection->expects($this->at(4)) |
86
|
|
|
->method('executeUpdate') |
87
|
|
|
->with( |
88
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
89
|
|
|
['name' => 'test'] |
90
|
|
|
); |
91
|
|
|
// expects $this->connection->getDatabasePlatform at(5) |
|
|
|
|
92
|
|
|
$this->connection->expects($this->at(6)) |
93
|
|
|
->method('executeUpdate') |
94
|
|
|
->with( |
95
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
96
|
|
|
['name' => 'test'], |
97
|
|
|
['name' => 'string'] |
98
|
|
|
); |
99
|
|
|
// expects $this->connection->getDatabasePlatform at(7) |
|
|
|
|
100
|
|
|
// expects $this->connection->getDatabasePlatform at(8) |
|
|
|
|
101
|
|
|
$this->connection->expects($this->at(9)) |
102
|
|
|
->method('executeUpdate') |
103
|
|
|
->with( |
104
|
|
|
'UPDATE test_table SET values = ?1 WHERE id = ?2', |
105
|
|
|
[[1, 2, 3], 1], |
106
|
|
|
['array', 'integer'] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->addSqls($query); |
110
|
|
|
$query->execute($logger); |
111
|
|
|
|
112
|
|
|
$this->assertEquals( |
113
|
|
|
$this->getExpectedLogs(), |
114
|
|
|
$logger->getMessages() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function addSqls(ParametrizedSqlMigrationQuery $query) |
119
|
|
|
{ |
120
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (\'name\')'); |
121
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (?1)', ['test']); |
122
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (?1)', ['test'], ['string']); |
123
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (:name)', ['name' => 'test']); |
124
|
|
|
$query->addSql('INSERT INTO test_table (name) VALUES (:name)', ['name' => 'test'], ['name' => 'string']); |
125
|
|
|
$query->addSql('UPDATE test_table SET values = ?1 WHERE id = ?2', [[1, 2, 3], 1], ['array', 'integer']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function getExpectedLogs() |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
'INSERT INTO test_table (name) VALUES (\'name\')', |
132
|
|
|
'INSERT INTO test_table (name) VALUES (?1)', |
133
|
|
|
'Parameters:', |
134
|
|
|
'[1] = test', |
135
|
|
|
'INSERT INTO test_table (name) VALUES (?1)', |
136
|
|
|
'Parameters:', |
137
|
|
|
'[1] = test', |
138
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
139
|
|
|
'Parameters:', |
140
|
|
|
'[name] = test', |
141
|
|
|
'INSERT INTO test_table (name) VALUES (:name)', |
142
|
|
|
'Parameters:', |
143
|
|
|
'[name] = test', |
144
|
|
|
'UPDATE test_table SET values = ?1 WHERE id = ?2', |
145
|
|
|
'Parameters:', |
146
|
|
|
'[1] = a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}', |
147
|
|
|
'[2] = 1', |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.