Преглед на файлове

[Locale] added tests to document behavior not implemented by StubIntlDateFormatter::parse()

Eriksen Costa преди 14 години
родител
ревизия
d8e35a2f5c

+ 1 - 4
src/Symfony/Component/Locale/Stub/DateFormat/DayOfWeekTransformer.php

@@ -39,7 +39,6 @@ class DayOfWeekTransformer extends Transformer
      */
     public function getReverseMatchingRegExp($length)
     {
-        $dayOfWeek = $dateTime->format('l');
         switch ($length) {
             case 4:
                 return 'Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday';
@@ -55,8 +54,6 @@ class DayOfWeekTransformer extends Transformer
      */
     public function extractDateOptions($matched, $length)
     {
-        return array(
-            'hour' => (int) $matched,
-        );
+        return array();
     }
 }

+ 2 - 4
src/Symfony/Component/Locale/Stub/DateFormat/DayOfYearTransformer.php

@@ -32,7 +32,7 @@ class DayOfYearTransformer extends Transformer
      */
     public function getReverseMatchingRegExp($length)
     {
-        return "\d{$length}";
+        return '\d{'.$length.'}';
     }
 
     /**
@@ -40,8 +40,6 @@ class DayOfYearTransformer extends Transformer
      */
     public function extractDateOptions($matched, $length)
     {
-        return array(
-            'hour' => (int) $matched,
-        );
+        return array();
     }
 }

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/QuarterTransformer.php

@@ -45,7 +45,7 @@ class QuarterTransformer extends Transformer
         switch ($length) {
             case 1:
             case 2:
-                return "\d{$length}";
+                return '\d{'.$length.'}';
             case 3:
                 return 'Q\d';
             default:

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

@@ -630,6 +630,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase
             // a previous timezoned parsing should not change the timezone for the next parsing
             array('y-M-d HH:mm:ss', '1970-1-1 00:00:00', 0),
 
+            // AM/PM (already covered by hours tests)
+            array('y-M-d HH:mm:ss a', '1970-1-1 00:00:00 AM', 0),
+            array('y-M-d HH:mm:ss a', '1970-1-1 00:00:00 PM', 43200),
+
             // regExp metachars in the pattern string
             array('y[M-d', '1970[1-1', 0),
             array('y[M/d', '1970[1/1', 0),
@@ -643,6 +647,72 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         );
     }
 
+    /**
+     * Just to document the differencies between the stub and the intl implementations. The intl can parse
+     * any of the tested formats alone. The stub does not implement them as it would be needed to add more
+     * abstraction, passing more context to the transformers objects. Any of the formats are ignored alone
+     * or with date/time data (years, months, days, hours, minutes and seconds).
+     *
+     * Also in intl, format like 'ss E' for '10 2' (2nd day of year + 10 seconds) are added, then we have
+     * 86,400 seconds (24h * 60min * 60s) + 10 seconds
+     *
+     * @dataProvider parseDifferencies()
+     */
+    public function testParseDifferenciesStub($pattern, $value, $stubExpected, $intlExpected)
+    {
+        $formatter = $this->createStubFormatter($pattern);
+        $this->assertSame($stubExpected, $formatter->parse($value));
+    }
+
+    /**
+     * @dataProvider parseDifferencies()
+     */
+    public function testParseDifferenciesIntl($pattern, $value, $stubExpected, $intlExpected)
+    {
+        $formatter = $this->createIntlFormatter($pattern);
+        $this->assertSame($intlExpected, $formatter->parse($value));
+    }
+
+    public function parseDifferencies()
+    {
+        return array(
+            // AM/PM, ignored if alone
+            array('a', 'AM', 0, 0),
+            array('a', 'PM', 0, 43200),
+
+            // day of week
+            array('E', 'Thu', 0, 0),
+            array('EE', 'Thu', 0, 0),
+            array('EEE', 'Thu', 0, 0),
+            array('EEEE', 'Thursday', 0, 0),
+            array('EEEEE', 'T', 0, 432000),
+            array('EEEEEE', 'Thu', 0, 0),
+
+            // day of year
+            array('D', '1', 0, 0),
+            array('D', '2', 0, 86400),
+
+            // quarter
+            array('Q', '1', 0, 0),
+            array('QQ', '01', 0, 0),
+            array('QQQ', 'Q1', 0, 0),
+            array('QQQQ', '1st quarter', 0, 0),
+            array('QQQQQ', '1st quarter', 0, 0),
+
+            array('Q', '2', 0, 7776000),
+            array('QQ', '02', 0, 7776000),
+            array('QQQ', 'Q2', 0, 7776000),
+            array('QQQQ', '2nd quarter', 0, 7776000),
+            array('QQQQQ', '2nd quarter', 0, 7776000),
+
+            array('q', '1', 0, 0),
+            array('qq', '01', 0, 0),
+            array('qqq', 'Q1', 0, 0),
+            array('qqqq', '1st quarter', 0, 0),
+            array('qqqqq', '1st quarter', 0, 0),
+        );
+    }
+
     public function testParseWithNullPositionValueStub()
     {
         $position = null;