Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/192531

Change subject: More refactoring in HtmlTimeFormatter
......................................................................

More refactoring in HtmlTimeFormatter

* Use full message keys instead of concatenating them. Relevant when
  searching for them.
* Remove dependency on core's Language object, just use the language code
  instead.
* Remove duplicate code from constructor, call parent constructor instead.
* Also remove the options from the test case provider. The class does not
  use any option except for the language. Which is set-up in the test now,
  not part of the individual test cases.

Change-Id: I83f3ec0c4afb5c136f227be2ee37a10db716a288
---
M lib/includes/formatters/HtmlTimeFormatter.php
M lib/tests/phpunit/formatters/HtmlTimeFormatterTest.php
M lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
3 files changed, 32 insertions(+), 44 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/31/192531/1

diff --git a/lib/includes/formatters/HtmlTimeFormatter.php 
b/lib/includes/formatters/HtmlTimeFormatter.php
index 524322e..8c99db6 100644
--- a/lib/includes/formatters/HtmlTimeFormatter.php
+++ b/lib/includes/formatters/HtmlTimeFormatter.php
@@ -4,7 +4,6 @@
 
 use DataValues\TimeValue;
 use InvalidArgumentException;
-use Language;
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\TimeFormatter;
 use ValueFormatters\ValueFormatter;
