1 | <?php |
||
18 | final class DiTypeTest extends TestCase |
||
19 | { |
||
20 | use ContainerTestTrait; |
||
21 | |||
22 | public function testDiTypeIsRegistered() |
||
23 | { |
||
24 | $container = $this->getContainer(); |
||
25 | |||
26 | $container->register('test_type', TestDiType::class) |
||
27 | ->addMethodCall('setContainer', [new Reference('service_container')]) |
||
28 | ->addTag('doctrine_di_type', ['type' => 'test']); |
||
29 | |||
30 | $this->compile($container); |
||
31 | |||
32 | $container->get('doctrine'); |
||
33 | |||
34 | $types = Type::getTypesMap(); |
||
35 | |||
36 | self::assertArrayHasKey('test', $types); |
||
37 | |||
38 | /** @var TestDiType $type */ |
||
39 | $type = Type::getType('test'); |
||
40 | |||
41 | self::assertInstanceOf(TestDiType::class, $type); |
||
42 | self::assertSame($container, $type->getContainer()); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @expectedException \LogicException |
||
47 | */ |
||
48 | public function testDiRequiresTypeTagAttribute() |
||
49 | { |
||
50 | $container = $this->getContainer(); |
||
51 | |||
52 | $container->register('test_type', TestDiType::class) |
||
53 | ->addMethodCall('setContainer', [new Reference('service_container')]) |
||
54 | ->addTag('doctrine_di_type'); |
||
55 | |||
56 | $this->compile($container); |
||
57 | } |
||
58 | |||
59 | public function testTypesAreNotRegisteredUntilRequired() |
||
60 | { |
||
61 | $container = $this->buildContainer( |
||
62 | [ |
||
63 | new DoctrineBundle(), |
||
64 | new BankiruDoctrineDiTypeBundle(), |
||
65 | ], |
||
66 | [ |
||
67 | 'doctrine_di_types' => [ |
||
68 | 'init_on_boot' => false, |
||
69 | ], |
||
70 | 'doctrine' => [ |
||
71 | 'dbal' => [ |
||
72 | 'driver' => 'pdo_sqlite', |
||
73 | 'memory' => true, |
||
74 | ], |
||
75 | ], |
||
76 | |||
77 | ], |
||
78 | false |
||
79 | ); |
||
80 | |||
81 | $container->register('test_type', TestDiType::class) |
||
82 | ->addMethodCall('setContainer', [new Reference('service_container')]) |
||
83 | ->addTag('doctrine_di_type', ['type' => 'test']); |
||
84 | |||
85 | $this->compile($container); |
||
86 | |||
87 | self::assertArrayNotHasKey('test', Type::getTypesMap()); |
||
88 | |||
89 | $container->get('doctrine'); |
||
90 | |||
91 | self::assertArrayHasKey('test', Type::getTypesMap()); |
||
92 | } |
||
93 | |||
94 | public function testExtensionShouldBeEnabled() |
||
95 | { |
||
96 | $container = $this->buildContainer( |
||
97 | [ |
||
98 | new DoctrineBundle(), |
||
99 | new BankiruDoctrineDiTypeBundle(), |
||
100 | ], |
||
101 | [ |
||
102 | 'doctrine' => [ |
||
103 | 'dbal' => [ |
||
104 | 'driver' => 'pdo_sqlite', |
||
105 | 'memory' => true, |
||
106 | ], |
||
107 | ], |
||
108 | |||
109 | ], |
||
110 | false |
||
111 | ); |
||
112 | |||
113 | $this->compile($container); |
||
114 | |||
115 | self::assertFalse($container->has('bankiru_doctrine_di_types.type_factory')); |
||
116 | } |
||
117 | |||
118 | public function testBundlePreservesPreviousConfigurator() |
||
119 | { |
||
120 | $container = $this->getContainer(); |
||
121 | |||
122 | $container->getDefinition('doctrine') |
||
123 | ->setConfigurator([Configurator::class, 'configure']); |
||
124 | |||
125 | $this->compile($container); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return ContainerBuilder |
||
130 | */ |
||
131 | protected function getContainer() |
||
132 | { |
||
133 | $container = $this->buildContainer( |
||
134 | [ |
||
135 | new DoctrineBundle(), |
||
136 | new BankiruDoctrineDiTypeBundle(), |
||
137 | ], |
||
138 | [ |
||
139 | 'doctrine_di_types' => null, |
||
140 | 'doctrine' => [ |
||
141 | 'dbal' => [ |
||
142 | 'driver' => 'pdo_sqlite', |
||
143 | 'memory' => true, |
||
144 | ], |
||
145 | ], |
||
146 | |||
147 | ], |
||
148 | false |
||
149 | ); |
||
150 | |||
151 | return $container; |
||
152 | } |
||
153 | } |
||
154 | |||
187 |