Skip to content

Commit 3110796

Browse files
dbuNyholm
authored andcommitted
validate type specific options
1 parent 5e3a44c commit 3110796

File tree

7 files changed

+200
-28
lines changed

7 files changed

+200
-28
lines changed

DependencyInjection/Configuration.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,16 @@ private function addAuthenticationPluiginNode()
243243
->then(function ($config) {
244244
switch ($config['type']) {
245245
case 'basic':
246-
if (empty($config['username']) || empty($config['password'])) {
247-
throw new InvalidConfigurationException('Authentication "basic" requires both "username" and "password".');
248-
}
246+
$this->validateAuthenticationType(['username', 'password'], $config, 'basic');
249247
break;
250248
case 'bearer':
251-
if (empty($config['token'])) {
252-
throw new InvalidConfigurationException('Authentication "bearer" requires a "token".');
253-
}
249+
$this->validateAuthenticationType(['token'], $config, 'bearer');
254250
break;
255251
case 'service':
256-
if (empty($config['service'])) {
257-
throw new InvalidConfigurationException('Authentication "service" requires a "service".');
258-
}
252+
$this->validateAuthenticationType(['service'], $config, 'service');
259253
break;
260254
case 'wsse':
261-
if (empty($config['username']) || empty($config['password'])) {
262-
throw new InvalidConfigurationException('Authentication "wsse" requires both "username" and "password".');
263-
}
255+
$this->validateAuthenticationType(['username', 'password'], $config, 'wsse');
264256
break;
265257
}
266258

@@ -283,4 +275,32 @@ private function addAuthenticationPluiginNode()
283275

284276
return $node;
285277
}
278+
279+
/**
280+
* Validate that the configuration fragment has the specified keys and none other.
281+
*
282+
* @param array $expected Fields that must exist
283+
* @param array $actual Actual configuration hashmap
284+
* @param string $authName Name of authentication method for error messages
285+
*
286+
* @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type')
287+
*/
288+
private function validateAuthenticationType(array $expected, array $actual, $authName)
289+
{
290+
unset($actual['type']);
291+
$actual = array_keys($actual);
292+
sort($actual);
293+
sort($expected);
294+
295+
if ($expected === $actual) {
296+
return;
297+
}
298+
299+
throw new InvalidConfigurationException(sprintf(
300+
'Authentication "%s" requires %s but got %s',
301+
$authName,
302+
implode(', ', $expected),
303+
implode(', ', $actual)
304+
));
305+
}
286306
}

Tests/Resources/Fixtures/config/full.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,58 @@
1313
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory',
1414
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory',
1515
],
16+
'toolbar' => [
17+
'enabled' => true,
18+
'formatter' => 'my_toolbar_formatter',
19+
],
20+
'plugins' => [
21+
'authentication' => [
22+
'my_basic' => [
23+
'type' => 'basic',
24+
'username' => 'foo',
25+
'password' => 'bar',
26+
],
27+
'my_wsse' => [
28+
'type' => 'wsse',
29+
'username' => 'foo',
30+
'password' => 'bar',
31+
],
32+
'my_brearer' => [
33+
'type' => 'bearer',
34+
'token' => 'foo',
35+
],
36+
'my_service' => [
37+
'type' => 'service',
38+
'service' => 'my_auth_serivce',
39+
],
40+
],
41+
'cache' => [
42+
'stream_factory' => 'my_other_stream_factory',
43+
'config' => [
44+
'default_ttl' => 42,
45+
'respect_cache_headers' => false,
46+
],
47+
],
48+
'cookie' => [
49+
'cookie_jar' => 'my_cookie_jar',
50+
],
51+
'decoder' => [
52+
'enabled' => false,
53+
],
54+
'history' => [
55+
'journal' => 'my_journal',
56+
],
57+
'logger' => [
58+
'enabled' => false,
59+
],
60+
'redirect' => [
61+
'enabled' => false,
62+
],
63+
'retry' => [
64+
'enabled' => false,
65+
],
66+
'stopwatch' => [
67+
'enabled' => false,
68+
],
69+
],
1670
]);

Tests/Resources/Fixtures/config/full.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@
1414
<uri-factory>Http\Message\UriFactory\GuzzleUriFactory</uri-factory>
1515
<stream-factory>Http\Message\StreamFactory\GuzzleStreamFactory</stream-factory>
1616
</classes>
17+
<toolbar enabled="true" formatter="my_toolbar_formatter"/>
18+
<plugins>
19+
<authentication>
20+
<my_basic type="basic" username="foo" password="bar"/>
21+
<my_wsse type="wsse" username="foo" password="bar"/>
22+
<my_bearer type="bearer" token="foo"/>
23+
<my_service type="service" service="my_auth_service"/>
24+
</authentication>
25+
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
26+
<config default-ttl="42" respect-cache-headers="false"/>
27+
</cache>
28+
<cookie cookie-jar="my_cookie_jar"/>
29+
<decoder enabled="false"/>
30+
<history journal="my_journal"/>
31+
<logger enabled="false"/>
32+
<redirect enabled="false"/>
33+
<retry enabled="false"/>
34+
<stopwatch enabled="false"/>
35+
</plugins>
1736
</config>
1837

