forked from contao/test-case
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContaoDatabaseTrait.php
59 lines (49 loc) · 1.41 KB
/
ContaoDatabaseTrait.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
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\TestCase;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
trait ContaoDatabaseTrait
{
/**
* @var Connection
*/
private static $connection;
protected static function loadFileIntoDatabase(string $sqlFile): void
{
if (!file_exists($sqlFile)) {
throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $sqlFile));
}
$conn = static::getConnection();
$conn->exec(file_get_contents($sqlFile));
}
protected static function getConnection(): Connection
{
if (null === self::$connection) {
if (false !== getenv('DATABASE_URL')) {
$params = [
'driver' => 'pdo_mysql',
'url' => getenv('DATABASE_URL'),
];
} else {
$params = [
'driver' => 'pdo_mysql',
'host' => getenv('DB_HOST'),
'port' => getenv('DB_PORT'),
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'dbname' => getenv('DB_NAME'),
];
}
self::$connection = DriverManager::getConnection($params);
}
return self::$connection;
}
}