This commit is contained in:
wangjinlei
2024-07-17 09:23:45 +08:00
parent edf9deeb1a
commit 881ac3e056
1001 changed files with 41032 additions and 5452 deletions

View File

@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Writer;
use PhpOffice\Math\Element;
use PhpOffice\Math\Exception\NotImplementedException;
use PhpOffice\Math\Math;
use PhpOffice\Math\Writer\MathML;
class MathMLTest extends WriterTestCase
{
public function testWrite(): void
{
$opTimes = new Element\Operator('&InvisibleTimes;');
$math = new Math();
$row = new Element\Row();
$math->add($row);
$row->add(new Element\Identifier('a'));
$row->add(clone $opTimes);
$superscript = new Element\Superscript(
new Element\Identifier('x'),
new Element\Numeric(2)
);
$row->add($superscript);
$row->add(new Element\Operator('+'));
$row->add(new Element\Identifier('b'));
$row->add(clone $opTimes);
$row->add(new Element\Identifier('x'));
$row->add(new Element\Operator('+'));
$row->add(new Element\Identifier('c'));
$writer = new MathML();
$output = $writer->write($math);
$expected = '<?xml version="1.0" encoding="UTF-8"?>'
. PHP_EOL
. '<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">'
. '<math xmlns="http://www.w3.org/1998/Math/MathML">'
. '<mrow><mi>a</mi><mo>&amp;InvisibleTimes;</mo><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>b</mi><mo>&amp;InvisibleTimes;</mo><mi>x</mi><mo>+</mo><mi>c</mi>'
. '</mrow>'
. '</math>'
. PHP_EOL;
$this->assertEquals($expected, $output);
$this->assertIsSchemaMathMLValid($output);
}
public function testWriteFraction(): void
{
$math = new Math();
$fraction = new Element\Fraction(
new Element\Identifier('π'),
new Element\Numeric(2)
);
$math->add($fraction);
$writer = new MathML();
$output = $writer->write($math);
$expected = '<?xml version="1.0" encoding="UTF-8"?>'
. PHP_EOL
. '<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">'
. '<math xmlns="http://www.w3.org/1998/Math/MathML">'
. '<mfrac>'
. '<mi>π</mi><mn>2</mn>'
. '</mfrac>'
. '</math>'
. PHP_EOL;
$this->assertEquals($expected, $output);
$this->assertIsSchemaMathMLValid($output);
}
public function testWriteNotImplemented(): void
{
$this->expectException(NotImplementedException::class);
if (method_exists($this, 'expectExceptionMessageRegExp')) {
$this->expectExceptionMessageRegExp('/PhpOffice\\\Math\\\Writer\\\MathML::getElementTagName : The element of the class/');
$this->expectExceptionMessageRegExp('/has no tag name/');
} else {
// @phpstan-ignore-next-line
$this->expectExceptionMessageMatches('/PhpOffice\\\Math\\\Writer\\\MathML::getElementTagName : The element of the class/');
// @phpstan-ignore-next-line
$this->expectExceptionMessageMatches('/has no tag name/');
}
$math = new Math();
$object = new class() extends Element\AbstractElement {};
$math->add($object);
$writer = new MathML();
$output = $writer->write($math);
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Writer;
use PhpOffice\Math\Element;
use PhpOffice\Math\Exception\NotImplementedException;
use PhpOffice\Math\Math;
use PhpOffice\Math\Writer\OfficeMathML;
class OfficeMathMLTest extends WriterTestCase
{
public function testWriteFraction(): void
{
$math = new Math();
$fraction = new Element\Fraction(
new Element\Identifier('π'),
new Element\Numeric(2)
);
$math->add($fraction);
$writer = new OfficeMathML();
$output = $writer->write($math);
$expected = '<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">'
. '<m:oMath>'
. '<m:f>'
. '<m:num><m:r><m:t>π</m:t></m:r></m:num>'
. '<m:den><m:r><m:t>2</m:t></m:r></m:den>'
. '</m:f>'
. '</m:oMath>'
. '</m:oMathPara>';
$this->assertEquals($expected, $output);
}
public function testWriteRow(): void
{
$math = new Math();
$row = new Element\Row();
$math->add($row);
$row->add(new Element\Identifier('x'));
$writer = new OfficeMathML();
$output = $writer->write($math);
$expected = '<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">'
. '<m:oMath>'
. '<m:r><m:t>x</m:t></m:r>'
. '</m:oMath>'
. '</m:oMathPara>';
$this->assertEquals($expected, $output);
}
public function testWriteNotImplemented(): void
{
$this->expectException(NotImplementedException::class);
if (method_exists($this, 'expectExceptionMessageRegExp')) {
$this->expectExceptionMessageRegExp('/PhpOffice\\\Math\\\Writer\\\OfficeMathML::getElementTagName : The element of the class/');
$this->expectExceptionMessageRegExp('/has no tag name/');
} else {
// @phpstan-ignore-next-line
$this->expectExceptionMessageMatches('/PhpOffice\\\Math\\\Writer\\\OfficeMathML::getElementTagName : The element of the class/');
// @phpstan-ignore-next-line
$this->expectExceptionMessageMatches('/has no tag name/');
}
$math = new Math();
$object = new class() extends Element\AbstractElement {};
$math->add($object);
$writer = new OfficeMathML();
$output = $writer->write($math);
}
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Writer;
use DOMDocument;
use LibXMLError;
use PHPUnit\Framework\TestCase;
class WriterTestCase extends TestCase
{
public function assertIsSchemaMathMLValid(string $content): void
{
$dom = new DOMDocument();
$dom->loadXML($content);
$xmlSource = $dom->saveXML();
if (is_string($xmlSource)) {
$dom->loadXML($xmlSource);
$dom->schemaValidate(dirname(__DIR__, 2) . '/resources/schema/mathml3/mathml3.xsd');
$error = libxml_get_last_error();
if ($error instanceof LibXMLError) {
$this->failXmlError($error, $xmlSource);
} else {
$this->assertTrue(true);
}
return;
}
$this->fail(sprintf('The XML is not valid : %s', $content));
}
/**
* @param array<string, string> $params
*/
protected function failXmlError(LibXMLError $error, string $source, array $params = []): void
{
switch ($error->level) {
case LIBXML_ERR_WARNING:
$errorType = 'warning';
break;
case LIBXML_ERR_ERROR:
$errorType = 'error';
break;
case LIBXML_ERR_FATAL:
$errorType = 'fatal';
break;
default:
$errorType = 'Error';
break;
}
$errorLine = (int) $error->line;
$contents = explode("\n", $source);
$lines = [];
if (isset($contents[$errorLine - 2])) {
$lines[] = '>> ' . $contents[$errorLine - 2];
}
if (isset($contents[$errorLine - 1])) {
$lines[] = '>>> ' . $contents[$errorLine - 1];
}
if (isset($contents[$errorLine])) {
$lines[] = '>> ' . $contents[$errorLine];
}
$paramStr = '';
if (!empty($params)) {
$paramStr .= "\n" . ' - Parameters :' . "\n";
foreach ($params as $key => $val) {
$paramStr .= ' - ' . $key . ' : ' . $val . "\n";
}
}
$this->fail(sprintf(
"Validation %s :\n - - Line : %s\n - Message : %s - Lines :\n%s%s",
$errorType,
$error->line,
$error->message,
implode(PHP_EOL, $lines),
$paramStr
));
}
}