@@ -20,14 +19,9 @@
 class HtmlTimeFormatter extends ValueFormatterBase {
 
        private static $calendarKeys = array(
-               TimeFormatter::CALENDAR_GREGORIAN => 'gregorian',
-               TimeFormatter::CALENDAR_JULIAN => 'julian',
+               TimeFormatter::CALENDAR_GREGORIAN => 
'valueview-expert-timevalue-calendar-gregorian',
+               TimeFormatter::CALENDAR_JULIAN => 
'valueview-expert-timevalue-calendar-julian',
        );
-
-       /**
-        * @var Language
-        */
-       private $language;
 
        /**
         * @var ValueFormatter
@@ -39,15 +33,9 @@
         * @param ValueFormatter $dateTimeFormatter
         */
        public function __construct( FormatterOptions $options, ValueFormatter 
$dateTimeFormatter ) {
+               parent::__construct( $options );
+
                $this->dateTimeFormatter = $dateTimeFormatter;
-
-               $this->options = $options;
-
-               $this->options->defaultOption( ValueFormatter::OPT_LANG, 'en' );
-
-               $this->language = Language::factory(
-                       $this->options->getOption( ValueFormatter::OPT_LANG )
-               );
        }
 
        /**
@@ -101,8 +89,9 @@
         */
        private function formatCalendarName( $calendarModel ) {
                if ( array_key_exists( $calendarModel, self::$calendarKeys ) ) {
-                       $key = 'valueview-expert-timevalue-calendar-' . 
self::$calendarKeys[$calendarModel];
-                       $msg = wfMessage( $key )->inLanguage( $this->language );
+                       $key = self::$calendarKeys[$calendarModel];
+                       $lang = $this->getOption( ValueFormatter::OPT_LANG );
+                       $msg = wfMessage( $key )->inLanguage( $lang );
 
                        if ( $msg->exists() ) {
                                return htmlspecialchars( $msg->text() );
diff --git a/lib/tests/phpunit/formatters/HtmlTimeFormatterTest.php 
b/lib/tests/phpunit/formatters/HtmlTimeFormatterTest.php
index 54cb1ab..d230f3d 100644
--- a/lib/tests/phpunit/formatters/HtmlTimeFormatterTest.php
+++ b/lib/tests/phpunit/formatters/HtmlTimeFormatterTest.php
@@ -3,8 +3,10 @@
 namespace Wikibase\Lib\Test;
 
 use DataValues\TimeValue;
+use PHPUnit_Framework_TestCase;
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\TimeFormatter;
+use ValueFormatters\ValueFormatter;
 use Wikibase\Lib\HtmlTimeFormatter;
 
 /**
@@ -17,11 +19,15 @@
  *
  * @licence GNU GPL v2+
  * @author Adam Shorland
+ * @author Thiemo Mättig
  */
-class HtmlTimeFormatterTest extends \PHPUnit_Framework_TestCase {
+class HtmlTimeFormatterTest extends PHPUnit_Framework_TestCase {
 
-       private function getMockFormatter() {
-               $mock = $this->getMockBuilder( 
'\ValueFormatters\ValueFormatter' )
+       /**
+        * @return ValueFormatter
+        */
+       private function getDateTimeFormatter() {
+               $mock = $this->getMockBuilder( 'ValueFormatters\ValueFormatter' 
)
                        ->disableOriginalConstructor()
                        ->getMock();
                $mock->expects( $this->once() )
@@ -32,9 +38,14 @@
 
        /**
         * @dataProvider timeFormatProvider
+        * @param TimeValue $value
+        * @param string $pattern
         */
-       public function testFormat( $value, $options, $pattern ) {
-               $formatter = new HtmlTimeFormatter( $options, 
$this->getMockFormatter() );
+       public function testFormat( TimeValue $value, $pattern ) {
+               $options = new FormatterOptions( array(
+                       ValueFormatter::OPT_LANG => 'qqx',
+               ) );
+               $formatter = new HtmlTimeFormatter( $options, 
$this->getDateTimeFormatter() );
 
                $html = $formatter->format( $value );
                $this->assertRegExp( $pattern, $html );
@@ -47,7 +58,6 @@
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
                                '/^MOCKDATE$/'
                        ),
                        'a julian day in 1920' => array(
@@ -55,15 +65,13 @@
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_JULIAN ),
-                               new FormatterOptions(),
-                               '/^MOCKDATE<sup 
class="wb-calendar-name">Julian<\/sup>$/'
+                               '/^MOCKDATE<sup 
class="wb-calendar-name">\(valueview-expert-timevalue-calendar-julian\)<\/sup>$/'
                        ),
                        'a month in 1920' => array(
                                new TimeValue( '+1920-05-01T00:00:00Z',
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_MONTH,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
                                '/^MOCKDATE$/'
                        ),
                        'a gregorian day in 1520' => array(
@@ -71,31 +79,27 @@
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
-                               '/^MOCKDATE<sup 
class="wb-calendar-name">Gregorian<\/sup>$/'
+                               '/^MOCKDATE<sup 
class="wb-calendar-name">\(valueview-expert-timevalue-calendar-gregorian\)<\/sup>$/'
                        ),
                        'a julian day in 1520' => array(
                                new TimeValue( '+1520-05-01T00:00:00Z',
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_JULIAN ),
-                               new FormatterOptions(),
-                               '/^MOCKDATE<sup 
class="wb-calendar-name">Julian<\/sup>$/'
+                               '/^MOCKDATE<sup 
class="wb-calendar-name">\(valueview-expert-timevalue-calendar-julian\)<\/sup>$/'
                        ),
                        'a julian day in 1980' => array(
                                new TimeValue( '+1980-05-01T00:00:00Z',
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_JULIAN ),
-                               new FormatterOptions(),
-                               '/^MOCKDATE<sup 
class="wb-calendar-name">Julian<\/sup>$/'
+                               '/^MOCKDATE<sup 
class="wb-calendar-name">\(valueview-expert-timevalue-calendar-julian\)<\/sup>$/'
                        ),
                        '2014-10-10' => array(
                                new TimeValue( '+2014-10-10T00:00:00Z',
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
                                '/^MOCKDATE$/'
                        ),
                        '2014-10-10 with leading zeros' => array(
@@ -103,7 +107,6 @@
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
                                '/^MOCKDATE$/'
                        ),
                        'massive year' => array(
@@ -111,7 +114,6 @@
                                        1 * 60 * 60, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
                                '/^MOCKDATE$/'
                        ),
                        'negative' => array(
@@ -119,23 +121,20 @@
                                        0, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
-                               '/^MOCKDATE<sup 
class="wb-calendar-name">Gregorian<\/sup>$/'
+                               '/^MOCKDATE<sup 
class="wb-calendar-name">\(valueview-expert-timevalue-calendar-gregorian\)<\/sup>$/'
                        ),
                        '32-bit integer overflow' => array(
                                new TimeValue( '-2147483649-01-01T00:00:00Z',
                                        0, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        TimeFormatter::CALENDAR_GREGORIAN ),
-                               new FormatterOptions(),
-                               '/^MOCKDATE<sup 
class="wb-calendar-name">Gregorian<\/sup>$/'
+                               '/^MOCKDATE<sup 
class="wb-calendar-name">\(valueview-expert-timevalue-calendar-gregorian\)<\/sup>$/'
                        ),
                        'unknown calendar model' => array(
                                new TimeValue( '+2100-01-01T00:00:00Z',
                                        0, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        'Stardate' ),
-                               new FormatterOptions(),
                                '/^MOCKDATE<sup 
class="wb-calendar-name">Stardate<\/sup>$/'
                        ),
                        'HTML entities' => array(
@@ -143,7 +142,6 @@
                                        0, 0, 0,
                                        TimeValue::PRECISION_DAY,
                                        '<a>injection</a>' ),
-                               new FormatterOptions(),
                                '/^MOCKDATE<sup 
class="wb-calendar-name">&lt;a&gt;injection&lt;\/a&gt;<\/sup>$/'
                        ),
                );
diff --git a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php 
b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
index 725dc41..1cd39c8 100644
--- a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
+++ b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
@@ -28,9 +28,10 @@
         * @param string $pattern
         */
        public function testFormat( TimeValue $value, $pattern ) {
-               $formatter = new TimeDetailsFormatter( new FormatterOptions( 
array(
+               $options = new FormatterOptions( array(
                        ValueFormatter::OPT_LANG => 'qqx',
-               ) ) );
+               ) );
+               $formatter = new TimeDetailsFormatter( $options );
 
                $html = $formatter->format( $value );
                $this->assertRegExp( $pattern, $html );

-- 
To view, visit https://gerrit.wikimedia.org/r/192531
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I83f3ec0c4afb5c136f227be2ee37a10db716a288
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to