ParameterParser: getFloat with empty string value throws exception

This commit is contained in:
marc tobias
2018-03-06 13:35:27 +01:00
parent 123a3c0347
commit 47258f40ea
2 changed files with 9 additions and 6 deletions

View File

@@ -36,7 +36,7 @@ class ParameterParser
public function getFloat($sName, $bDefault = false) public function getFloat($sName, $bDefault = false)
{ {
if (!isset($this->aParams[$sName]) || strlen($this->aParams[$sName]) == 0) { if (!isset($this->aParams[$sName])) {
return $bDefault; return $bDefault;
} }

View File

@@ -73,20 +73,23 @@ class ParameterParserTest extends \PHPUnit_Framework_TestCase
$oParams = new ParameterParser([ $oParams = new ParameterParser([
'float1' => '1.0', 'float1' => '1.0',
'float2' => '-5', 'float2' => '-5',
'float3' => '', 'float3' => 0
'float4' => 0
]); ]);
$this->assertSame(false, $oParams->getFloat('non-exists')); $this->assertSame(false, $oParams->getFloat('non-exists'));
$this->assertSame(999, $oParams->getFloat('non-exists', 999)); $this->assertSame(999, $oParams->getFloat('non-exists', 999));
$this->assertSame(1.0, $oParams->getFloat('float1')); $this->assertSame(1.0, $oParams->getFloat('float1'));
$this->assertSame(-5.0, $oParams->getFloat('float2')); $this->assertSame(-5.0, $oParams->getFloat('float2'));
$this->assertSame(false, $oParams->getFloat('float3')); // FIXME: should be 0 instead? $this->assertSame(0.0, $oParams->getFloat('float3'));
$this->assertSame(0.0, $oParams->getFloat('float4'));
} }
public function testGetFloatWithEmptyString()
{
$this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float4'");
(new ParameterParser(['float4' => '']))->getFloat('float4');
}
public function testGetFloatWithString() public function testGetFloatWithTextString()
{ {
$this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float5'"); $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float5'");
(new ParameterParser(['float5' => 'a']))->getFloat('float5'); (new ParameterParser(['float5' => 'a']))->getFloat('float5');