-
Notifications
You must be signed in to change notification settings - Fork 708
/
FOSRestExtensionTest.php
225 lines (187 loc) · 8.24 KB
/
FOSRestExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Tests\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder,
Symfony\Component\DependencyInjection\Definition,
Symfony\Component\DependencyInjection\Reference;
use FOS\RestBundle\DependencyInjection\FOSRestExtension;
/**
* FOSRestExtension test.
*
* @author Bulat Shakirzyanov <avalanche123>
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class FOSRestExtensionTest extends \PHPUnit_Framework_TestCase
{
private $container;
private $extension;
public function setUp()
{
$this->container = new ContainerBuilder();
$this->extension = new FOSRestExtension();
}
public function tearDown()
{
unset($this->container, $this->extension);
}
public function testDisableBodyListener()
{
$config = array(
'fos_rest' => array('body_listener' => false)
);
$this->extension->load($config, $this->container);
$this->assertFalse($this->container->hasDefinition('fos_rest.body_listener'));
}
public function testLoadBodyListenerWithDefaults()
{
$this->extension->load(array(), $this->container);
$decoders = array(
'json' => 'fos_rest.decoder.json',
'xml' => 'fos_rest.decoder.xml'
);
$this->assertTrue($this->container->hasDefinition('fos_rest.body_listener'));
$this->assertParameter($decoders, 'fos_rest.decoders');
}
public function testDisableFormatListener()
{
$config = array(
'fos_rest' => array('format_listener' => false)
);
$this->extension->load($config, $this->container);
$this->assertFalse($this->container->hasDefinition('fos_rest.format_listener'));
}
public function testLoadFormatListenerWithDefaults()
{
$this->extension->load(array(), $this->container);
$this->assertTrue($this->container->hasDefinition('fos_rest.format_listener'));
$this->assertParameter(array('html', '*/*'), 'fos_rest.default_priorities');
$this->assertParameter('html', 'fos_rest.fallback_format');
}
public function testDisableFlashMessageListener()
{
$config = array(
'fos_rest' => array('flash_message_listener' => false)
);
$this->extension->load($config, $this->container);
$this->assertFalse($this->container->hasDefinition('fos_rest.flash_message_listener'));
}
public function testLoadFlashMessageListenerWithDefaults()
{
$this->extension->load(array(), $this->container);
$options = array(
'name' => 'flashes',
'path' => '/',
'domain' => null,
'secure' => '',
'httpOnly' => true
);
$this->assertTrue($this->container->hasDefinition('fos_rest.flash_message_listener'));
$this->assertParameter($options, 'fos_rest.flash_message_listener.options');
}
public function testLoadServicesWithDefaults()
{
$this->extension->load(array(), $this->container);
$this->assertAlias('fos_rest.view_handler.default', 'fos_rest.view_handler');
}
public function testLoadFormatsWithDefaults()
{
$this->extension->load(array(), $this->container);
$formats = array(
'json' => false,
'xml' => false,
'html' => true
);
$this->assertEquals($formats, $this->container->getParameter('fos_rest.formats'));
}
public function testDisableViewResponseListener()
{
$config = array(
'fos_rest' => array('view' => array('view_response_listener' => false))
);
$this->extension->load($config, $this->container);
$this->assertFalse($this->container->hasDefinition('fos_rest.view_response_listener'));
}
/**
* Test that extension loads properly.
*/
public function testConfigLoad()
{
$controllerLoaderDefinitionName = 'fos_rest.routing.loader.controller';
$controllerLoaderClassParameter = 'fos_rest.routing.loader.controller.class';
$controllerLoaderClass = 'FOS\RestBundle\Routing\Loader\RestRouteLoader';
$yamlCollectionLoaderDefinitionName = 'fos_rest.routing.loader.yaml_collection';
$yamlCollectionLoaderClassParameter = 'fos_rest.routing.loader.yaml_collection.class';
$yamlCollectionLoaderClass = 'FOS\RestBundle\Routing\Loader\RestYamlCollectionLoader';
$xmlCollectionLoaderDefinitionName = 'fos_rest.routing.loader.xml_collection';
$xmlCollectionLoaderClassParameter = 'fos_rest.routing.loader.xml_collection.class';
$xmlCollectionLoaderClass = 'FOS\RestBundle\Routing\Loader\RestXmlCollectionLoader';
$this->extension->load(array(), $this->container);
$this->assertEquals($controllerLoaderClass, $this->container->getParameter($controllerLoaderClassParameter));
$this->assertTrue($this->container->hasDefinition($controllerLoaderDefinitionName));
$this->assertValidRestRouteLoader(
$this->container->getDefinition($controllerLoaderDefinitionName),
$controllerLoaderClassParameter
);
$this->assertEquals($yamlCollectionLoaderClass, $this->container->getParameter($yamlCollectionLoaderClassParameter));
$this->assertTrue($this->container->hasDefinition($yamlCollectionLoaderDefinitionName));
$this->assertValidRestFileLoader(
$this->container->getDefinition($yamlCollectionLoaderDefinitionName),
$yamlCollectionLoaderClassParameter
);
$this->assertEquals($xmlCollectionLoaderClass, $this->container->getParameter($xmlCollectionLoaderClassParameter));
$this->assertTrue($this->container->hasDefinition($xmlCollectionLoaderDefinitionName));
$this->assertValidRestFileLoader(
$this->container->getDefinition($xmlCollectionLoaderDefinitionName),
$xmlCollectionLoaderClassParameter
);
}
/**
* Assert that loader definition described properly.
*
* @param Definition $loader loader definition
* @param string $loaderClassParameter loader class parameter name
*/
private function assertValidRestRouteLoader(Definition $loader, $loaderClassParameter)
{
$arguments = $loader->getArguments();
$this->assertEquals('%' . $loaderClassParameter . '%', $loader->getClass());
$this->assertEquals(4, count($arguments));
$this->assertEquals('service_container', (string) $arguments[0]);
$this->assertEquals('controller_name_converter', (string) $arguments[1]);
$this->assertEquals('annotation_reader', (string) $arguments[2]);
$this->assertEquals('%fos_rest.routing.loader.default_format%', (string) $arguments[3]);
$this->assertArrayHasKey('routing.loader', $loader->getTags());
}
/**
* Assert that loader definition described properly.
*
* @param Definition $loader loader definition
* @param string $loaderClassParameter loader class parameter name
*/
private function assertValidRestFileLoader(Definition $loader, $loaderClassParameter)
{
$locatorRef = new Reference('file_locator');
$processorRef = new Reference('fos_rest.routing.loader.processor');
$arguments = $loader->getArguments();
$this->assertEquals('%' . $loaderClassParameter . '%', $loader->getClass());
$this->assertEquals(2, count($arguments));
$this->assertEquals($locatorRef, $arguments[0]);
$this->assertEquals($processorRef, $arguments[1]);
$this->assertArrayHasKey('routing.loader', $loader->getTags());
}
private function assertAlias($value, $key)
{
$this->assertEquals($value, (string) $this->container->getAlias($key), sprintf('%s alias is correct', $key));
}
private function assertParameter($value, $key)
{
$this->assertEquals($value, $this->container->getParameter($key), sprintf('%s parameter is correct', $key));
}
}