mediawiki-extensions-Math/tests/phpunit/MathFormatterTest.php
Umherirrender e599afa6db Use assertStringContainsString in unit tests
Using assertContains() with string haystacks is deprecated and will not
be supported in PHPUnit 9. Refactor your test to use
assertStringContainsString() or assertStringContainsStringIgnoringCase()
instead.

Change-Id: Ic35f3c60a7f49dfe244b87192d7f161c117b37e1
2020-04-05 13:46:04 +02:00

116 lines
3.5 KiB
PHP

<?php
use DataValues\NumberValue;
use DataValues\StringValue;
use Wikibase\Lib\Formatters\SnakFormatter;
/**
* Test the results of MathFormatter
*
* @covers \MathFormatter
*
* @group Math
*
* @license GPL-2.0-or-later
*/
class MathFormatterTest extends MediaWikiTestCase {
const SOME_TEX = 'a^2+b^2 < c^2';
protected static $hasRestbase;
public static function setUpBeforeClass() : void {
$rbi = new MathRestbaseInterface();
self::$hasRestbase = $rbi->checkBackend( true );
}
protected function setUp() : void {
parent::setUp();
if ( !self::$hasRestbase ) {
$this->markTestSkipped( 'Can not connect to Restbase Math interface.' );
}
}
/**
* Checks the
* @covers \MathFormatter::__construct
*/
public function testBasics() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
// check if the format input was corretly passed to the class
$this->assertSame( SnakFormatter::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' );
}
public function testNotStringValue() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$this->expectException( InvalidArgumentException::class );
$formatter->format( new NumberValue( 0 ) );
}
public function testNullValue() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$this->expectException( InvalidArgumentException::class );
$formatter->format( null );
}
public function testUnknownFormatFallsBackToMathMl() {
$formatter = new MathFormatter( 'unknown/unknown' );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( '</math>', $resultFormat );
}
/**
* @covers \MathFormatter::format
*/
public function testUnknownFormatFailure() {
$formatter = new MathFormatter( 'unknown/unknown' );
$value = new StringValue( '\noTex' );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( 'unknown function', $resultFormat );
}
public function testFormatPlain() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertSame( self::SOME_TEX, $resultFormat );
}
public function testFormatHtml() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( '</math>', $resultFormat, 'Result must contain math-tag' );
}
public function testFormatDiffHtml() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertStringContainsString( '</math>', $resultFormat, 'Result must contain math-tag' );
$this->assertStringContainsString( '</h4>', $resultFormat, 'Result must contain a <h4> tag' );
$this->assertStringContainsString( '</code>', $resultFormat, 'Result must contain a <code> tag' );
$this->assertStringContainsString(
'wb-details',
$resultFormat,
'Result must contain wb-details class'
);
$this->assertStringContainsString(
htmlspecialchars( self::SOME_TEX ),
$resultFormat,
'Result must contain the TeX source'
);
}
public function testFormatXWiki() {
$tex = self::SOME_TEX;
$formatter = new MathFormatter( SnakFormatter::FORMAT_WIKI );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );
}
}