diff --git a/MathFormatter.php b/MathFormatter.php index d7c48265d..93802ef3a 100644 --- a/MathFormatter.php +++ b/MathFormatter.php @@ -2,7 +2,6 @@ use DataValues\StringValue; use ValueFormatters\Exceptions\MismatchingDataValueTypeException; -use ValueFormatters\FormattingException; use ValueFormatters\ValueFormatter; use Wikibase\Lib\SnakFormatter; @@ -17,22 +16,25 @@ use Wikibase\Lib\SnakFormatter; class MathFormatter implements ValueFormatter { + /** + * @var string One of the SnakFormatter::FORMAT_... constants. + */ private $format; - /* + /** * Loads format to distinguish the type of formatting * - * @param string $format + * @param string $format One of the SnakFormatter::FORMAT_... constants. * * @throws InvalidArgumentException */ public function __construct( $format ) { switch ( $format ) { - case ( SnakFormatter::FORMAT_HTML ): - case ( SnakFormatter::FORMAT_HTML_DIFF ): - case ( SnakFormatter::FORMAT_HTML_WIDGET ): - case ( SnakFormatter::FORMAT_WIKI ): - case ( SnakFormatter::FORMAT_PLAIN ): + case SnakFormatter::FORMAT_PLAIN: + case SnakFormatter::FORMAT_WIKI: + case SnakFormatter::FORMAT_HTML: + case SnakFormatter::FORMAT_HTML_DIFF: + case SnakFormatter::FORMAT_HTML_WIDGET: $this->format = $format; break; default: @@ -40,44 +42,40 @@ class MathFormatter implements ValueFormatter { } } - /* - * + /** * @param StringValue $value * + * @throws MismatchingDataValueTypeException * @return string - * @throws \ValueFormatters\Exceptions\MismatchingDataValueTypeException */ public function format( $value ) { if ( !( $value instanceof StringValue ) ) { throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) ); } + $tex = $value->getValue(); switch ( $this->format ) { - case ( SnakFormatter::FORMAT_PLAIN ): - return "$tex"; - case ( SnakFormatter::FORMAT_WIKI ): + case SnakFormatter::FORMAT_PLAIN: + return $tex; + case SnakFormatter::FORMAT_WIKI: return "$tex"; - case ( SnakFormatter::FORMAT_HTML ): - case ( SnakFormatter::FORMAT_HTML_WIDGET ): - case ( SnakFormatter::FORMAT_HTML_DIFF ): + default: $renderer = new MathMathML( $tex ); - if ( $renderer->checkTex() ) { - if ( $renderer->render() ) { - return $renderer->getHtmlOutput(); - } + if ( $renderer->checkTex() && $renderer->render() ) { + return $renderer->getHtmlOutput(); } + // TeX string is not valid or rendering failed return $renderer->getLastError(); } } /** - * - * @return format + * @return string One of the SnakFormatter::FORMAT_... constants. */ - public function getFormat() { return $this->format; } + } diff --git a/MathInputCheckRestbase.php b/MathInputCheckRestbase.php index 0fd2bc545..eb70d661d 100644 --- a/MathInputCheckRestbase.php +++ b/MathInputCheckRestbase.php @@ -9,8 +9,6 @@ * @author Moritz Schubotz */ -use MediaWiki\Logger\LoggerFactory; - class MathInputCheckRestbase extends MathInputCheck { private $restbaseInterface; diff --git a/tests/MathFormatterTest.php b/tests/MathFormatterTest.php index 9a499ce0a..6e19c3bb4 100644 --- a/tests/MathFormatterTest.php +++ b/tests/MathFormatterTest.php @@ -9,12 +9,8 @@ use DataValues\StringValue; use DataValues\NumberValue; class MathFormatterTest extends MediaWikiTestCase { - const SOME_TEX = "a^2+b^2=c^2"; - const FORMAT_PLAIN = 'text/plain'; - const FORMAT_HTML = 'text/html'; - const FORMAT_XWIKI = 'text/x-wiki'; - const FORMAT_UNKNOWN = 'unknown/unknown'; - const FORMAT_VALUE = ""; + + const SOME_TEX = 'a^2+b^2=c^2'; protected static $hasRestbase; @@ -23,19 +19,12 @@ class MathFormatterTest extends MediaWikiTestCase { self::$hasRestbase = $rbi->checkBackend( true ); } - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ protected function setUp() { parent::setUp(); - if ( !self::$hasRestbase ) { - $this->markTestSkipped( "Can not connect to Restbase Math interface." ); - } - } - protected function tearDown() { - parent::tearDown(); + if ( !self::$hasRestbase ) { + $this->markTestSkipped( 'Can not connect to Restbase Math interface.' ); + } } /** @@ -43,16 +32,16 @@ class MathFormatterTest extends MediaWikiTestCase { * @covers MathFormatter::__construct() */ public function testBasics() { - $formatter = new MathFormatter( self::FORMAT_PLAIN ); + $formatter = new MathFormatter( 'text/plain' ); // check if the format input was corretly passed to the class - $this->assertEquals( self::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' ); + $this->assertSame( 'text/plain', $formatter->getFormat(), 'test getFormat' ); } /** * @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException */ public function testNotStringValue() { - $formatter = new MathFormatter( self::FORMAT_PLAIN ); + $formatter = new MathFormatter( 'text/plain' ); $formatter->format( new NumberValue( 0 ) ); } @@ -60,7 +49,7 @@ class MathFormatterTest extends MediaWikiTestCase { * @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException */ public function testNullValue() { - $formatter = new MathFormatter( self::FORMAT_PLAIN ); + $formatter = new MathFormatter( 'text/plain' ); $formatter->format( null ); } @@ -68,33 +57,29 @@ class MathFormatterTest extends MediaWikiTestCase { * @expectedException InvalidArgumentException */ public function testUnknownFormat() { - $formatter = new MathFormatter( self::FORMAT_UNKNOWN ); + new MathFormatter( 'unknown/unknown' ); } public function testFormatPlain() { - $formatter = new MathFormatter( self::FORMAT_PLAIN ); + $formatter = new MathFormatter( 'text/plain' ); $value = new StringValue( self::SOME_TEX ); $resultFormat = $formatter->format( $value ); - $this->assertEquals( self::SOME_TEX, $resultFormat, - 'Results should be equal' ); - + $this->assertSame( self::SOME_TEX, $resultFormat ); } public function testFormatHtml() { - $formatter = new MathFormatter( self::FORMAT_HTML ); + $formatter = new MathFormatter( 'text/html' ); $value = new StringValue( self::SOME_TEX ); $resultFormat = $formatter->format( $value ); - $this->assertContains( '', $resultFormat, - 'Result must contain math-tag' ); + $this->assertContains( '', $resultFormat, 'Result must contain math-tag' ); } public function testFormatXWiki() { $tex = self::SOME_TEX; - $formatter = new MathFormatter( self::FORMAT_XWIKI ); + $formatter = new MathFormatter( 'text/x-wiki' ); $value = new StringValue( self::SOME_TEX ); $resultFormat = $formatter->format( $value ); - $this->assertEquals( "$tex", $resultFormat, - 'Tex wasn\'t properly wrapped' ); - + $this->assertSame( "$tex", $resultFormat, 'Tex wasn\'t properly wrapped' ); } + }