1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\AddressChangeContext\Tests; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\DriverManager; |
9
|
|
|
use Doctrine\DBAL\Exception; |
10
|
|
|
use Doctrine\ORM\Configuration; |
11
|
|
|
use Doctrine\ORM\EntityManager; |
12
|
|
|
use Doctrine\ORM\ORMSetup; |
13
|
|
|
use WMDE\Fundraising\AddressChangeContext\AddressChangeContextFactory; |
14
|
|
|
|
15
|
|
|
class TestAddressChangeContextFactory { |
16
|
|
|
|
17
|
|
|
private Configuration $doctrineConfig; |
18
|
|
|
private ?EntityManager $entityManager; |
19
|
|
|
private AddressChangeContextFactory $contextFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array<string,mixed> $config |
23
|
|
|
* |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function __construct( private array $config ) { |
27
|
|
|
$this->contextFactory = new AddressChangeContextFactory(); |
28
|
|
|
$this->doctrineConfig = ORMSetup::createXMLMetadataConfiguration( |
29
|
|
|
$this->contextFactory->getDoctrineMappingPaths(), |
30
|
|
|
true |
31
|
|
|
); |
32
|
|
|
$this->entityManager = null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function newConnection(): Connection { |
36
|
|
|
$connection = DriverManager::getConnection( $this->config['db'] ); |
37
|
|
|
$this->contextFactory->registerCustomTypes( $connection ); |
38
|
|
|
return $connection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function newEntityManager(): EntityManager { |
42
|
|
|
return new EntityManager( $this->newConnection(), $this->doctrineConfig ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getEntityManager(): EntityManager { |
46
|
|
|
if ( $this->entityManager === null ) { |
47
|
|
|
$this->entityManager = $this->newEntityManager(); |
48
|
|
|
} |
49
|
|
|
return $this->entityManager; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|