Explorar o código

[Locale] support standard date and time types if no format is set, refactoring and more tests

Igor Wiedler %!s(int64=14) %!d(string=hai) anos
pai
achega
c7380fbb4c

+ 47 - 0
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -29,7 +29,25 @@ class StubIntlDateFormatter
     const TRADITIONAL = 0;
     const GREGORIAN = 1;
 
+    private $defaultDateFormats = array(
+        self::NONE      => '',
+        self::FULL      => 'EEEE, LLLL d, y',
+        self::LONG      => 'LLLL d, y',
+        self::MEDIUM    => 'LLL d, y',
+        self::SHORT     => 'M/d/yy',
+    );
+
+    private $defaultTimeFormats = array(
+        self::FULL      => 'h:mm:ss a zzzz',
+        self::LONG      => 'h:mm:ss a z',
+        self::MEDIUM    => 'h:mm:ss a',
+        self::SHORT     => 'h:mm a',
+    );
+
+    private $datetype;
+    private $timetype;
     private $pattern;
+
     private $dateTimeZone;
 
     public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
@@ -38,6 +56,9 @@ class StubIntlDateFormatter
             throw new \InvalidArgumentException('Unsupported $locale value. Only the \'en\' locale is supported. Install the intl extension for full localization capabilities.');
         }
 
+        $this->datetype = $datetype;
+        $this->timetype = $timetype;
+
         $this->setPattern($pattern);
 
         try {
@@ -196,21 +217,47 @@ class StubIntlDateFormatter
         return $formatted;
     }
 
+    public function getLocale()
+    {
+        return 'en';
+    }
+
     public function getPattern()
     {
         return $this->pattern;
     }
 
     public function getCalendar()
+    {
+        return self::GREGORIAN;
+    }
+
+    public function setLocale($locale)
     {
         $this->throwMethodNotImplementException(__METHOD__);
     }
 
     public function setPattern($pattern)
     {
+        if (null === $pattern) {
+            $patternParts = array();
+            if (self::NONE !== $this->datetype) {
+                $patternParts[] = $this->defaultDateFormats[$this->datetype];
+            }
+            if (self::NONE !== $this->timetype) {
+                $patternParts[] = $this->defaultTimeFormats[$this->timetype];
+            }
+            $pattern = implode(' ', $patternParts);
+        }
+
         $this->pattern = $pattern;
     }
 
+    public function setCalendar()
+    {
+        $this->throwMethodNotImplementException(__METHOD__);
+    }
+
     private function throwMethodNotImplementException($methodName)
     {
         $message = sprintf('The %s::%s() is not implemented. Install the intl extension for full localization capabilities.', __CLASS__, $methodName);

+ 68 - 33
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -16,6 +16,34 @@ use Symfony\Component\Locale\Stub\StubIntlDateFormatter;
 
 class StubIntlDateFormatterTest extends \PHPUnit_Framework_TestCase
 {
+    /**
+     * @expectedException InvalidArgumentException
+     */
+    public function testConstructorWithUnsupportedLocale()
+    {
+        $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
+    }
+
+    public function testConstructor()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
+        $this->assertEquals('Y-M-d', $formatter->getPattern());
+    }
+
+    /**
+    * @dataProvider formatProvider
+    */
+    public function testFormat($pattern, $timestamp, $expected)
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
+        $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
+
+        if (extension_loaded('intl')) {
+            $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
+            $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
+        }
+    }
+
     public function formatProvider()
     {
         return array(
@@ -198,6 +226,22 @@ class StubIntlDateFormatterTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+    /**
+    * @dataProvider formatWithTimezoneProvider
+    */
+    public function testFormatWithTimezone($timestamp, $timezone, $expected)
+    {
+        $pattern = 'yyyy-MM-dd HH:mm:ss';
+
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
+        $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
+
+        if (extension_loaded('intl')) {
+            $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
+            $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
+        }
+    }
+
     public function formatWithTimezoneProvider()
     {
         return array(
@@ -226,47 +270,32 @@ class StubIntlDateFormatterTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException InvalidArgumentException
-     */
-    public function testConstructorWithUnsupportedLocale()
-    {
-        $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
-    }
-
-    public function testConstructor()
-    {
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
-        $this->assertEquals('Y-M-d', $formatter->getPattern());
-    }
-
-    /**
-    * @dataProvider formatProvider
+    * @dataProvider defaultDateFormatsProvider
     */
-    public function testFormat($pattern, $timestamp, $expected)
+    public function testDefaultDateFormats($timestamp, $datetype, $timetype, $expected)
     {
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
+        $formatter = new StubIntlDateFormatter('en', $datetype, $timetype, 'UTC');
         $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
 
         if (extension_loaded('intl')) {
-            $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
+            $formatter = new \IntlDateFormatter('en', $datetype, $timetype, 'UTC');
             $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
         }
     }
 
-    /**
-    * @dataProvider formatWithTimezoneProvider
-    */
-    public function testFormatWithTimezone($timestamp, $timezone, $expected)
+    public function defaultDateFormatsProvider()
     {
-        $pattern = 'yyyy-MM-dd HH:mm:ss';
-
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
-        $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
-
-        if (extension_loaded('intl')) {
-            $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
-            $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
-        }
+        return array(
+            array(0, StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'Thursday, January 1, 1970'),
+            array(0, StubIntlDateFormatter::LONG, StubIntlDateFormatter::NONE, 'January 1, 1970'),
+            array(0, StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::NONE, 'Jan 1, 1970'),
+            array(0, StubIntlDateFormatter::SHORT, StubIntlDateFormatter::NONE, '1/1/70'),
+
+            array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT+00:00'),
+            array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT+00:00'),
+            array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'),
+            array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'),
+        );
     }
 
     /**
@@ -274,7 +303,13 @@ class StubIntlDateFormatterTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetCalendar()
     {
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC');
-        $formatter->getCalendar();
+        $formatter = new StubIntlDateFormatter('en');
+        $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
+    }
+
+    public function testSetCalendar()
+    {
+        $formatter = new StubIntlDateFormatter('en');
+        $this->assertEquals(StubIntlDateFormatter::GREGORIAN, $formatter->getCalendar());
     }
 }