Skip to content

Commit 717165a

Browse files
committed
Merge pull request #906 from WouterJ/patch-2
[DX] Improved error message on wrong error
2 parents e7afb7a + 1322b35 commit 717165a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ private function addExceptionSection(ArrayNodeDefinition $rootNode)
235235
->scalarNode('exception_controller')->defaultNull()->end()
236236
->arrayNode('codes')
237237
->useAttributeAsKey('name')
238+
->validate()
239+
->ifTrue(function ($v) { return 0 !== count(array_filter($v, function ($i) { return !defined('FOS\RestBundle\Util\Codes::'.$i) && !is_int($i); })); })
240+
->thenInvalid('Invalid HTTP code in fos_rest.exception.codes, see FOS\RestBundle\Util\Codes for all valid codes.')
241+
->end()
238242
->prototype('scalar')->end()
239243
->end()
240244
->arrayNode('messages')

Tests/DependencyInjection/FOSRestExtensionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ public function testLoadBadClassThrowsException()
370370

371371
/**
372372
* @expectedException \InvalidArgumentException
373+
* @expectedExceptionMessage Could not load class 'UnknownException' or the class does not extend from '\Exception'
373374
*/
374375
public function testLoadBadMessagesClassThrowsException()
375376
{
@@ -398,6 +399,39 @@ public function testLoadOkMessagesClass()
398399
$this->assertFalse($this->container->hasDefinition('fos_rest.exception.codes'));
399400
}
400401

402+
/**
403+
* @dataProvider getLoadBadCodeValueThrowsExceptionData
404+
*
405+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
406+
* @expectedExceptionMessage Invalid HTTP code in fos_rest.exception.codes
407+
*/
408+
public function testLoadBadCodeValueThrowsException($value)
409+
{
410+
$this->extension->load(array(
411+
'fos_rest' => array(
412+
'exception' => array(
413+
'codes' => array(
414+
'Exception' => $value,
415+
),
416+
),
417+
),
418+
), $this->container);
419+
}
420+
421+
public function getLoadBadCodeValueThrowsExceptionData()
422+
{
423+
$data = array(
424+
null,
425+
'HTTP_NOT_EXISTS',
426+
'some random string',
427+
true,
428+
);
429+
430+
return array_map(function ($i) {
431+
return array($i);
432+
}, $data);
433+
}
434+
401435
/**
402436
* Assert that loader definition described properly.
403437
*

0 commit comments

Comments
 (0)