merged 2.0

This commit is contained in:
Fabien Potencier 2012-04-18 10:38:31 +02:00
commit 92b0824900
17 changed files with 358 additions and 137 deletions

View File

@ -180,7 +180,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader->load('badtag1.yml'); $loader->load('badtag1.yml');
$this->fail('->load() should throw an exception when the tags key of a service is not an array'); $this->fail('->load() should throw an exception when the tags key of a service is not an array');
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array'); $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tags key is not an array');
$this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array'); $this->assertStringStartsWith('Parameter "tags" must be an array for service', $e->getMessage(), '->load() throws an InvalidArgumentException if the tags key is not an array');
} }
} }
@ -192,7 +192,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader->load('badtag2.yml'); $loader->load('badtag2.yml');
$this->fail('->load() should throw an exception when a tag is missing the name key'); $this->fail('->load() should throw an exception when a tag is missing the name key');
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key'); $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if a tag is missing the name key');
$this->assertStringStartsWith('A "tags" entry is missing a "name" key for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is missing the name key'); $this->assertStringStartsWith('A "tags" entry is missing a "name" key for service ', $e->getMessage(), '->load() throws an InvalidArgumentException if a tag is missing the name key');
} }
} }

View File

@ -33,7 +33,7 @@ class MethodArgumentValueNotImplementedException extends NotImplementedException
$methodName, $methodName,
$argName, $argName,
var_export($argValue, true), var_export($argValue, true),
$additionalMessage != '' ? ' '.$additionalMessage.'. ' : '' $additionalMessage !== '' ? ' '.$additionalMessage.'. ' : ''
); );
parent::__construct($message); parent::__construct($message);

View File

@ -45,3 +45,14 @@ function intl_get_error_code() {
function intl_get_error_message() { function intl_get_error_message() {
return StubIntl::getErrorMessage(); return StubIntl::getErrorMessage();
} }
/**
* Stub implementation for the intl_error_name function of the intl extension
*
* @return String will be the same as the name of the error code constant
*
* @see Symfony\Component\Locale\Stub\StubIntl::getErrorName
*/
function intl_error_name($errorCode) {
return StubIntl::getErrorName($errorCode);
}

View File

@ -155,7 +155,7 @@ class FullTransformer
} }
// behave like the intl extension // behave like the intl extension
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed');
return false; return false;
} }
@ -292,7 +292,7 @@ class FullTransformer
// If month is false, return immediately (intl behavior) // If month is false, return immediately (intl behavior)
if (false === $month) { if (false === $month) {
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed');
return false; return false;
} }

View File

@ -88,7 +88,7 @@ class TimeZoneTransformer extends Transformer
)); ));
} }
return 'Etc/GMT'.($hours != 0 ? $signal.$hours : ''); return 'Etc/GMT'.($hours !== 0 ? $signal.$hours : '');
} }
throw new \InvalidArgumentException('The GMT time zone \'%s\' does not match with the supported formats GMT[+-]HH:MM or GMT[+-]HHMM.'); throw new \InvalidArgumentException('The GMT time zone \'%s\' does not match with the supported formats GMT[+-]HH:MM or GMT[+-]HHMM.');

View File

