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
|
|
|
public function testDefaultConfig() |
24
|
|
|
{ |
25
|
|
|
$container = $this->createContainerFromFile('default'); |
26
|
|
|
|
27
|
|
|
$this->assertTrue($container->getDefinition(SerializerInterface::class)->isPublic()); |
28
|
|
|
$this->assertTrue($container->getAlias('bdf_serializer')->isPublic()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testNormalizers() |
32
|
|
|
{ |
33
|
|
|
$container = $this->createContainerFromFile('default', [], false); |
34
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass(), new SerializerNormalizerPass()]); |
35
|
|
|
$container->compile(); |
36
|
|
|
|
37
|
|
|
$definition = $container->getDefinition('bdf_serializer.normalizer.loader'); |
38
|
|
|
|
39
|
|
|
$normalizers = $definition->getArgument(0); |
40
|
|
|
$this->assertCount(3, $normalizers); |
41
|
|
|
$this->assertSame('bdf_serializer.normalizer.datetime', (string) $normalizers[0]); |
42
|
|
|
$this->assertSame('bdf_serializer.normalizer.traversable', (string) $normalizers[1]); |
43
|
|
|
$this->assertSame('bdf_serializer.normalizer.property', (string) $normalizers[2]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testLoaders() |
47
|
|
|
{ |
48
|
|
|
$container = $this->createContainerFromFile('default', [], false); |
49
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass(), new SerializerLoaderPass()]); |
50
|
|
|
$container->compile(); |
51
|
|
|
|
52
|
|
|
$definition = $container->getDefinition('bdf_serializer.metadata_factory'); |
53
|
|
|
|
54
|
|
|
$loaders = $definition->getArgument(0); |
55
|
|
|
$this->assertCount(2, $loaders); |
56
|
|
|
$this->assertSame('bdf_serializer.metadata.loader.static_method', (string) $loaders[0]); |
57
|
|
|
$this->assertSame('bdf_serializer.metadata.loader.annotation', (string) $loaders[1]); |
58
|
|
|
$this->assertSame(null, $definition->getArgument(1)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testServiceCache() |
62
|
|
|
{ |
63
|
|
|
$container = $this->createContainerFromFile('with_cache', [], false); |
64
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass()]); |
65
|
|
|
$container->setDefinition('TestCache', (new Definition('TestCache'))->setPublic(true)); |
66
|
|
|
$container->compile(); |
67
|
|
|
|
68
|
|
|
$definition = $container->getDefinition('bdf_serializer.metadata_factory'); |
69
|
|
|
$this->assertSame('TestCache', (string) $definition->getArgument(1)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testPoolCache() |
73
|
|
|
{ |
74
|
|
|
$container = $this->createContainerFromFile('with_pool_cache', [], false); |
75
|
|
|
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([new TestCaseAllPublicCompilerPass()]); |
76
|
|
|
$container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true)); |
77
|
|
|
$container->compile(); |
78
|
|
|
|
79
|
|
|
$definition = $container->getDefinition('bdf_serializer.metadata_factory'); |
80
|
|
|
$this->assertSame('bdf_serializer.cache', (string) $definition->getArgument(1)); |
81
|
|
|
|
82
|
|
|
$definition = $container->getDefinition('bdf_serializer.cache'); |
83
|
|
|
$this->assertSame('cache.app', (string) $definition->getArgument(0)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return ContainerBuilder |
88
|
|
|
*/ |
89
|
|
|
protected function createContainer(array $data = []) |
90
|
|
|
{ |
91
|
|
|
return new ContainerBuilder(new EnvPlaceholderParameterBag($data)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $file |
96
|
|
|
* @param array $data |
97
|
|
|
* @param bool $compile |
98
|
|
|
* |
99
|
|
|
* @return ContainerBuilder |
100
|
|
|
* |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
protected function createContainerFromFile($file, $data = [], $compile = true) |
104
|
|
|
{ |
105
|
|
|
$container = $this->createContainer($data); |
106
|
|
|
$container->registerExtension(new BdfSerializerExtension()); |
107
|
|
|
|
108
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/config')); |
109
|
|
|
$loader->load($file.'.yml'); |
110
|
|
|
|
111
|
|
|
if (!$compile) { |
112
|
|
|
return $container; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$container->compile(); |
116
|
|
|
|
117
|
|
|
return $container; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
class TestCaseAllPublicCompilerPass implements CompilerPassInterface |
122
|
|
|
{ |
123
|
|
|
public function process(ContainerBuilder $container): void |
124
|
|
|
{ |
125
|
|
|
foreach ($container->getDefinitions() as $id => $definition) { |
126
|
|
|
if (false === strpos($id, 'bdf_serializer')) { |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$definition->setPublic(true); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
foreach ($container->getAliases() as $id => $alias) { |
134
|
|
|
if (false === strpos($id, 'bdf_serializer')) { |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$alias->setPublic(true); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|