Skip to content

Commit 6f0ec12

Browse files
authored
Add SQLiteAdapter::isMemory helper function (#2336)
1 parent fa2b585 commit 6f0ec12

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Phinx/Config/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function getEnvironment(string $name): ?array
179179
if (
180180
isset($environments[$name]['adapter'])
181181
&& $environments[$name]['adapter'] === 'sqlite'
182-
&& !empty($environments[$name]['memory'])
182+
&& SQLiteAdapter::isMemory($environments[$name])
183183
) {
184184
$environments[$name]['name'] = SQLiteAdapter::MEMORY;
185185
}

src/Phinx/Db/Adapter/SQLiteAdapter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ public function databaseVersionAtLeast(string $ver): bool
138138
return version_compare($actual, $ver, '>=');
139139
}
140140

141+
/**
142+
* Check if the given options represent a memory database
143+
*
144+
* @param array $options Options to check
145+
* @return bool
146+
*/
147+
public static function isMemory(array $options): bool
148+
{
149+
return !empty($options['memory']) || ($options['name'] ?? '') === static::MEMORY;
150+
}
151+
141152
/**
142153
* {@inheritDoc}
143154
*
@@ -171,7 +182,7 @@ public function connect(): void
171182
$dsn = 'sqlite:file:' . ($options['name'] ?? '') . '?' . implode('&', $params);
172183
} else {
173184
// use a memory database if the option was specified
174-
if (!empty($options['memory']) || $options['name'] === static::MEMORY) {
185+
if (SQLiteAdapter::isMemory($options)) {
175186
$dsn = 'sqlite:' . static::MEMORY;
176187
} else {
177188
$dsn = 'sqlite:' . $options['name'] . $this->suffix;
@@ -204,7 +215,7 @@ public function connect(): void
204215
*/
205216
public static function getSuffix(array $options): string
206217
{
207-
if (($options['name'] ?? '') === self::MEMORY) {
218+
if (SQLiteAdapter::isMemory($options)) {
208219
return '';
209220
}
210221

tests/Phinx/Db/Adapter/SQLiteAdapterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3481,4 +3481,23 @@ public function testPdoNotPersistentConnection()
34813481
$adapter = new SQLiteAdapter(SQLITE_DB_CONFIG);
34823482
$this->assertFalse($adapter->getConnection()->getAttribute(PDO::ATTR_PERSISTENT));
34833483
}
3484+
3485+
public function isMemoryProvider(): array
3486+
{
3487+
return [
3488+
[['name' => ':memory:'], true],
3489+
[['memory' => true], true],
3490+
[['name' => 'foo', 'memory' => true], true],
3491+
[['name' => 'bar'], false],
3492+
[['memory' => false], false],
3493+
];
3494+
}
3495+
3496+
/**
3497+
* @dataProvider isMemoryProvider
3498+
*/
3499+
public function testIsMemory(array $config, bool $expected): void
3500+
{
3501+
$this->assertSame($expected, SQLiteAdapter::isMemory($config));
3502+
}
34843503
}

0 commit comments

Comments
 (0)