@ -21,16 +21,6 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio
*/ */
class StubCollator class StubCollator
{ {
/**
* Constants defined by the intl extension, not class constants in IntlDateFormatter
* TODO: remove if the Form component drop the call to the intl_is_failure() function
*
* @see StubIntlDateFormatter::getErrorCode()
* @see StubIntlDateFormatter::getErrorMessage()
*/
const U_ZERO_ERROR = 0;
const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR';
/** Attribute constants */ /** Attribute constants */
const FRENCH_COLLATION = 0; const FRENCH_COLLATION = 0;
const ALTERNATE_HANDLING = 1; const ALTERNATE_HANDLING = 1;
@ -158,7 +148,7 @@ class StubCollator
*/ */
public function getErrorCode() public function getErrorCode()
{ {
return self::U_ZERO_ERROR; return StubIntl::U_ZERO_ERROR;
} }
/** /**
@ -168,7 +158,7 @@ class StubCollator
*/ */
public function getErrorMessage() public function getErrorMessage()
{ {
return self::U_ZERO_ERROR_MESSAGE; return 'U_ZERO_ERROR';
} }
/** /**

View File

@ -45,20 +45,9 @@ abstract class StubIntl
* @var array * @var array
*/ */
private static $errorCodes = array( private static $errorCodes = array(
self::U_ZERO_ERROR,
self::U_ILLEGAL_ARGUMENT_ERROR,
self::U_PARSE_ERROR,
);
/**
* The error messages of all known error codes
*
* @var array
*/
private static $errorMessages = array(
self::U_ZERO_ERROR => 'U_ZERO_ERROR', self::U_ZERO_ERROR => 'U_ZERO_ERROR',
self::U_ILLEGAL_ARGUMENT_ERROR => 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR', self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR',
self::U_PARSE_ERROR => 'Date parsing failed: U_PARSE_ERROR', self::U_PARSE_ERROR => 'U_PARSE_ERROR',
); );
/** /**
@ -68,6 +57,13 @@ abstract class StubIntl
*/ */
private static $errorCode = self::U_ZERO_ERROR; private static $errorCode = self::U_ZERO_ERROR;
/**
* The error code of the last operation
*
* @var integer
*/
private static $errorMessage = 'U_ZERO_ERROR';
/** /**
* Returns whether the error code indicates a failure * Returns whether the error code indicates a failure
* *
@ -77,8 +73,8 @@ abstract class StubIntl
*/ */
static public function isFailure($errorCode) static public function isFailure($errorCode)
{ {
return in_array($errorCode, self::$errorCodes, true) return isset(self::$errorCodes[$errorCode])
&& $errorCode !== self::U_ZERO_ERROR; && $errorCode > self::U_ZERO_ERROR;
} }
/** /**
@ -102,22 +98,38 @@ abstract class StubIntl
*/ */
static public function getErrorMessage() static public function getErrorMessage()
{ {
return self::$errorMessages[self::$errorCode]; return self::$errorMessage;
} }
/** /**
* Sets the current error code * Returns the symbolic name for a given error code
* *
* @param integer $code One of the error constants in this class * @return string
*/
static public function getErrorName($code)
{
if (isset(self::$errorCodes[$code])) {
return self::$errorCodes[$code];
}
return '[BOGUS UErrorCode]';
}
/**
* Sets the current error
*
* @param integer $code One of the error constants in this class
* @param string $message The ICU class error message
* *
* @throws \InvalidArgumentException If the code is not one of the error constants in this class * @throws \InvalidArgumentException If the code is not one of the error constants in this class
*/ */
static public function setErrorCode($code) static public function setError($code, $message = '')
{ {
if (!isset(self::$errorMessages[$code])) { if (!isset(self::$errorCodes[$code])) {
throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $code)); throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $code));
} }
self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code];
self::$errorCode = $code; self::$errorCode = $code;
} }
} }

View File