1938
</container>

Tests/Resources/Fixtures/config/full.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,43 @@ httplug:
88
client: Http\Adapter\Guzzle6\Client
99
message_factory: Http\Message\MessageFactory\GuzzleMessageFactory
1010
uri_factory: Http\Message\UriFactory\GuzzleUriFactory
11-
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
11+
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
12+
toolbar:
13+
enabled: true
14+
formatter: my_toolbar_formatter
15+
plugins:
16+
authentication:
17+
my_basic:
18+
type: basic
19+
username: foo
20+
password: bar
21+
my_wsse:
22+
type: wsse
23+
username: foo
24+
password: bar
25+
my_brearer:
26+
type: bearer
27+
token: foo
28+
my_service:
29+
type: service
30+
service: my_auth_serivce
31+
cache:
32+
cache_pool: my_cache_pool
33+
stream_factory: my_other_stream_factory
34+
config:
35+
default_ttl: 42
36+
respect_cache_headers: false
37+
cookie:
38+
cookie_jar: my_cookie_jar
39+
decoder:
40+
enabled: false
41+
history:
42+
journal: my_journal
43+
logger:
44+
enabled: false
45+
redirect:
46+
enabled: false
47+
retry:
48+
enabled: false
49+
stopwatch:
50+
enabled: false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
httplug:
2+
plugins:
3+
authentication:
4+
my_auth:
5+
type: service
6+
service: foobar
7+
username: user
8+
password: pass

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,45 +112,67 @@ public function testSupportsAllConfigFormats()
112112
],
113113
'clients' => [],
114114
'toolbar' => [
115-
'enabled' => 'auto',
116-
'formatter' => null,
115+
'enabled' => true,
116+
'formatter' => 'my_toolbar_formatter',
117117
],
118118
'plugins' => [
119-
'authentication' => [],
119+
'authentication' => [
120+
'my_basic' => [
121+
'type' => 'basic',
122+
'username' => 'foo',
123+
'password' => 'bar',
124+
],
125+
'my_wsse' => [
126+
'type' => 'wsse',
127+
'username' => 'foo',
128+
'password' => 'bar',
129+
],
130+
'my_brearer' => [
131+
'type' => 'bearer',
132+
'token' => 'foo',
133+
],
134+
'my_service' => [
135+
'type' => 'service',
136+
'service' => 'my_auth_serivce',
137+
],
138+
],
120139
'cache' => [
121-
'enabled' => false,
122-
'stream_factory' => 'httplug.stream_factory',
140+
'enabled' => true,
141+
'cache_pool' => 'my_cache_pool',
142+
'stream_factory' => 'my_other_stream_factory',
123143
'config' => [
124-
'default_ttl' => null,
125-
'respect_cache_headers' => true,
144+
'default_ttl' => 42,
145+
'respect_cache_headers' => false,
126146
],
127147
],
128148
'cookie' => [
129-
'enabled' => false,
149+
'enabled' => true,
150+
'cookie_jar' => 'my_cookie_jar',
130151
],
131152
'decoder' => [
132-
'enabled' => true,
153+
'enabled' => false,
133154
'use_content_encoding' => true,
134155
],
135156
'history' => [
136-
'enabled' => false,
157+
'enabled' => true,
158+
'journal' => 'my_journal',
137159
],
138160
'logger' => [
139-
'enabled' => true,
161+
'enabled' => false,
140162
'logger' => 'logger',
141163
'formatter' => null,
142164
],
143165
'redirect' => [
144-
'enabled' => true,
166+
'enabled' => false,
145167
'preserve_header' => true,
146168
'use_default_for_multiple' => true,
147169
],
148170
'retry' => [
149-
'enabled' => true,
171+
'enabled' => false,
150172
'retry' => 1,
151173
],
152174
'stopwatch' => [
153-
'enabled' => true,
175+
'enabled' => false,
154176
'stopwatch' => 'debug.stopwatch',
155177
],
156178
],
@@ -175,7 +197,17 @@ public function testSupportsAllConfigFormats()
175197
*/
176198
public function testMissingClass()
177199
{
178-
$file = __DIR__.'/../../Resources/Fixtures/config/invalid.yml';
200+
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_class.yml';
201+
$this->assertProcessedConfigurationEquals([], [$file]);
202+
}
203+
204+
/**
205+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
206+
* @expectedExceptionMessage password, service, username
207+
*/
208+
public function testInvalidAuthentication()
209+
{
210+
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_auth.yml';
179211
$this->assertProcessedConfigurationEquals([], [$file]);
180212
}
181213
}

0 commit comments

Comments
 (0)