forked from php-db/phpdb-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupTrait.php
More file actions
105 lines (87 loc) · 3.02 KB
/
SetupTrait.php
File metadata and controls
105 lines (87 loc) · 3.02 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
<?php
declare(strict_types=1);
namespace PhpDbIntegrationTest\Adapter\Sqlite\Container\TestAsset;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ArrayUtils;
use Override;
use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Sqlite\ConfigProvider;
use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo;
use PhpDb\Container\AdapterManager;
use PhpDb\Container\ConfigProvider as LaminasDbConfigProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Psr\Container\ContainerInterface;
/**
* This trait provides a setup method for integration tests that require
* a database adapter configuration.
*
* It initializes the service manager with a database configuration,
* allowing for the creation of an adapter manager and the retrieval
* of an adapter instance.
*/
#[RequiresPhpExtension('pdo_sqlite')]
trait SetupTrait
{
protected array $config = ['db' => []];
protected ?AdapterInterface $adapter;
protected AdapterManager $adapterManager;
protected ContainerInterface $container;
protected DriverInterface|string|null $driver;
#[Override]
protected function setUp(): void
{
$this->getAdapter();
parent::setUp();
}
protected function getAdapter(array $config = []): AdapterInterface
{
$connectionConfig = [
'db' => [
'driver' => $this->driver ?? Pdo::class,
'connection' => [
'dsn' => 'sqlite::memory:',
'charset' => 'utf8',
'driver_options' => [],
],
'options' => [
//'buffer_results' => false,
],
],
];
// merge service config from both PhpDb and PhpDb\Adapter\Mysql
$serviceManagerConfig = ArrayUtils::merge(
(new LaminasDbConfigProvider())()['dependencies'],
(new ConfigProvider())()['dependencies']
);
$serviceManagerConfig = ArrayUtils::merge(
$serviceManagerConfig,
$connectionConfig
);
// prefer passed config over environment variables
if ($config !== []) {
$serviceManagerConfig = ArrayUtils::merge($serviceManagerConfig, $config);
}
$serviceManagerConfig = ArrayUtils::merge(
$serviceManagerConfig,
[
'services' => [
'config' => $serviceManagerConfig,
],
]
);
$this->config = $serviceManagerConfig;
$this->container = new ServiceManager($this->config);
$this->adapterManager = $this->container->get(AdapterManager::class);
$this->adapter = $this->adapterManager->get(AdapterInterface::class);
return $this->adapter;
}
protected function getConfig(): array
{
return $this->config;
}
protected function getHostname(): string
{
return $this->getConfig()['db']['connection']['hostname'];
}
}