Bladeren bron

[Locale] add remaining IntlDateFormatter methods with tests

Igor Wiedler 14 jaren geleden
bovenliggende
commit
1191ab4b56

+ 55 - 6
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -60,12 +60,7 @@ class StubIntlDateFormatter
         $this->timetype = $timetype;
 
         $this->setPattern($pattern);
-
-        try {
-            $this->dateTimeZone = new \DateTimeZone($timezone);
-        } catch (\Exception $e) {
-            $this->dateTimeZone = new \DateTimeZone('UTC');
-        }
+        $this->setTimezoneId($timezone);
     }
 
     public function format($timestamp)
@@ -260,4 +255,58 @@ class StubIntlDateFormatter
     {
         throw new MethodNotImplementedException(__METHOD__);
     }
+
+    public function getDateType()
+    {
+        return $this->datetype;
+    }
+
+    public function getTimeType()
+    {
+        return $this->timetype;
+    }
+
+    public function getErrorCode()
+    {
+        throw new MethodNotImplementedException(__METHOD__);
+    }
+
+    public function getErrorMessage()
+    {
+        throw new MethodNotImplementedException(__METHOD__);
+    }
+
+    public function getTimezoneId()
+    {
+        return $this->dateTimeZone->getName();
+    }
+
+    public function setTimezoneId($timezoneId)
+    {
+        try {
+            $this->dateTimeZone = new \DateTimeZone($timezoneId);
+        } catch (\Exception $e) {
+            $this->dateTimeZone = new \DateTimeZone('UTC');
+        }
+    }
+
+    public function isLenient()
+    {
+        throw new MethodNotImplementedException(__METHOD__);
+    }
+
+    public function localtime($value, &$position = 0)
+    {
+        throw new MethodNotImplementedException(__METHOD__);
+    }
+
+    public function parse($value, &$position = 0)
+    {
+        throw new MethodNotImplementedException(__METHOD__);
+    }
+
+    static public function create($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
+    {
+        return new self($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
+    }
 }

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

@@ -344,4 +344,105 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
         $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
     }
+
+    public function testGetDateType()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getDateType());
+    }
+
+    public function testGetTimeType()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL);
+        $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getTimeType());
+    }
+
+    /**
+     * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
+     */
+    public function testGetErrorCode()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $formatter->getErrorCode();
+    }
+
+    /**
+     * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
+     */
+    public function testGetErrorMessage()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $formatter->getErrorMessage();
+    }
+
+    /**
+    * @dataProvider timezoneIdProvider
+    */
+    public function testGetTimezoneId($timezoneId)
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, $timezoneId);
+        $this->assertEquals($timezoneId, $formatter->getTimezoneId(), 'Check timezone id with stub implementation.');
+
+        if ($this->isIntlExtensionLoaded()) {
+            $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, $timezoneId);
+            $this->assertEquals($timezoneId, $formatter->getTimezoneId(), 'Check timezone id with intl extension.');
+        }
+    }
+
+    public function timezoneIdProvider()
+    {
+        return array(
+            array('Europe/Zurich'),
+            array('Asia/Dubai'),
+        );
+    }
+
+    public function testSetTimezoneId()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'UTC');
+        $this->assertEquals('UTC', $formatter->getTimezoneId(), 'Check timezone id with stub implementation.');
+
+        $formatter->setTimezoneId('Europe/Zurich');
+        $this->assertEquals('Europe/Zurich', $formatter->getTimezoneId(), 'Check timezone id with intl extension.');
+
+        if ($this->isIntlExtensionLoaded()) {
+            $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'UTC');
+
+            $formatter->setTimezoneId('Europe/Zurich');
+            $this->assertEquals('Europe/Zurich', $formatter->getTimezoneId(), 'Check timezone id with intl extension.');
+        }
+    }
+
+    /**
+     * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
+     */
+    public function testLocaltime()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $formatter->localtime('Wednesday, December 31, 1969 4:00:00 PM PT');
+    }
+
+    /**
+     * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
+     */
+    public function testParse()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $formatter->parse('Wednesday, December 31, 1969 4:00:00 PM PT');
+    }
+
+    /**
+     * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
+     */
+    public function testIsLenient()
+    {
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $formatter->isLenient();
+    }
+
+    public function testStaticCreate()
+    {
+        $formatter = StubIntlDateFormatter::create('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
+        $this->assertInstanceOf('Symfony\Component\Locale\Stub\StubIntlDateFormatter', $formatter);
+    }
 }