@ -26,14 +26,18 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio
class StubIntlDateFormatter class StubIntlDateFormatter
{ {
/** /**
* Constants defined by the intl extension, not class constants in IntlDateFormatter * The error code from the last operation
* TODO: remove if the Form component drop the call to the intl_is_failure() function
* *
* @see StubIntlDateFormatter::getErrorCode() * @var integer
* @see StubIntlDateFormatter::getErrorMessage()
*/ */
const U_ZERO_ERROR = 0; protected $errorCode = StubIntl::U_ZERO_ERROR;
const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR';
/**
* The error message from the last operation
*
* @var string
*/
protected $errorMessage = 'U_ZERO_ERROR';
/* date/time format types */ /* date/time format types */
const NONE = -1; const NONE = -1;
@ -120,11 +124,11 @@ class StubIntlDateFormatter
*/ */
public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null) public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null)
{ {
if ('en' != $locale) { if ('en' !== $locale) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the \'en\' locale is supported'); throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the \'en\' locale is supported');
} }
if (self::GREGORIAN != $calendar) { if (self::GREGORIAN !== $calendar) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported'); throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported');
} }
@ -171,24 +175,42 @@ class StubIntlDateFormatter
{ {
// intl allows timestamps to be passed as arrays - we don't // intl allows timestamps to be passed as arrays - we don't
if (is_array($timestamp)) { if (is_array($timestamp)) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, 'Only integer unix timestamps are supported'); $message = version_compare(\PHP_VERSION, '5.3.4', '>=') ?
'Only integer unix timestamps and DateTime objects are supported' :
'Only integer unix timestamps are supported';
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message);
} }
// behave like the intl extension // behave like the intl extension
if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) { $argumentError = null;
StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR); if (version_compare(\PHP_VERSION, '5.3.4', '<') && !is_int($timestamp)) {
$argumentError = 'datefmt_format: takes either an array or an integer timestamp value ';
} elseif (version_compare(\PHP_VERSION, '5.3.4', '>=') && !is_int($timestamp) && !$timestamp instanceOf \DateTime) {
$argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object';
}
if (null !== $argumentError) {
StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, $argumentError);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
return false; return false;
} }
// As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
if ($timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) { if (version_compare(\PHP_VERSION, '5.3.4', '>=') && $timestamp instanceOf \DateTime) {
$timestamp = $timestamp->getTimestamp(); $timestamp = $timestamp->getTimestamp();
} }
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
$formatted = $transformer->format($this->createDateTime($timestamp)); $formatted = $transformer->format($this->createDateTime($timestamp));
// behave like the intl extension
StubIntl::setError(StubIntl::U_ZERO_ERROR);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
return $formatted; return $formatted;
} }
@ -225,7 +247,7 @@ class StubIntlDateFormatter
*/ */
public function getErrorCode() public function getErrorCode()
{ {
return self::U_ZERO_ERROR; return $this->errorCode;
} }
/** /**
@ -237,7 +259,7 @@ class StubIntlDateFormatter
*/ */
public function getErrorMessage() public function getErrorMessage()
{ {
return self::U_ZERO_ERROR_MESSAGE; return $this->errorMessage;
} }
/** /**
@ -350,12 +372,18 @@ class StubIntlDateFormatter
throw new MethodArgumentNotImplementedException(__METHOD__, 'position'); throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
} }
StubIntl::setErrorCode(StubIntl::U_ZERO_ERROR);
$dateTime = $this->createDateTime(0); $dateTime = $this->createDateTime(0);
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
return $transformer->parse($dateTime, $value); $timestamp = $transformer->parse($dateTime, $value);
// behave like the intl extension. FullTransformer::parse() set the proper error
if (false === $timestamp) {
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
}
return $timestamp;
} }
/** /**

View File

@ -480,7 +480,7 @@ class StubLocale
*/ */
static private function getStubData($locale, $cacheVariable, $stubDataDir) static private function getStubData($locale, $cacheVariable, $stubDataDir)
{ {
if ('en' != $locale) { if ('en' !== $locale) {
throw new \InvalidArgumentException(sprintf('Only the \'en\' locale is supported. %s', NotImplementedException::INTL_INSTALL_MESSAGE)); throw new \InvalidArgumentException(sprintf('Only the \'en\' locale is supported. %s', NotImplementedException::INTL_INSTALL_MESSAGE));
} }

View File

@ -24,33 +24,6 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio
*/ */
class StubNumberFormatter class StubNumberFormatter
{ {
/**
* Constants defined by the intl extension, not class constants in NumberFormatter
* TODO: remove if the Form component drop the call to the intl_is_failure() function
*
* @see StubNumberFormatter::getErrorCode()
* @see StubNumberFormatter::getErrorMessage()
*/
const U_ZERO_ERROR = 0;
const U_PARSE_ERROR = 9;
/**
* The error messages for each error code
*
* @var array
*/
protected $errorMessages = array(
self::U_ZERO_ERROR => 'U_ZERO_ERROR',
self::U_PARSE_ERROR => 'Number parsing failed: U_PARSE_ERROR',
);
/**
* The error code from the last operation
*
* @var integer
*/
protected $errorCode = self::U_ZERO_ERROR;
/** Format style constants */ /** Format style constants */
const PATTERN_DECIMAL = 0; const PATTERN_DECIMAL = 0;
const DECIMAL = 1; const DECIMAL = 1;
@ -138,6 +111,30 @@ class StubNumberFormatter
const PAD_BEFORE_SUFFIX = 2; const PAD_BEFORE_SUFFIX = 2;
const PAD_AFTER_SUFFIX = 3; const PAD_AFTER_SUFFIX = 3;
/**
* The error code from the last operation
*
* @var integer
*/
protected $errorCode = StubIntl::U_ZERO_ERROR;
/**
* The error message from the last operation
*
* @var string
*/
protected $errorMessage = 'U_ZERO_ERROR';
/**
* @var string
*/
private $locale;
/**
* @var int
*/
private $style;
/** /**
* Default values for the en locale * Default values for the en locale
* *
@ -224,16 +221,6 @@ class StubNumberFormatter
'negative' => -9223372036854775808 'negative' => -9223372036854775808
); );
/**
* @var string
*/
private $locale = null;
/**
* @var int
*/
private $style = null;
/** /**
* Constructor * Constructor
* *
@ -364,8 +351,12 @@ class StubNumberFormatter
$fractionDigits = $this->getAttribute(self::FRACTION_DIGITS); $fractionDigits = $this->getAttribute(self::FRACTION_DIGITS);
$value = $this->round($value, $fractionDigits); $value = $this->round($value, $fractionDigits);
$value = $this->formatNumber($value, $fractionDigits);
return $this->formatNumber($value, $fractionDigits); // behave like the intl extension
$this->resetError();
return $value;
} }
/** /**
@ -403,7 +394,7 @@ class StubNumberFormatter
*/ */
public function getErrorMessage() public function getErrorMessage()
{ {
return $this->errorMessages[$this->errorCode]; return $this->errorMessage;
} }
/** /**
@ -514,15 +505,21 @@ class StubNumberFormatter
// Any string before the numeric value causes error in the parsing // Any string before the numeric value causes error in the parsing
if (isset($matches[1]) && !empty($matches[1])) { if (isset($matches[1]) && !empty($matches[1])) {
$this->errorCode = self::U_PARSE_ERROR; StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Number parsing failed');
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
return false; return false;
} }
// Remove everything that is not number or dot (.) // Remove everything that is not number or dot (.)
$value = preg_replace('/[^0-9\.\-]/', '', $value); $value = preg_replace('/[^0-9\.\-]/', '', $value);
$value = $this->convertValueDataType($value, $type);
return $this->convertValueDataType($value, $type); // behave like the intl extension
$this->resetError();
return $value;
} }
/** /**
@ -623,6 +620,16 @@ class StubNumberFormatter
throw new MethodNotImplementedException(__METHOD__); throw new MethodNotImplementedException(__METHOD__);
} }
/**
* Set the error to the default U_ZERO_ERROR
*/
protected function resetError()
{
StubIntl::setError(StubIntl::U_ZERO_ERROR);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
}
/** /**
* Rounds a currency value, applying increment rounding if applicable * Rounds a currency value, applying increment rounding if applicable
* *

View File

@ -13,7 +13,8 @@ namespace Symfony\Component\Locale\Tests\Stub;
use Symfony\Component\Locale\Locale; use Symfony\Component\Locale\Locale;
use Symfony\Component\Locale\Stub\StubCollator; use Symfony\Component\Locale\Stub\StubCollator;
use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase; use Symfony\Component\Locale\Stub\StubIntl;
use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
class StubCollatorTest extends LocaleTestCase class StubCollatorTest extends LocaleTestCase
{ {
@ -89,13 +90,13 @@ class StubCollatorTest extends LocaleTestCase
public function testGetErrorCode() public function testGetErrorCode()
{ {
$collator = $this->createStubCollator(); $collator = $this->createStubCollator();
$this->assertEquals(StubCollator::U_ZERO_ERROR, $collator->getErrorCode()); $this->assertEquals(StubIntl::U_ZERO_ERROR, $collator->getErrorCode());
} }
public function testGetErrorMessage() public function testGetErrorMessage()
{ {
$collator = $this->createStubCollator(); $collator = $this->createStubCollator();
$this->assertEquals(StubCollator::U_ZERO_ERROR_MESSAGE, $collator->getErrorMessage()); $this->assertEquals('U_ZERO_ERROR', $collator->getErrorMessage());
} }
public function testGetLocale() public function testGetLocale()

View File

@ -51,49 +51,69 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertNull($formatter->getTimeZoneId()); $this->assertNull($formatter->getTimeZoneId());
} }
public function testFormatWithUnsupportedTimestampArgument()
{
$formatter = $this->createStubFormatter();
$localtime = array(
'tm_sec' => 59,
'tm_min' => 3,
'tm_hour' => 15,
'tm_mday' => 15,
'tm_mon' => 3,
'tm_year' => 112,
'tm_wday' => 0,
'tm_yday' => 105,
'tm_isdst' => 0
);
try {
$formatter->format($localtime);
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException', $e);
if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
$this->assertStringEndsWith('Only integer unix timestamps and DateTime objects are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage());
} else {
$this->assertStringEndsWith('Only integer unix timestamps are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage());
}
}
}
/** /**
* @dataProvider formatProvider * @dataProvider formatProvider
*/ */
public function testFormatStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') public function testFormatStub($pattern, $timestamp, $expected)
{ {
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
$formatter = $this->createStubFormatter($pattern); $formatter = $this->createStubFormatter($pattern);
$this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($expected, $formatter->format($timestamp));
$this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertFalse(StubIntl::isFailure($formatter->getErrorCode()));
} }
/** /**
* @dataProvider formatProvider * @dataProvider formatProvider
*/ */
public function testFormatIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') public function testFormatIntl($pattern, $timestamp, $expected)
{
$this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld();
$formatter = $this->createIntlFormatter($pattern);
$this->assertSame($expected, $formatter->format($timestamp));
$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()));
}
/**
* @dataProvider formatErrorProvider
*/
public function testFormatErrorIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
{ {
$this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld(); $this->skipIfICUVersionIsTooOld();
if (version_compare(PHP_VERSION, '5.3.3') > 0) { $errorCode = StubIntl::U_ZERO_ERROR;
$this->markTestSkipped('The intl error messages were change in PHP 5.3.3.'); $errorMessage = 'U_ZERO_ERROR';
}
$formatter = $this->createIntlFormatter($pattern); $formatter = $this->createIntlFormatter($pattern);
$this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($expected, $formatter->format($timestamp));
$this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code()); $this->assertSame($errorCode, intl_get_error_code());
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); $this->assertFalse(intl_is_failure(intl_get_error_code()));
} }
public function formatProvider() public function formatProvider()
@ -275,7 +295,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
); );
// As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
if (version_compare(\PHP_VERSION, '5.3.4', '>=')) { if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
$dateTime = new \DateTime('@0'); $dateTime = new \DateTime('@0');
/* general, DateTime */ /* general, DateTime */
@ -290,13 +310,51 @@ class StubIntlDateFormatterTest extends LocaleTestCase
return $formatData; return $formatData;
} }
/**
* @dataProvider formatErrorProvider
*/
public function testFormatIllegalArgumentErrorStub($pattern, $timestamp, $errorMessage)
{
$errorCode = StubIntl::U_ILLEGAL_ARGUMENT_ERROR;
$formatter = $this->createStubFormatter($pattern);
$this->assertFalse($formatter->format($timestamp));
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertTrue(StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertTrue(StubIntl::isFailure($formatter->getErrorCode()));
}
/**
* @dataProvider formatErrorProvider
*/
public function testFormatIllegalArgumentErrorIntl($pattern, $timestamp, $errorMessage)
{
$this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld();
$errorCode = StubIntl::U_ILLEGAL_ARGUMENT_ERROR;
$formatter = $this->createIntlFormatter($pattern);
$this->assertFalse($formatter->format($timestamp));
$this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code());
$this->assertTrue(intl_is_failure(intl_get_error_code()));
}
public function formatErrorProvider() public function formatErrorProvider()
{ {
/* errors */ $message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR';
if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
$message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR';
}
return array( return array(
array('y-M-d', '0', false, 1, 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'), array('y-M-d', '0', $message),
array('y-M-d', 'foobar', false, 1, 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'), array('y-M-d', 'foobar', $message),
); );
} }
@ -480,13 +538,13 @@ class StubIntlDateFormatterTest extends LocaleTestCase
public function testGetErrorCode() public function testGetErrorCode()
{ {
$formatter = $this->createStubFormatter(); $formatter = $this->createStubFormatter();
$this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR, $formatter->getErrorCode()); $this->assertEquals(StubIntl::getErrorCode(), $formatter->getErrorCode());
} }
public function testGetErrorMessage() public function testGetErrorMessage()
{ {
$formatter = $this->createStubFormatter(); $formatter = $this->createStubFormatter();
$this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage()); $this->assertEquals(StubIntl::getErrorMessage(), $formatter->getErrorMessage());
} }
public function testGetLocale() public function testGetLocale()
@ -538,7 +596,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertSame($expected, $formatter->parse($value)); $this->assertSame($expected, $formatter->parse($value));
$this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code()); $this->assertSame($errorCode, intl_get_error_code());
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); $this->assertFalse(intl_is_failure(intl_get_error_code()));
} }
/** /**
@ -553,7 +611,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertSame($expected, $formatter->parse($value)); $this->assertSame($expected, $formatter->parse($value));
$this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertFalse(StubIntl::isFailure($formatter->getErrorCode()));
} }
public function parseProvider() public function parseProvider()
@ -712,7 +773,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertFalse($formatter->parse($value)); $this->assertFalse($formatter->parse($value));
$this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code()); $this->assertSame($errorCode, intl_get_error_code());
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); $this->assertTrue(intl_is_failure(intl_get_error_code()));
} }
/** /**
@ -727,7 +788,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertFalse($formatter->parse($value)); $this->assertFalse($formatter->parse($value));
$this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertTrue(StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertTrue(StubIntl::isFailure($formatter->getErrorCode()));
} }
public function parseErrorProvider() public function parseErrorProvider()

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Locale\Tests\Stub; namespace Symfony\Component\Locale\Tests\Stub;
use Symfony\Component\Locale\Locale; use Symfony\Component\Locale\Locale;
use Symfony\Component\Locale\Stub\StubIntl;
use Symfony\Component\Locale\Stub\StubNumberFormatter; use Symfony\Component\Locale\Stub\StubNumberFormatter;
use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase; use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase;
@ -276,15 +277,36 @@ class StubNumberFormatterTest extends LocaleTestCase
public function testFormatStub() public function testFormatStub()
{ {
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
$formatter = $this->getStubFormatterWithDecimalStyle(); $formatter = $this->getStubFormatterWithDecimalStyle();
$this->assertSame('9.555', $formatter->format(9.555)); $this->assertSame('9.555', $formatter->format(9.555));
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertFalse(StubIntl::isFailure($formatter->getErrorCode()));
} }
public function testFormatIntl() public function testFormatIntl()
{ {
$this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfIntlExtensionIsNotLoaded();
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
$formatter = $this->getIntlFormatterWithDecimalStyle(); $formatter = $this->getIntlFormatterWithDecimalStyle();
$this->assertSame('9.555', $formatter->format(9.555)); $this->assertSame('9.555', $formatter->format(9.555));
$this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code());
$this->assertFalse(intl_is_failure(intl_get_error_code()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertFalse(intl_is_failure($formatter->getErrorCode()));
} }
/** /**
@ -665,7 +687,7 @@ class StubNumberFormatterTest extends LocaleTestCase
public function testGetErrorCode() public function testGetErrorCode()
{ {
$formatter = $this->getStubFormatterWithDecimalStyle(); $formatter = $this->getStubFormatterWithDecimalStyle();
$this->assertEquals(StubNumberFormatter::U_ZERO_ERROR, $formatter->getErrorCode()); $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
} }
public function testGetLocale() public function testGetLocale()
@ -720,10 +742,19 @@ class StubNumberFormatterTest extends LocaleTestCase
$this->assertSame($expected, $parsedValue, $message); $this->assertSame($expected, $parsedValue, $message);
if ($expected === false) { if ($expected === false) {
$this->assertSame($formatter::U_PARSE_ERROR, $formatter->getErrorCode()); $errorCode = StubIntl::U_PARSE_ERROR;
$errorMessage = 'Number parsing failed: U_PARSE_ERROR';
} else { } else {
$this->assertEquals($formatter::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()));
} }
/** /**
@ -733,15 +764,25 @@ class StubNumberFormatterTest extends LocaleTestCase
{ {
$this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld(); $this->skipIfICUVersionIsTooOld();
$formatter = $this->getIntlFormatterWithDecimalStyle(); $formatter = $this->getIntlFormatterWithDecimalStyle();
$parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE); $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
$this->assertSame($expected, $parsedValue, $message); $this->assertSame($expected, $parsedValue, $message);
if ($expected === false) { if ($expected === false) {
$this->assertSame(U_PARSE_ERROR, $formatter->getErrorCode()); $errorCode = StubIntl::U_PARSE_ERROR;
$errorMessage = 'Number parsing failed: U_PARSE_ERROR';
} else { } 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() public function parseProvider()

View File

@ -58,6 +58,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
} }
} }
protected function isGreaterOrEqualThanPhpVersion($version)
{
return version_compare(\PHP_VERSION, $version, '>=');
}
protected function isGreaterOrEqualThanIcuVersion($version) protected function isGreaterOrEqualThanIcuVersion($version)
{ {
$version = $this->normalizeIcuVersion($version); $version = $this->normalizeIcuVersion($version);

View File

@ -37,7 +37,7 @@ class StaticMethodLoader implements LoaderInterface
throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->getName(), $this->methodName)); throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->getName(), $this->methodName));
} }
if ($reflMethod->getDeclaringClass()->getName() != $reflClass->getName()) { if ($reflClass->isAbstract() || $reflMethod->getDeclaringClass()->getName() != $reflClass->getName()) {
return false; return false;
} }

View File

@ -33,6 +33,14 @@ class StaticMethodLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($loader->loadClassMetadata($metadata)); $this->assertFalse($loader->loadClassMetadata($metadata));
} }
public function testLoadClassMetadataReturnsFalseIfMethodIsAbstract()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\AbstractStaticLoaderEntity');
$this->assertFalse($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadata() public function testLoadClassMetadata()
{ {
$loader = new StaticMethodLoader('loadMetadata'); $loader = new StaticMethodLoader('loadMetadata');
@ -78,3 +86,8 @@ class BaseStaticLoaderDocument
$metadata->addConstraint(new ConstraintA()); $metadata->addConstraint(new ConstraintA());
} }
} }
abstract class AbstractStaticLoaderEntity
{
abstract public static function loadMetadata(ClassMetadata $metadata);
}

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Locale\Stub;
require_once __DIR__.'/../TestCase.php';
use Symfony\Component\Locale\Locale;
use Symfony\Component\Locale\Stub\StubIntl;
use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
class StubIntlTest extends LocaleTestCase
{
public function codeProvider()
{
return array (
array(-129, '[BOGUS UErrorCode]'),
array(0, 'U_ZERO_ERROR'),
array(1, 'U_ILLEGAL_ARGUMENT_ERROR'),
array(9, 'U_PARSE_ERROR'),
array(129, '[BOGUS UErrorCode]'),
);
}
/**
* @dataProvider codeProvider
*/
public function testGetErrorName($code, $name)
{
$this->assertSame($name, StubIntl::getErrorName($code));
}
/**
* @dataProvider codeProvider
*/
public function testGetErrorNameWithIntl($code, $name)
{
$this->skipIfIntlExtensionIsNotLoaded();
$this->assertSame(intl_error_name($code), StubIntl::getErrorName($code));
}
}