|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hgraca\DoctrineTestDbRegenerationBundle\Symfony; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\DataFixtures\Loader; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Hgraca\DoctrineTestDbRegenerationBundle\DependencyInjection\Configuration; |
|
10
|
|
|
use Hgraca\DoctrineTestDbRegenerationBundle\DependencyInjection\CreateDbRegenerationServiceLocatorCompilerPass; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ServiceLocator; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class TestContainer |
|
17
|
|
|
{ |
|
18
|
|
|
public const KEY_FIXTURES_LOADER = Configuration::ROOT . '.' . Configuration::FIXTURES_LOADER_SERVICE_ID; |
|
19
|
|
|
public const KEY_DOCTRINE_SERVICE_ID = Configuration::ROOT . '.' . Configuration::DOCTRINE_SERVICE_ID; |
|
20
|
|
|
public const KEY_TEST_DB_BKP_PATH = Configuration::ROOT . '.' . Configuration::TEST_DB_BKP_PATH; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ContainerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $container; |
|
26
|
|
|
|
|
27
|
24 |
|
public function __construct(ContainerInterface $container = null) |
|
28
|
|
|
{ |
|
29
|
24 |
|
$this->container = $container ?? $this->createContainer(); |
|
30
|
24 |
|
} |
|
31
|
|
|
|
|
32
|
16 |
|
public function getFixturesLoader(): Loader |
|
33
|
|
|
{ |
|
34
|
16 |
|
return $this->getService( |
|
35
|
16 |
|
$this->getParameter(self::KEY_FIXTURES_LOADER) |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
16 |
|
public function getEntityManager(): EntityManagerInterface |
|
40
|
|
|
{ |
|
41
|
16 |
|
return $this->getService($this->getParameter(self::KEY_DOCTRINE_SERVICE_ID)) |
|
42
|
16 |
|
->getManager(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
16 |
|
public function getDbBkpDir(): string |
|
46
|
|
|
{ |
|
47
|
16 |
|
return $this->getParameter(self::KEY_TEST_DB_BKP_PATH); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
22 |
|
public function getParameter(string $parameter) |
|
54
|
|
|
{ |
|
55
|
22 |
|
return $this->getContainer()->getParameter($parameter); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
20 |
|
public function getService(string $service) |
|
62
|
|
|
{ |
|
63
|
20 |
|
return $this->getServiceLocator()->get($service); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
24 |
|
public function getContainer(): ContainerInterface |
|
67
|
|
|
{ |
|
68
|
24 |
|
return $this->container; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
20 |
|
private function getServiceLocator(): ServiceLocator |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var ServiceLocator $serviceLocator */ |
|
74
|
20 |
|
$serviceLocator = $this->getContainer() |
|
75
|
20 |
|
->get(CreateDbRegenerationServiceLocatorCompilerPass::TEST_DB_REGENERATION_SERVICE_LOCATOR); |
|
76
|
|
|
|
|
77
|
20 |
|
return $serviceLocator; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function createContainer(): ContainerInterface |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->createTestContainer()->getContainer(); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function createTestContainer(): KernelInterface |
|
86
|
|
|
{ |
|
87
|
|
|
$kernelTestCase = new class() extends KernelTestCase { |
|
88
|
|
|
public function getKernel(): KernelInterface |
|
89
|
|
|
{ |
|
90
|
|
|
$kernel = $this->bootKernel(); |
|
91
|
|
|
|
|
92
|
|
|
self::$kernel = null; |
|
93
|
|
|
|
|
94
|
|
|
return $kernel; |
|
95
|
|
|
} |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
return $kernelTestCase->getKernel(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|