Skip to content

Commit

Permalink
[Locale] fixed StubNumberFormatter::parse() to behave like the Number…
Browse files Browse the repository at this point in the history
…Formatter::parse() regarding to error flagging
  • Loading branch information
eriksencosta committed Apr 17, 2012
1 parent 0a60664 commit f16ff89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Symfony/Component/Locale/Stub/StubNumberFormatter.php
Expand Up @@ -501,7 +501,7 @@ public function parse($value, $type = self::TYPE_DOUBLE, &$position = null)

// Any string before the numeric value causes error in the parsing
if (isset($matches[1]) && !empty($matches[1])) {
StubIntl::setError(StubIntl::U_PARSE_ERROR);
StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Number parsing failed');
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();

Expand All @@ -510,8 +510,14 @@ public function parse($value, $type = self::TYPE_DOUBLE, &$position = null)

// Remove everything that is not number or dot (.)
$value = preg_replace('/[^0-9\.\-]/', '', $value);
$value = $this->convertValueDataType($value, $type);

return $this->convertValueDataType($value, $type);
// behave like the intl extension
StubIntl::setError(StubIntl::U_ZERO_ERROR);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();

return $value;
}

/**
Expand Down
Expand Up @@ -669,10 +669,19 @@ public function testParseStub($value, $expected, $message = '')
$this->assertSame($expected, $parsedValue, $message);

if ($expected === false) {
$this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_PARSE_ERROR;
$errorMessage = 'Number parsing failed: U_PARSE_ERROR';
} else {
$this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
}

$this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
}

/**
Expand All @@ -682,15 +691,25 @@ public function testParseIntl($value, $expected, $message = '')
{
$this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld();

$formatter = $this->getIntlFormatterWithDecimalStyle();
$parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
$this->assertSame($expected, $parsedValue, $message);

if ($expected === false) {
$this->assertSame(U_PARSE_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_PARSE_ERROR;
$errorMessage = 'Number parsing failed: U_PARSE_ERROR';
} else {
$this->assertEquals(U_ZERO_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
}

$this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code());
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode()));
}

public function parseProvider()
Expand Down

0 comments on commit f16ff89

Please sign in to comment.