forked from cakephp/phinx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdoAdapterTest.php
More file actions
206 lines (175 loc) · 6.81 KB
/
PdoAdapterTest.php
File metadata and controls
206 lines (175 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
namespace Test\Phinx\Db\Adapter;
use PDO;
use PDOException;
use Phinx\Config\Config;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Test\Phinx\DeprecationException;
use Test\Phinx\TestUtils;
class PdoAdapterTest extends TestCase
{
/**
* @var \Phinx\Db\Adapter\PdoAdapter|\PHPUnit\Framework\MockObject\MockObject
*/
private $adapter;
protected function setUp(): void
{
$this->adapter = $this->getMockForAbstractClass('\Phinx\Db\Adapter\PdoAdapter', [['foo' => 'bar']]);
}
protected function tearDown(): void
{
unset($this->adapter);
}
public function testOptions()
{
$options = $this->adapter->getOptions();
$this->assertArrayHasKey('foo', $options);
$this->assertEquals('bar', $options['foo']);
}
public function testOptionsSetConnection()
{
$connection = $this->getMockBuilder(PDO::class)->disableOriginalConstructor()->getMock();
$this->adapter->setOptions(['connection' => $connection]);
$this->assertSame($connection, $this->adapter->getConnection());
}
public function testOptionsSetSchemaTableName()
{
$this->assertEquals('phinxlog', $this->adapter->getSchemaTableName());
$this->adapter->setOptions(['migration_table' => 'schema_table_test']);
$this->assertEquals('schema_table_test', $this->adapter->getSchemaTableName());
}
public function testOptionsSetDefaultMigrationTableThrowsDeprecation()
{
TestUtils::throwUserDeprecatedError();
$this->assertEquals('phinxlog', $this->adapter->getSchemaTableName());
$this->expectException(DeprecationException::class);
$this->expectExceptionMessage('The default_migration_table setting for adapter has been deprecated since 0.13.0. Use `migration_table` instead.');
$this->adapter->setOptions(['default_migration_table' => 'schema_table_test']);
$this->assertEquals('schema_table_test', $this->adapter->getSchemaTableName());
}
public function testSchemaTableName()
{
$this->assertEquals('phinxlog', $this->adapter->getSchemaTableName());
$this->adapter->setSchemaTableName('schema_table_test');
$this->assertEquals('schema_table_test', $this->adapter->getSchemaTableName());
}
/**
* @dataProvider getVersionLogDataProvider
*/
public function testGetVersionLog($versionOrder, $expectedOrderBy)
{
$adapter = $this->getMockForAbstractClass(
'\Phinx\Db\Adapter\PdoAdapter',
[['version_order' => $versionOrder]],
'',
true,
true,
true,
['fetchAll', 'getSchemaTableName', 'quoteTableName']
);
$schemaTableName = 'log';
$adapter->expects($this->once())
->method('getSchemaTableName')
->will($this->returnValue($schemaTableName));
$adapter->expects($this->once())
->method('quoteTableName')
->with($schemaTableName)
->will($this->returnValue("'$schemaTableName'"));
$mockRows = [
[
'version' => '20120508120534',
'key' => 'value',
],
[
'version' => '20130508120534',
'key' => 'value',
],
];
$adapter->expects($this->once())
->method('fetchAll')
->with("SELECT * FROM '$schemaTableName' ORDER BY $expectedOrderBy")
->will($this->returnValue($mockRows));
// we expect the mock rows but indexed by version creation time
$expected = [
'20120508120534' => [
'version' => '20120508120534',
'key' => 'value',
],
'20130508120534' => [
'version' => '20130508120534',
'key' => 'value',
],
];
$this->assertEquals($expected, $adapter->getVersionLog());
}
public function getVersionLogDataProvider()
{
return [
'With Creation Time Version Order' => [
Config::VERSION_ORDER_CREATION_TIME, 'version ASC',
],
'With Execution Time Version Order' => [
Config::VERSION_ORDER_EXECUTION_TIME, 'start_time ASC, version ASC',
],
];
}
public function testGetVersionLogInvalidVersionOrderKO()
{
$this->expectExceptionMessage('Invalid version_order configuration option');
$adapter = $this->getMockForAbstractClass(
'\Phinx\Db\Adapter\PdoAdapter',
[['version_order' => 'invalid']]
);
$this->expectException(RuntimeException::class);
$adapter->getVersionLog();
}
public function testGetVersionLongDryRun()
{
$adapter = $this->getMockForAbstractClass(
'\Phinx\Db\Adapter\PdoAdapter',
[['version_order' => Config::VERSION_ORDER_CREATION_TIME]],
'',
true,
true,
true,
['isDryRunEnabled', 'fetchAll', 'getSchemaTableName', 'quoteTableName']
);
$schemaTableName = 'log';
$adapter->expects($this->once())
->method('isDryRunEnabled')
->will($this->returnValue(true));
$adapter->expects($this->once())
->method('getSchemaTableName')
->will($this->returnValue($schemaTableName));
$adapter->expects($this->once())
->method('quoteTableName')
->with($schemaTableName)
->will($this->returnValue("'$schemaTableName'"));
$adapter->expects($this->once())
->method('fetchAll')
->with("SELECT * FROM '$schemaTableName' ORDER BY version ASC")
->will($this->throwException(new PDOException()));
$this->assertEquals([], $adapter->getVersionLog());
}
/**
* Tests that execute() can be called on the adapter, and that the SQL is passed through to the PDO.
*/
public function testExecuteCanBeCalled()
{
/** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */
$pdo = $this->getMockBuilder(PDO::class)->disableOriginalConstructor()->onlyMethods(['exec'])->getMock();
$pdo->expects($this->once())->method('exec')->with('SELECT 1')->will($this->returnValue(1));
$this->adapter->setConnection($pdo);
$this->adapter->execute('SELECT 1');
}
public function testExecuteRightTrimsSemiColons()
{
/** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */
$pdo = $this->getMockBuilder(PDO::class)->disableOriginalConstructor()->onlyMethods(['exec'])->getMock();
$pdo->expects($this->once())->method('exec')->with('SELECT 1')->will($this->returnValue(1));
$this->adapter->setConnection($pdo);
$this->adapter->execute('SELECT 1;;');
}
}