diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index 7985c3000f..35b59853b0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -180,7 +180,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase $loader->load('badtag1.yml'); $this->fail('->load() should throw an exception when the tags key of a service is not an array'); } 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'); } } @@ -192,7 +192,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase $loader->load('badtag2.yml'); $this->fail('->load() should throw an exception when a tag is missing the name key'); } 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'); } } diff --git a/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php b/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php index 770f4181f6..2fdb6feaed 100644 --- a/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php +++ b/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php @@ -33,7 +33,7 @@ class MethodArgumentValueNotImplementedException extends NotImplementedException $methodName, $argName, var_export($argValue, true), - $additionalMessage != '' ? ' '.$additionalMessage.'. ' : '' + $additionalMessage !== '' ? ' '.$additionalMessage.'. ' : '' ); parent::__construct($message); diff --git a/src/Symfony/Component/Locale/Resources/stubs/functions.php b/src/Symfony/Component/Locale/Resources/stubs/functions.php index 100f6fb99d..2444c3735a 100644 --- a/src/Symfony/Component/Locale/Resources/stubs/functions.php +++ b/src/Symfony/Component/Locale/Resources/stubs/functions.php @@ -45,3 +45,14 @@ function intl_get_error_code() { function intl_get_error_message() { 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); +} \ No newline at end of file diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php index 5cb50e76a9..e06e0431ec 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php +++ b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php @@ -155,7 +155,7 @@ class FullTransformer } // behave like the intl extension - StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); + StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed'); return false; } @@ -292,7 +292,7 @@ class FullTransformer // If month is false, return immediately (intl behavior) if (false === $month) { - StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); + StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed'); return false; } diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php index d48e0a84b4..f817ba0d21 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php +++ b/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php @@ -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.'); diff --git a/src/Symfony/Component/Locale/Stub/StubCollator.php b/src/Symfony/Component/Locale/Stub/StubCollator.php index a6bd4ddb7d..73b1d76096 100644 --- a/src/Symfony/Component/Locale/Stub/StubCollator.php +++ b/src/Symfony/Component/Locale/Stub/StubCollator.php @@ -21,16 +21,6 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio */ 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 */ const FRENCH_COLLATION = 0; const ALTERNATE_HANDLING = 1; @@ -158,7 +148,7 @@ class StubCollator */ public function getErrorCode() { - return self::U_ZERO_ERROR; + return StubIntl::U_ZERO_ERROR; } /** @@ -168,7 +158,7 @@ class StubCollator */ public function getErrorMessage() { - return self::U_ZERO_ERROR_MESSAGE; + return 'U_ZERO_ERROR'; } /** diff --git a/src/Symfony/Component/Locale/Stub/StubIntl.php b/src/Symfony/Component/Locale/Stub/StubIntl.php index bb00ade862..8c8648027b 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntl.php +++ b/src/Symfony/Component/Locale/Stub/StubIntl.php @@ -45,20 +45,9 @@ abstract class StubIntl * @var 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_ILLEGAL_ARGUMENT_ERROR => 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR', - self::U_PARSE_ERROR => 'Date parsing failed: U_PARSE_ERROR', + self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR', + self::U_PARSE_ERROR => 'U_PARSE_ERROR', ); /** @@ -68,6 +57,13 @@ abstract class StubIntl */ 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 * @@ -77,8 +73,8 @@ abstract class StubIntl */ static public function isFailure($errorCode) { - return in_array($errorCode, self::$errorCodes, true) - && $errorCode !== self::U_ZERO_ERROR; + return isset(self::$errorCodes[$errorCode]) + && $errorCode > self::U_ZERO_ERROR; } /** @@ -102,22 +98,38 @@ abstract class StubIntl */ 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 */ - 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)); } + self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code]; self::$errorCode = $code; } } diff --git a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php index edc952094f..ca21c9e82c 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php @@ -26,14 +26,18 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio class StubIntlDateFormatter { /** - * 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 + * The error code from the last operation * - * @see StubIntlDateFormatter::getErrorCode() - * @see StubIntlDateFormatter::getErrorMessage() + * @var integer */ - const U_ZERO_ERROR = 0; - const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR'; + protected $errorCode = StubIntl::U_ZERO_ERROR; + + /** + * The error message from the last operation + * + * @var string + */ + protected $errorMessage = 'U_ZERO_ERROR'; /* date/time format types */ const NONE = -1; @@ -120,11 +124,11 @@ class StubIntlDateFormatter */ 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'); } - if (self::GREGORIAN != $calendar) { + if (self::GREGORIAN !== $calendar) { 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 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 - if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) { - StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR); + $argumentError = null; + 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; } // 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(); } $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); $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; } @@ -225,7 +247,7 @@ class StubIntlDateFormatter */ public function getErrorCode() { - return self::U_ZERO_ERROR; + return $this->errorCode; } /** @@ -237,7 +259,7 @@ class StubIntlDateFormatter */ public function getErrorMessage() { - return self::U_ZERO_ERROR_MESSAGE; + return $this->errorMessage; } /** @@ -350,12 +372,18 @@ class StubIntlDateFormatter throw new MethodArgumentNotImplementedException(__METHOD__, 'position'); } - StubIntl::setErrorCode(StubIntl::U_ZERO_ERROR); - $dateTime = $this->createDateTime(0); $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; } /** diff --git a/src/Symfony/Component/Locale/Stub/StubLocale.php b/src/Symfony/Component/Locale/Stub/StubLocale.php index f4c3852e6d..831960f21b 100644 --- a/src/Symfony/Component/Locale/Stub/StubLocale.php +++ b/src/Symfony/Component/Locale/Stub/StubLocale.php @@ -480,7 +480,7 @@ class StubLocale */ 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)); } diff --git a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php index d7accea3cc..08df95e173 100644 --- a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php @@ -24,33 +24,6 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio */ 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 */ const PATTERN_DECIMAL = 0; const DECIMAL = 1; @@ -138,6 +111,30 @@ class StubNumberFormatter const PAD_BEFORE_SUFFIX = 2; 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 * @@ -224,16 +221,6 @@ class StubNumberFormatter 'negative' => -9223372036854775808 ); - /** - * @var string - */ - private $locale = null; - - /** - * @var int - */ - private $style = null; - /** * Constructor * @@ -364,8 +351,12 @@ class StubNumberFormatter $fractionDigits = $this->getAttribute(self::FRACTION_DIGITS); $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() { - 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 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; } // 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 + $this->resetError(); + + return $value; } /** @@ -623,6 +620,16 @@ class StubNumberFormatter 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 * diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubCollatorTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubCollatorTest.php index c0b320110e..e4391d07f3 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubCollatorTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubCollatorTest.php @@ -13,7 +13,8 @@ namespace Symfony\Component\Locale\Tests\Stub; use Symfony\Component\Locale\Locale; 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 { @@ -89,13 +90,13 @@ class StubCollatorTest extends LocaleTestCase public function testGetErrorCode() { $collator = $this->createStubCollator(); - $this->assertEquals(StubCollator::U_ZERO_ERROR, $collator->getErrorCode()); + $this->assertEquals(StubIntl::U_ZERO_ERROR, $collator->getErrorCode()); } public function testGetErrorMessage() { $collator = $this->createStubCollator(); - $this->assertEquals(StubCollator::U_ZERO_ERROR_MESSAGE, $collator->getErrorMessage()); + $this->assertEquals('U_ZERO_ERROR', $collator->getErrorMessage()); } public function testGetLocale() diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php index 325997575a..446ecc640f 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php @@ -51,49 +51,69 @@ class StubIntlDateFormatterTest extends LocaleTestCase $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 */ - 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); $this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $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 */ - public function testFormatIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') - { - $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') + public function testFormatIntl($pattern, $timestamp, $expected) { $this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfICUVersionIsTooOld(); - if (version_compare(PHP_VERSION, '5.3.3') > 0) { - $this->markTestSkipped('The intl error messages were change in PHP 5.3.3.'); - } + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; $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())); + $this->assertFalse(intl_is_failure(intl_get_error_code())); } public function formatProvider() @@ -275,7 +295,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase ); // 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'); /* general, DateTime */ @@ -290,13 +310,51 @@ class StubIntlDateFormatterTest extends LocaleTestCase 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() { - /* 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( - 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', 'foobar', 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', $message), ); } @@ -480,13 +538,13 @@ class StubIntlDateFormatterTest extends LocaleTestCase public function testGetErrorCode() { $formatter = $this->createStubFormatter(); - $this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR, $formatter->getErrorCode()); + $this->assertEquals(StubIntl::getErrorCode(), $formatter->getErrorCode()); } public function testGetErrorMessage() { $formatter = $this->createStubFormatter(); - $this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage()); + $this->assertEquals(StubIntl::getErrorMessage(), $formatter->getErrorMessage()); } public function testGetLocale() @@ -538,7 +596,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($expected, $formatter->parse($value)); $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->assertFalse(intl_is_failure(intl_get_error_code())); } /** @@ -553,7 +611,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($expected, $formatter->parse($value)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $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() @@ -712,7 +773,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertFalse($formatter->parse($value)); $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->assertTrue(intl_is_failure(intl_get_error_code())); } /** @@ -727,7 +788,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertFalse($formatter->parse($value)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $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() diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php index 847b3bec72..dc73eb2b17 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Locale\Tests\Stub; use Symfony\Component\Locale\Locale; +use Symfony\Component\Locale\Stub\StubIntl; use Symfony\Component\Locale\Stub\StubNumberFormatter; use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase; @@ -276,15 +277,36 @@ class StubNumberFormatterTest extends LocaleTestCase public function testFormatStub() { + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; + $formatter = $this->getStubFormatterWithDecimalStyle(); $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() { $this->skipIfIntlExtensionIsNotLoaded(); + + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; + $formatter = $this->getIntlFormatterWithDecimalStyle(); $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() { $formatter = $this->getStubFormatterWithDecimalStyle(); - $this->assertEquals(StubNumberFormatter::U_ZERO_ERROR, $formatter->getErrorCode()); + $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode()); } public function testGetLocale() @@ -720,10 +742,19 @@ class StubNumberFormatterTest extends LocaleTestCase $this->assertSame($expected, $parsedValue, $message); if ($expected === false) { - $this->assertSame($formatter::U_PARSE_ERROR, $formatter->getErrorCode()); + $errorCode = StubIntl::U_PARSE_ERROR; + $errorMessage = 'Number parsing failed: U_PARSE_ERROR'; } 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->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() diff --git a/src/Symfony/Component/Locale/Tests/TestCase.php b/src/Symfony/Component/Locale/Tests/TestCase.php index 2914c0fc2f..634b7deda9 100644 --- a/src/Symfony/Component/Locale/Tests/TestCase.php +++ b/src/Symfony/Component/Locale/Tests/TestCase.php @@ -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) { $version = $this->normalizeIcuVersion($version); diff --git a/src/Symfony/Component/Validator/Mapping/Loader/StaticMethodLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/StaticMethodLoader.php index a74ffbb429..3e90fe91b5 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/StaticMethodLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/StaticMethodLoader.php @@ -37,7 +37,7 @@ class StaticMethodLoader implements LoaderInterface 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; } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php index 5ddb90dbfc..0f88936227 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php @@ -33,6 +33,14 @@ class StaticMethodLoaderTest extends \PHPUnit_Framework_TestCase $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() { $loader = new StaticMethodLoader('loadMetadata'); @@ -78,3 +86,8 @@ class BaseStaticLoaderDocument $metadata->addConstraint(new ConstraintA()); } } + +abstract class AbstractStaticLoaderEntity +{ + abstract public static function loadMetadata(ClassMetadata $metadata); +} diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlTest.php new file mode 100644 index 0000000000..a8c8e07094 --- /dev/null +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlTest.php @@ -0,0 +1,49 @@ + + * + * 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)); + } +}