|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration; |
|
4
|
|
|
|
|
5
|
|
|
use RDV\Bundle\MigrationBundle\Migration\MigrationQueryExecutor; |
|
6
|
|
|
|
|
7
|
|
|
class MigrationQueryExecutorTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
|
10
|
|
|
protected $connection; |
|
11
|
|
|
|
|
12
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
|
13
|
|
|
protected $logger; |
|
14
|
|
|
|
|
15
|
|
|
/** @var MigrationQueryExecutor */ |
|
16
|
|
|
protected $executor; |
|
17
|
|
|
|
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->connection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
|
21
|
|
|
->disableOriginalConstructor() |
|
22
|
|
|
->getMock(); |
|
23
|
|
|
$this->logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
24
|
|
|
$this->executor = new MigrationQueryExecutor($this->connection); |
|
25
|
|
|
$this->executor->setLogger($this->logger); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testGetConnection() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->assertSame($this->connection, $this->executor->getConnection()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
View Code Duplication |
public function testExecuteSql() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$query = 'DELETE FROM some_table'; |
|
36
|
|
|
|
|
37
|
|
|
$this->logger->expects($this->once()) |
|
38
|
|
|
->method('notice') |
|
39
|
|
|
->with($query); |
|
40
|
|
|
$this->connection->expects($this->once()) |
|
41
|
|
|
->method('executeQuery') |
|
42
|
|
|
->with($query); |
|
43
|
|
|
|
|
44
|
|
|
$this->executor->execute($query, false); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
public function testExecuteSqlDryRun() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$query = 'DELETE FROM some_table'; |
|
50
|
|
|
|
|
51
|
|
|
$this->logger->expects($this->once()) |
|
52
|
|
|
->method('notice') |
|
53
|
|
|
->with($query); |
|
54
|
|
|
$this->connection->expects($this->never()) |
|
55
|
|
|
->method('executeQuery'); |
|
56
|
|
|
|
|
57
|
|
|
$this->executor->execute($query, true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
public function testExecuteMigrationQuery() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery'); |
|
63
|
|
|
|
|
64
|
|
|
$query->expects($this->never()) |
|
65
|
|
|
->method('setConnection'); |
|
66
|
|
|
$query->expects($this->once()) |
|
67
|
|
|
->method('execute') |
|
68
|
|
|
->with($this->identicalTo($this->logger)); |
|
69
|
|
|
|
|
70
|
|
|
$this->executor->execute($query, false); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testExecuteConnectionAwareMigrationQuery() |
|
74
|
|
|
{ |
|
75
|
|
|
$query = $this->getMockBuilder('RDV\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery') |
|
76
|
|
|
->disableOriginalConstructor() |
|
77
|
|
|
->getMock(); |
|
78
|
|
|
|
|
79
|
|
|
$query->expects($this->once()) |
|
80
|
|
|
->method('setConnection') |
|
81
|
|
|
->with($this->identicalTo($this->connection)); |
|
82
|
|
|
$query->expects($this->once()) |
|
83
|
|
|
->method('execute') |
|
84
|
|
|
->with($this->identicalTo($this->logger)); |
|
85
|
|
|
|
|
86
|
|
|
$this->executor->execute($query, false); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function testExecuteMigrationQueryDryRun() |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$queryDescription = 'test query'; |
|
92
|
|
|
|
|
93
|
|
|
$query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery'); |
|
94
|
|
|
|
|
95
|
|
|
$query->expects($this->once()) |
|
96
|
|
|
->method('getDescription') |
|
97
|
|
|
->will($this->returnValue($queryDescription)); |
|
98
|
|
|
|
|
99
|
|
|
$this->logger->expects($this->once()) |
|
100
|
|
|
->method('notice') |
|
101
|
|
|
->with($queryDescription); |
|
102
|
|
|
|
|
103
|
|
|
$query->expects($this->never()) |
|
104
|
|
|
->method('execute'); |
|
105
|
|
|
|
|
106
|
|
|
$this->executor->execute($query, true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testExecuteMigrationQueryDryRunArrayDescription() |
|
110
|
|
|
{ |
|
111
|
|
|
$queryDescription = ['test query 1', 'test query 2']; |
|
112
|
|
|
|
|
113
|
|
|
$query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery'); |
|
114
|
|
|
|
|
115
|
|
|
$query->expects($this->once()) |
|
116
|
|
|
->method('getDescription') |
|
117
|
|
|
->will($this->returnValue($queryDescription)); |
|
118
|
|
|
|
|
119
|
|
|
$this->logger->expects($this->at(0)) |
|
120
|
|
|
->method('notice') |
|
121
|
|
|
->with($queryDescription[0]); |
|
122
|
|
|
$this->logger->expects($this->at(1)) |
|
123
|
|
|
->method('notice') |
|
124
|
|
|
->with($queryDescription[1]); |
|
125
|
|
|
|
|
126
|
|
|
$query->expects($this->never()) |
|
127
|
|
|
->method('execute'); |
|
128
|
|
|
|
|
129
|
|
|
$this->executor->execute($query, true); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
public function testExecuteMigrationQueryDryRunEmptyDescription() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$queryDescription = null; |
|
135
|
|
|
|
|
136
|
|
|
$query = $this->getMock('RDV\Bundle\MigrationBundle\Migration\MigrationQuery'); |
|
137
|
|
|
|
|
138
|
|
|
$query->expects($this->once()) |
|
139
|
|
|
->method('getDescription') |
|
140
|
|
|
->will($this->returnValue($queryDescription)); |
|
141
|
|
|
|
|
142
|
|
|
$this->logger->expects($this->never()) |
|
143
|
|
|
->method('notice'); |
|
144
|
|
|
|
|
145
|
|
|
$query->expects($this->never()) |
|
146
|
|
|
->method('execute'); |
|
147
|
|
|
|
|
148
|
|
|
$this->executor->execute($query, true); |
|
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.