Przeglądaj źródła

[Locale] throw exception if value different than null is provided (behavior not implemented in the stub)

Eriksen Costa 14 lat temu
rodzic
commit
4cc5b6f2d0

+ 14 - 7
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -15,6 +15,7 @@ use Symfony\Component\Locale\Stub\StubLocale;
 use Symfony\Component\Locale\Stub\DateFormat\FullTransformer;
 use Symfony\Component\Locale\Exception\NotImplementedException;
 use Symfony\Component\Locale\Exception\MethodNotImplementedException;
+use Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException;
 use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException;
 
 /**
@@ -242,15 +243,21 @@ class StubIntlDateFormatter
     /**
      * Parse string to a timestamp value
      *
-     * @param  string   $value      String to convert to a time value
-     * @param  int      $position   Position at which to start the parsing in $value (zero-based).
-     *                              If no error occurs before $value is consumed, $parse_pos will
-     *                              contain -1 otherwise it will contain the position at which parsing
-     *                              ended. If $parse_pos > strlen($value), the parse fails immediately.
-     * @return string               Parsed value as a timestamp
+     * @param  string   $value                        String to convert to a time value
+     * @param  int      $position                     Position at which to start the parsing in $value (zero-based).
+     *                                                If no error occurs before $value is consumed, $parse_pos will
+     *                                                contain -1 otherwise it will contain the position at which parsing
+     *                                                ended. If $parse_pos > strlen($value), the parse fails immediately.
+     * @return string                                 Parsed value as a timestamp
+     * @throws MethodArgumentNotImplementedException  When $position different than null, behavior not implemented
      */
-    public function parse($value, &$position = 0)
+    public function parse($value, &$position = null)
     {
+        // We don't calculate the position when parsing the value
+        if (!is_null($position)) {
+            throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
+        }
+
         $dateTime = $this->createDateTime(0);
         $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
         return $transformer->parse($dateTime, $value);

+ 19 - 0
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -426,6 +426,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
      */
     public function testParseIntl($pattern, $value, $expected)
     {
+        $this->skipIfIntlExtensionIsNotLoaded();
         $formatter = $this->createIntlFormatter($pattern);
         $this->assertSame($expected, $formatter->parse($value));
     }
@@ -543,6 +544,24 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         );
     }
 
+    public function testParseWithNullPositionValueStub()
+    {
+        $position = null;
+        $formatter = $this->createStubFormatter('y');
+        $this->assertSame(0, $formatter->parse('1970', $position));
+        $this->assertNull($position);
+    }
+
+    /**
+     * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
+     */
+    public function testParseWithNotNullPositionValueStub()
+    {
+        $position = 0;
+        $formatter = $this->createStubFormatter('y');
+        $this->assertSame(0, $formatter->parse('1970', $position));
+    }
+
     /**
      * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
      */