1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\SerializerBundle\Tests\DependencyInjection; |
20
|
|
|
|
21
|
|
|
use JMS\SerializerBundle\DependencyInjection\Compiler\TwigExtensionPass; |
22
|
|
|
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension; |
23
|
|
|
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass; |
24
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass; |
25
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
27
|
|
|
|
28
|
|
|
class TwigExtensionPassTest extends \PHPUnit_Framework_TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @return ContainerBuilder |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
private function getContainer() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$loader = new JMSSerializerExtension(); |
36
|
|
|
$container = new ContainerBuilder(); |
37
|
|
|
|
38
|
|
|
$container->getCompilerPassConfig()->setOptimizationPasses(array( |
39
|
|
|
new ResolveParameterPlaceHoldersPass(), |
40
|
|
|
new ResolveDefinitionTemplatesPass(), |
41
|
|
|
)); |
42
|
|
|
$container->getCompilerPassConfig()->setRemovingPasses(array(new RemoveUnusedDefinitionsPass())); |
43
|
|
|
|
44
|
|
|
$container->setParameter('kernel.debug', true); |
45
|
|
|
$container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); |
46
|
|
|
$container->setParameter('kernel.bundles', array()); |
47
|
|
|
|
48
|
|
|
$loader->load([[]], $container); |
49
|
|
|
return $container; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testStandardExtension() |
53
|
|
|
{ |
54
|
|
|
$container = $this->getContainer(); |
55
|
|
|
|
56
|
|
|
$pass = new TwigExtensionPass(); |
57
|
|
|
$pass->process($container); |
58
|
|
|
|
59
|
|
|
$extension = $container->getDefinition('jms_serializer.twig_extension.serializer'); |
60
|
|
|
$this->assertEquals('%jms_serializer.twig_extension.class%', (string)$extension->getClass()); |
61
|
|
|
$this->assertCount(1, $extension->getArguments()); |
62
|
|
|
|
63
|
|
|
$this->assertFalse($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testLazyExtension() |
67
|
|
|
{ |
68
|
|
|
if ( |
69
|
|
|
!class_exists('JMS\Serializer\Twig\SerializerRuntimeExtension') |
70
|
|
|
|| !interface_exists('Twig_RuntimeLoaderInterface') |
71
|
|
|
) { |
72
|
|
|
$this->markTestSkipped("Lazy extensions are supported only by serializer 1.7.0"); |
73
|
|
|
} |
74
|
|
|
$container = $this->getContainer(); |
75
|
|
|
|
76
|
|
|
$container->register('twig.runtime_loader'); |
77
|
|
|
|
78
|
|
|
$pass = new TwigExtensionPass(); |
79
|
|
|
$pass->process($container); |
80
|
|
|
|
81
|
|
|
$extension = $container->getDefinition('jms_serializer.twig_extension.serializer'); |
82
|
|
|
$this->assertEquals('%jms_serializer.twig_runtime_extension.class%', (string)$extension->getClass()); |
83
|
|
|
$this->assertCount(0, $extension->getArguments()); |
84
|
|
|
|
85
|
|
|
$this->assertTrue($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testLazyExtensionNotLoadedWhenOldSerializer() |
89
|
|
|
{ |
90
|
|
|
$container = $this->getContainer(); |
91
|
|
|
|
92
|
|
|
$container->getParameterBag()->add(array( |
93
|
|
|
'jms_serializer.twig_runtime_extension.class' => 'foo', |
94
|
|
|
'jms_serializer.twig_runtime_extension_helper.class' => 'bar', |
95
|
|
|
)); |
96
|
|
|
|
97
|
|
|
$container->register('twig.runtime_loader'); |
98
|
|
|
|
99
|
|
|
$pass = new TwigExtensionPass(); |
100
|
|
|
$pass->process($container); |
101
|
|
|
|
102
|
|
|
$extension = $container->getDefinition('jms_serializer.twig_extension.serializer'); |
103
|
|
|
$this->assertEquals('%jms_serializer.twig_extension.class%', (string)$extension->getClass()); |
104
|
|
|
$this->assertCount(1, $extension->getArguments()); |
105
|
|
|
|
106
|
|
|
$this->assertFalse($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper')); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.