/
TestTrait.php
52 lines (41 loc) · 1.12 KB
/
TestTrait.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
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Sqlite\Tests\Support;
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Driver;
use Yiisoft\Db\Sqlite\Dsn;
use Yiisoft\Db\Tests\Support\DbHelper;
trait TestTrait
{
private string $dsn = '';
/**
* @throws Exception
* @throws InvalidConfigException
*/
protected function getConnection(bool $fixture = false): ConnectionPDOInterface
{
$db = new Connection(new Driver($this->getDsn()), DbHelper::getSchemaCache());
if ($fixture) {
DbHelper::loadFixture($db, __DIR__ . '/Fixture/sqlite.sql');
}
return $db;
}
protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn('sqlite', 'memory'))->asString();
}
return $this->dsn;
}
protected function getDriverName(): string
{
return 'sqlite';
}
protected function setDsn(string $dsn): void
{
$this->dsn = $dsn;
}
}