|
1 | | -<?php |
2 | | - |
3 | | -declare(strict_types=1); |
4 | | - |
5 | | -namespace PhpDbIntegrationTest\Adapter\Sqlite\Container\TestAsset; |
6 | | - |
7 | | -use Laminas\ServiceManager\ServiceManager; |
8 | | -use Laminas\Stdlib\ArrayUtils; |
9 | | -use Override; |
10 | | -use PhpDb\Adapter\AdapterInterface; |
11 | | -use PhpDb\Adapter\Driver\DriverInterface; |
12 | | -use PhpDb\Adapter\Sqlite\ConfigProvider; |
13 | | -use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; |
14 | | -use PhpDb\ConfigProvider as LaminasDbConfigProvider; |
15 | | -use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
16 | | -use Psr\Container\ContainerInterface; |
17 | | - |
18 | | -/** |
19 | | - * This trait provides a setup method for integration tests that require |
20 | | - * a database adapter configuration. |
21 | | - * |
22 | | - * It initializes the service manager with a database configuration, |
23 | | - * allowing for the creation of an adapter manager and the retrieval |
24 | | - * of an adapter instance. |
25 | | - */ |
26 | | -#[RequiresPhpExtension('pdo_sqlite')] |
27 | | -trait SetupTrait |
28 | | -{ |
29 | | - protected array $config = ['db' => []]; |
30 | | - |
31 | | - protected ?AdapterInterface $adapter; |
32 | | - |
33 | | - protected ContainerInterface $container; |
34 | | - |
35 | | - protected DriverInterface|string|null $driver; |
36 | | - |
37 | | - #[Override] |
38 | | - protected function setUp(): void |
39 | | - { |
40 | | - $this->getAdapter(); |
41 | | - parent::setUp(); |
42 | | - } |
43 | | - |
44 | | - protected function getAdapter(array $config = []): AdapterInterface |
45 | | - { |
46 | | - $connectionConfig = [ |
47 | | - 'db' => [ |
48 | | - 'driver' => $this->driver ?? Pdo::class, |
49 | | - 'connection' => [ |
50 | | - 'dsn' => 'sqlite::memory:', |
51 | | - 'charset' => 'utf8', |
52 | | - 'driver_options' => [], |
53 | | - ], |
54 | | - 'options' => [ |
55 | | - //'buffer_results' => false, |
56 | | - ], |
57 | | - ], |
58 | | - ]; |
59 | | - |
60 | | - // merge service config from both PhpDb and PhpDb\Adapter\Mysql |
61 | | - $serviceManagerConfig = ArrayUtils::merge( |
62 | | - (new LaminasDbConfigProvider())()['dependencies'], |
63 | | - (new ConfigProvider())()['dependencies'] |
64 | | - ); |
65 | | - |
66 | | - $serviceManagerConfig = ArrayUtils::merge( |
67 | | - $serviceManagerConfig, |
68 | | - $connectionConfig |
69 | | - ); |
70 | | - |
71 | | - // prefer passed config over environment variables |
72 | | - if ($config !== []) { |
73 | | - $serviceManagerConfig = ArrayUtils::merge($serviceManagerConfig, $config); |
74 | | - } |
75 | | - |
76 | | - $serviceManagerConfig = ArrayUtils::merge( |
77 | | - $serviceManagerConfig, |
78 | | - [ |
79 | | - 'services' => [ |
80 | | - 'config' => $serviceManagerConfig, |
81 | | - ], |
82 | | - ] |
83 | | - ); |
84 | | - |
85 | | - $this->config = $serviceManagerConfig; |
86 | | - $this->container = new ServiceManager($this->config); |
87 | | - $this->adapter = $this->container->get(AdapterInterface::class); |
88 | | - |
89 | | - return $this->adapter; |
90 | | - } |
91 | | - |
92 | | - protected function getConfig(): array |
93 | | - { |
94 | | - return $this->config; |
95 | | - } |
96 | | - |
97 | | - protected function getHostname(): string |
98 | | - { |
99 | | - return $this->getConfig()['db']['connection']['hostname']; |
100 | | - } |
101 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpDbIntegrationTest\Adapter\Sqlite\Container\TestAsset; |
| 6 | + |
| 7 | +use Laminas\ServiceManager\ServiceManager; |
| 8 | +use Laminas\Stdlib\ArrayUtils; |
| 9 | +use Override; |
| 10 | +use PhpDb\Adapter\AdapterInterface; |
| 11 | +use PhpDb\Adapter\Driver\DriverInterface; |
| 12 | +use PhpDb\Adapter\Sqlite\ConfigProvider; |
| 13 | +use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; |
| 14 | +use PhpDb\ConfigProvider as LaminasDbConfigProvider; |
| 15 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
| 16 | +use Psr\Container\ContainerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * This trait provides a setup method for integration tests that require |
| 20 | + * a database adapter configuration. |
| 21 | + * |
| 22 | + * It initializes the service manager with a database configuration, |
| 23 | + * allowing for the creation of an adapter manager and the retrieval |
| 24 | + * of an adapter instance. |
| 25 | + */ |
| 26 | +#[RequiresPhpExtension('pdo_sqlite')] |
| 27 | +trait SetupTrait |
| 28 | +{ |
| 29 | + protected array $config = ['db' => []]; |
| 30 | + |
| 31 | + protected ?AdapterInterface $adapter; |
| 32 | + |
| 33 | + protected ContainerInterface $container; |
| 34 | + |
| 35 | + protected DriverInterface|string|null $driver; |
| 36 | + |
| 37 | + #[Override] |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + $this->getAdapter(); |
| 41 | + parent::setUp(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function getAdapter(array $config = []): AdapterInterface |
| 45 | + { |
| 46 | + $connectionConfig = [ |
| 47 | + 'db' => [ |
| 48 | + 'driver' => $this->driver ?? Pdo::class, |
| 49 | + 'connection' => [ |
| 50 | + 'dsn' => 'sqlite::memory:', |
| 51 | + 'charset' => 'utf8', |
| 52 | + 'driver_options' => [], |
| 53 | + ], |
| 54 | + 'options' => [ |
| 55 | + //'buffer_results' => false, |
| 56 | + ], |
| 57 | + ], |
| 58 | + ]; |
| 59 | + |
| 60 | + // merge service config from both PhpDb and PhpDb\Adapter\Mysql |
| 61 | + $serviceManagerConfig = ArrayUtils::merge( |
| 62 | + (new LaminasDbConfigProvider())()['dependencies'], |
| 63 | + (new ConfigProvider())()['dependencies'] |
| 64 | + ); |
| 65 | + |
| 66 | + $serviceManagerConfig = ArrayUtils::merge( |
| 67 | + $serviceManagerConfig, |
| 68 | + $connectionConfig |
| 69 | + ); |
| 70 | + |
| 71 | + // prefer passed config over environment variables |
| 72 | + if ($config !== []) { |
| 73 | + $serviceManagerConfig = ArrayUtils::merge($serviceManagerConfig, $config); |
| 74 | + } |
| 75 | + |
| 76 | + $serviceManagerConfig = ArrayUtils::merge( |
| 77 | + $serviceManagerConfig, |
| 78 | + [ |
| 79 | + 'services' => [ |
| 80 | + 'config' => $serviceManagerConfig, |
| 81 | + ], |
| 82 | + ] |
| 83 | + ); |
| 84 | + |
| 85 | + $this->config = $serviceManagerConfig; |
| 86 | + $this->container = new ServiceManager($this->config); |
| 87 | + $this->adapter = $this->container->get(AdapterInterface::class); |
| 88 | + |
| 89 | + return $this->adapter; |
| 90 | + } |
| 91 | + |
| 92 | + protected function getConfig(): array |
| 93 | + { |
| 94 | + return $this->config; |
| 95 | + } |
| 96 | + |
| 97 | + protected function getHostname(): string |
| 98 | + { |
| 99 | + return $this->getConfig()['db']['connection']['hostname']; |
| 100 | + } |
| 101 | +} |
0 commit comments