1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace ShlinkioTest\Shlink\Common\Doctrine; |
||
6 | |||
7 | use Doctrine\Common\Persistence\Mapping\Driver\PHPDriver; |
||
8 | use Doctrine\DBAL\Driver\PDOSqlite; |
||
9 | use Doctrine\DBAL\Types\Type; |
||
10 | use Laminas\ServiceManager\ServiceManager; |
||
11 | use PHPUnit\Framework\TestCase; |
||
12 | use ReflectionObject; |
||
13 | use Shlinkio\Shlink\Common\Doctrine\EntityManagerFactory; |
||
14 | use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; |
||
15 | |||
16 | use function array_filter; |
||
17 | use function array_merge; |
||
18 | |||
19 | use const ARRAY_FILTER_USE_KEY; |
||
20 | |||
21 | class EntityManagerFactoryTest extends TestCase |
||
22 | { |
||
23 | private EntityManagerFactory $factory; |
||
24 | |||
25 | public function setUp(): void |
||
26 | { |
||
27 | if (Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)) { |
||
28 | $typeRegistry = Type::getTypeRegistry(); |
||
29 | $ref = new ReflectionObject($typeRegistry); |
||
30 | $instancesProp = $ref->getProperty('instances'); |
||
31 | $instancesProp->setAccessible(true); |
||
32 | $withoutChronosType = array_filter( |
||
33 | $typeRegistry->getMap(), |
||
34 | fn (string $key): bool => $key !== ChronosDateTimeType::CHRONOS_DATETIME, |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
35 | ARRAY_FILTER_USE_KEY, |
||
36 | ); |
||
37 | $instancesProp->setValue($typeRegistry, $withoutChronosType); |
||
38 | } |
||
39 | |||
40 | $this->factory = new EntityManagerFactory(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | * @dataProvider provideConfig |
||
46 | */ |
||
47 | public function serviceIsCreated(array $config, int $expectedAutoGenerateProxies): void |
||
48 | { |
||
49 | $sm = new ServiceManager(['services' => [ |
||
50 | 'config' => $config, |
||
51 | ]]); |
||
52 | |||
53 | $this->assertFalse(Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)); |
||
54 | $em = ($this->factory)($sm); |
||
55 | |||
56 | $this->assertTrue(Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)); |
||
57 | $this->assertEquals($expectedAutoGenerateProxies, $em->getConfiguration()->getAutoGenerateProxyClasses()); |
||
58 | $this->assertInstanceOf(PDOSqlite\Driver::class, $em->getConnection()->getDriver()); |
||
59 | $this->assertEquals(__DIR__, $em->getConfiguration()->getProxyDir()); |
||
60 | |||
61 | /** @var PHPDriver $metaDriver */ |
||
62 | $metaDriver = $em->getConfiguration()->getMetadataDriverImpl(); |
||
63 | $this->assertEquals([__FILE__], $metaDriver->getLocator()->getPaths()); |
||
64 | } |
||
65 | |||
66 | public function provideConfig(): iterable |
||
67 | { |
||
68 | $baseConfig = [ |
||
69 | 'entity_manager' => [ |
||
70 | 'orm' => [ |
||
71 | 'types' => [ |
||
72 | ChronosDateTimeType::CHRONOS_DATETIME => ChronosDateTimeType::class, |
||
73 | ], |
||
74 | 'proxies_dir' => __DIR__, |
||
75 | 'entities_mappings' => [__FILE__], |
||
76 | ], |
||
77 | 'connection' => [ |
||
78 | 'driver' => 'pdo_sqlite', |
||
79 | ], |
||
80 | ], |
||
81 | ]; |
||
82 | |||
83 | yield [array_merge($baseConfig, ['debug' => true]), 1]; |
||
84 | yield [array_merge($baseConfig, ['debug' => '1']), 1]; |
||
85 | yield [array_merge($baseConfig, ['debug' => 'true']), 1]; |
||
86 | yield [array_merge($baseConfig, ['debug' => false]), 0]; |
||
87 | yield [array_merge($baseConfig, ['debug' => null]), 0]; |
||
88 | } |
||
89 | } |
||
90 |