1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Persistence\Tests; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\EventManager; |
12
|
|
|
use Doctrine\DBAL\Connection; |
13
|
|
|
use Doctrine\DBAL\DriverManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Database connection factory for integration tests. |
17
|
|
|
*/ |
18
|
|
|
class DatabaseConnectionFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Associative array of <code>[driver => AbstractPlatform]</code>. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $databasePlatforms = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Doctrine\Common\EventManager |
29
|
|
|
*/ |
30
|
|
|
private $eventManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \EzSystems\DoctrineSchema\Database\DbPlatform\DbPlatform[] $databasePlatforms |
34
|
|
|
* @param \Doctrine\Common\EventManager $eventManager |
35
|
|
|
*/ |
36
|
|
|
public function __construct(iterable $databasePlatforms, EventManager $eventManager) |
37
|
|
|
{ |
38
|
|
|
$this->databasePlatforms = []; |
39
|
|
|
foreach ($databasePlatforms as $databasePlatform) { |
40
|
|
|
$this->databasePlatforms[$databasePlatform->getDriverName()] = $databasePlatform; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->eventManager = $eventManager; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Connect to a database described by URL (a.k.a. DSN). |
48
|
|
|
* |
49
|
|
|
* @param string $databaseURL |
50
|
|
|
* |
51
|
|
|
* @return \Doctrine\DBAL\Connection |
52
|
|
|
* |
53
|
|
|
* @throws \Doctrine\DBAL\DBALException if connection failed |
54
|
|
|
*/ |
55
|
|
|
public function createConnection(string $databaseURL): Connection |
56
|
|
|
{ |
57
|
|
|
$params = ['url' => $databaseURL]; |
58
|
|
|
|
59
|
|
|
// set DbPlatform based on database url scheme |
60
|
|
|
$scheme = parse_url($databaseURL, PHP_URL_SCHEME); |
61
|
|
|
$driverName = 'pdo_' . $scheme; |
62
|
|
|
if (isset($this->databasePlatforms[$driverName])) { |
63
|
|
|
$params['platform'] = $this->databasePlatforms[$driverName]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return DriverManager::getConnection($params, null, $this->eventManager); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|