1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\SerializerBundle\Tests\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Bdf\Serializer\SerializerInterface; |
6
|
|
|
use Bdf\SerializerBundle\DependencyInjection\BdfSerializerExtension; |
7
|
|
|
use Bdf\SerializerBundle\DependencyInjection\Compiler\SerializerLoaderPass; |
8
|
|
|
use Bdf\SerializerBundle\DependencyInjection\Compiler\SerializerNormalizerPass; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
11
|
|
|
use Symfony\Component\Config\FileLocator; |
12
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
16
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* BdfSerializerExtensionTests |
20
|
|
|
*/ |
21
|
|
|
class BdfSerializerExtensionTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
public function test_default_config() |
27
|
|
|
{ |
28
|
|
|
$container = $this->createContainerFromFile('default'); |
29
|
|
|
|
30
|
|
|
$this->assertTrue($container->getDefinition(SerializerInterface::class)->isPublic()); |
31
|
|
|
$this->assertTrue($container->getAlias('bdf_serializer')->isPublic()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
public function test_normalizers() |
38
|
|
|
{ |
39
|
|
|
$container = $this->createContainerFromFile('default', [], false); |
40
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass(), new SerializerNormalizerPass()]); |
41
|
|
|
$container->compile(); |
42
|
|
|
|
43
|
|
|
$definition = $container->getDefinition('bdf_serializer.normalizer.loader'); |
44
|
|
|
|
45
|
|
|
$normalizers = $definition->getArgument(0); |
46
|
|
|
$this->assertCount(3, $normalizers); |
47
|
|
|
$this->assertSame('bdf_serializer.normalizer.datetime', (string)$normalizers[0]); |
48
|
|
|
$this->assertSame('bdf_serializer.normalizer.traversable', (string)$normalizers[1]); |
49
|
|
|
$this->assertSame('bdf_serializer.normalizer.property', (string)$normalizers[2]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
|
|
public function test_loaders() |
56
|
|
|
{ |
57
|
|
|
$container = $this->createContainerFromFile('default', [], false); |
58
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass(), new SerializerLoaderPass()]); |
59
|
|
|
$container->compile(); |
60
|
|
|
|
61
|
|
|
$definition = $container->getDefinition('bdf_serializer.metadata_factory'); |
62
|
|
|
|
63
|
|
|
$loaders = $definition->getArgument(0); |
64
|
|
|
$this->assertCount(2, $loaders); |
65
|
|
|
$this->assertSame('bdf_serializer.metadata.loader.static_method', (string)$loaders[0]); |
66
|
|
|
$this->assertSame('bdf_serializer.metadata.loader.annotation', (string)$loaders[1]); |
67
|
|
|
$this->assertSame(null, $definition->getArgument(1)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
|
public function test_service_cache() |
74
|
|
|
{ |
75
|
|
|
$container = $this->createContainerFromFile('with_cache', [], false); |
76
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass()]); |
77
|
|
|
$container->setDefinition('TestCache', (new Definition('TestCache'))->setPublic(true)); |
78
|
|
|
$container->compile(); |
79
|
|
|
|
80
|
|
|
$definition = $container->getDefinition('bdf_serializer.metadata_factory'); |
81
|
|
|
$this->assertSame('TestCache', (string)$definition->getArgument(1)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
|
|
public function test_pool_cache() |
88
|
|
|
{ |
89
|
|
|
$container = $this->createContainerFromFile('with_pool_cache', [], false); |
90
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass()]); |
91
|
|
|
$container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true)); |
92
|
|
|
$container->compile(); |
93
|
|
|
|
94
|
|
|
$definition = $container->getDefinition('bdf_serializer.metadata_factory'); |
95
|
|
|
$this->assertSame('bdf_serializer.cache', (string)$definition->getArgument(1)); |
96
|
|
|
|
97
|
|
|
$definition = $container->getDefinition('bdf_serializer.cache'); |
98
|
|
|
$this->assertSame('cache.app', (string)$definition->getArgument(0)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $data |
103
|
|
|
* @return ContainerBuilder |
104
|
|
|
*/ |
105
|
|
|
protected function createContainer(array $data = []) |
106
|
|
|
{ |
107
|
|
|
return new ContainerBuilder(new EnvPlaceholderParameterBag($data)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $file |
112
|
|
|
* @param array $data |
113
|
|
|
* @param bool $compile |
114
|
|
|
* @return ContainerBuilder |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
|
protected function createContainerFromFile($file, $data = [], $compile = true) |
118
|
|
|
{ |
119
|
|
|
$container = $this->createContainer($data); |
120
|
|
|
$container->registerExtension(new BdfSerializerExtension()); |
121
|
|
|
|
122
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/config')); |
123
|
|
|
$loader->load($file.'.yml'); |
124
|
|
|
|
125
|
|
|
if (!$compile) { |
126
|
|
|
return $container; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$container->compile(); |
130
|
|
|
|
131
|
|
|
return $container; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
class TestCaseAllPublicCompilerPass implements CompilerPassInterface |
136
|
|
|
{ |
137
|
|
|
public function process(ContainerBuilder $container) : void |
138
|
|
|
{ |
139
|
|
|
foreach ($container->getDefinitions() as $id => $definition) { |
140
|
|
|
if (strpos($id, 'bdf_serializer') === false) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$definition->setPublic(true); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
foreach ($container->getAliases() as $id => $alias) { |
148
|
|
|
if (strpos($id, 'bdf_serializer') === false) { |
149
|
|
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$alias->setPublic(true); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |