-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
SqLiteFactory.php
64 lines (54 loc) · 2.1 KB
/
SqLiteFactory.php
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
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Migration\Tests\Support\Factory;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Sqlite\Connection as SqLiteConnection;
use Yiisoft\Db\Sqlite\Driver as SqLiteDriver;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Test\Support\SimpleCache\MemorySimpleCache;
use Yiisoft\Db\Migration\Tests\Support\Helper\ContainerConfig;
use Yiisoft\Db\Migration\Tests\Support\Helper\ContainerHelper;
use function dirname;
final class SqLiteFactory
{
public static function createContainer(?ContainerConfig $config = null): ContainerInterface
{
$config ??= new ContainerConfig();
$container = new SimpleContainer(
[
LoggerInterface::class => new NullLogger(),
SchemaCache::class => new SchemaCache(new MemorySimpleCache()),
Aliases::class => new Aliases(
[
'@runtime' => dirname(__DIR__, 2) . '/runtime',
],
),
],
static function (string $id) use (&$container, $config): object {
return match ($id) {
ConnectionInterface::class => new SqLiteConnection(
new SqLiteDriver('sqlite::memory:'),
new SchemaCache(new MemorySimpleCache())
),
SqLiteConnection::class => $container->get(ConnectionInterface::class),
default => ContainerHelper::get($container, $id, $config),
};
}
);
return $container;
}
public static function clearDatabase(ContainerInterface $container): void
{
$db = $container->get(SqLiteConnection::class);
$command = $db->createCommand();
foreach ($db->getSchema()->getTableNames() as $tableName) {
$command->dropTable($tableName)->execute();
}
$db->close();
}
}