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,46 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element;
use PHPUnit\Framework\TestCase;
class AbstractGroupElementTest extends TestCase
{
public function testConstruct(): void
{
$row = new Element\Row();
$this->assertIsArray($row->getElements());
$this->assertCount(0, $row->getElements());
}
public function testAdd(): void
{
$identifierA = new Element\Identifier('a');
$row = new Element\Row();
$this->assertCount(0, $row->getElements());
$this->assertInstanceOf(Element\AbstractGroupElement::class, $row->add($identifierA));
$this->assertCount(1, $row->getElements());
$this->assertEquals([$identifierA], $row->getElements());
}
public function testRemove(): void
{
$identifierA = new Element\Identifier('a');
$row = new Element\Row();
$row->add($identifierA);
$this->assertCount(1, $row->getElements());
$this->assertInstanceOf(Element\AbstractGroupElement::class, $row->remove($identifierA));
$this->assertCount(0, $row->getElements());
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element;
use PhpOffice\Math\Element\Fraction;
use PHPUnit\Framework\TestCase;
class FractionTest extends TestCase
{
public function testConstruct(): void
{
$identifierA = new Element\Identifier('a');
$identifierB = new Element\Identifier('b');
$fraction = new Fraction($identifierA, $identifierB);
$this->assertEquals($identifierA, $fraction->getNumerator());
$this->assertEquals($identifierB, $fraction->getDenominator());
}
public function testBase(): void
{
$identifierA = new Element\Identifier('a');
$identifierB = new Element\Identifier('b');
$identifierC = new Element\Identifier('c');
$fraction = new Fraction($identifierA, $identifierB);
$this->assertEquals($identifierA, $fraction->getNumerator());
$this->assertInstanceOf(Fraction::class, $fraction->setNumerator($identifierC));
$this->assertEquals($identifierC, $fraction->getNumerator());
}
public function testFraction(): void
{
$identifierA = new Element\Identifier('a');
$identifierB = new Element\Identifier('b');
$identifierC = new Element\Identifier('c');
$fraction = new Fraction($identifierA, $identifierB);
$this->assertEquals($identifierB, $fraction->getDenominator());
$this->assertInstanceOf(Fraction::class, $fraction->setDenominator($identifierC));
$this->assertEquals($identifierC, $fraction->getDenominator());
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element\Identifier;
use PHPUnit\Framework\TestCase;
class IdentifierTest extends TestCase
{
public function testConstruct(): void
{
$operator = new Identifier('x');
$this->assertEquals('x', $operator->getValue());
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element\Numeric;
use PHPUnit\Framework\TestCase;
class NumericTest extends TestCase
{
public function testConstruct(): void
{
$numeric = new Numeric(2);
$this->assertEquals(2, $numeric->getValue());
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element\Operator;
use PHPUnit\Framework\TestCase;
class OperatorTest extends TestCase
{
public function testConstruct(): void
{
$operator = new Operator('+');
$this->assertEquals('+', $operator->getValue());
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element\Semantics;
use PHPUnit\Framework\TestCase;
class SemanticsTest extends TestCase
{
public function testConstruct(): void
{
$semantics = new Semantics();
$this->assertIsArray($semantics->getAnnotations());
$this->assertCount(0, $semantics->getAnnotations());
}
public function testAnnotation(): void
{
$semantics = new Semantics();
$this->assertIsArray($semantics->getAnnotations());
$this->assertCount(0, $semantics->getAnnotations());
$this->assertInstanceOf(Semantics::class, $semantics->addAnnotation('encoding', 'content'));
$this->assertEquals(['encoding' => 'content'], $semantics->getAnnotations());
$this->assertCount(1, $semantics->getAnnotations());
$this->assertEquals('content', $semantics->getAnnotation('encoding'));
$this->assertNull($semantics->getAnnotation('notexisting'));
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Element;
use PhpOffice\Math\Element;
use PhpOffice\Math\Element\Superscript;
use PHPUnit\Framework\TestCase;
class SuperscriptTest extends TestCase
{
public function testConstruct(): void
{
$superscript = new Superscript(new Element\Identifier('a'), new Element\Identifier('a'));
$this->assertInstanceOf(Element\Identifier::class, $superscript->getBase());
$this->assertInstanceOf(Element\Identifier::class, $superscript->getSuperscript());
}
public function testBase(): void
{
$identifierA = new Element\Identifier('a');
$identifierB = new Element\Identifier('b');
$identifierC = new Element\Identifier('c');
$superscript = new Superscript($identifierA, $identifierB);
$this->assertEquals($identifierA, $superscript->getBase());
$this->assertInstanceOf(Superscript::class, $superscript->setBase($identifierC));
$this->assertEquals($identifierC, $superscript->getBase());
}
public function testSuperscript(): void
{
$identifierA = new Element\Identifier('a');
$identifierB = new Element\Identifier('b');
$identifierC = new Element\Identifier('c');
$superscript = new Superscript($identifierA, $identifierB);
$this->assertEquals($identifierB, $superscript->getSuperscript());
$this->assertInstanceOf(Superscript::class, $superscript->setSuperscript($identifierC));
$this->assertEquals($identifierC, $superscript->getSuperscript());
}
}

View File

@@ -0,0 +1,243 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Reader;
use PhpOffice\Math\Element;
use PhpOffice\Math\Exception\InvalidInputException;
use PhpOffice\Math\Exception\NotImplementedException;
use PhpOffice\Math\Math;
use PhpOffice\Math\Reader\MathML;
use PHPUnit\Framework\TestCase;
class MathMLTest extends TestCase
{
public function testReadBasic(): void
{
$content = '<?xml version="1.0" encoding="UTF-8"?>
<!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>&InvisibleTimes;</mo> <msup><mi>x</mi><mn>2</mn></msup>
<mo>+</mo><mi>b</mi><mo>&InvisibleTimes;</mo><mi>x</mi>
<mo>+</mo><mi>c</mi>
</mrow>
</math>';
$reader = new MathML();
$math = $reader->read($content);
$this->assertInstanceOf(Math::class, $math);
$elements = $math->getElements();
$this->assertCount(1, $elements);
$this->assertInstanceOf(Element\Row::class, $elements[0]);
/** @var Element\Row $element */
$element = $elements[0];
$subElements = $element->getElements();
$this->assertCount(9, $subElements);
/** @var Element\Identifier $subElement */
$subElement = $subElements[0];
$this->assertInstanceOf(Element\Identifier::class, $subElement);
$this->assertEquals('a', $subElement->getValue());
/** @var Element\Identifier $subElement */
$subElement = $subElements[1];
$this->assertInstanceOf(Element\Operator::class, $subElement);
$this->assertEquals('InvisibleTimes', $subElement->getValue());
/** @var Element\Superscript $subElement */
$subElement = $subElements[2];
$this->assertInstanceOf(Element\Superscript::class, $subElements[2]);
/** @var Element\Identifier $base */
$base = $subElement->getBase();
$this->assertInstanceOf(Element\Identifier::class, $base);
$this->assertEquals('x', $base->getValue());
/** @var Element\Numeric $superscript */
$superscript = $subElement->getSuperscript();
$this->assertInstanceOf(Element\Numeric::class, $superscript);
$this->assertEquals(2, $superscript->getValue());
/** @var Element\Operator $subElement */
$subElement = $subElements[3];
$this->assertInstanceOf(Element\Operator::class, $subElement);
$this->assertEquals('+', $subElement->getValue());
/** @var Element\Identifier $subElement */
$subElement = $subElements[4];
$this->assertInstanceOf(Element\Identifier::class, $subElement);
$this->assertEquals('b', $subElement->getValue());
/** @var Element\Operator $subElement */
$subElement = $subElements[5];
$this->assertInstanceOf(Element\Operator::class, $subElement);
$this->assertEquals('InvisibleTimes', $subElement->getValue());
/** @var Element\Identifier $subElement */
$subElement = $subElements[6];
$this->assertInstanceOf(Element\Identifier::class, $subElement);
$this->assertEquals('x', $subElement->getValue());
/** @var Element\Operator $subElement */
$subElement = $subElements[7];
$this->assertInstanceOf(Element\Operator::class, $subElement);
$this->assertEquals('+', $subElement->getValue());
/** @var Element\Identifier $subElement */
$subElement = $subElements[8];
$this->assertInstanceOf(Element\Identifier::class, $subElement);
$this->assertEquals('c', $subElement->getValue());
}
public function testReadFraction(): void
{
$content = '<?xml version="1.0" encoding="UTF-8"?>
<!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 bevelled="true">
<mfrac>
<mi> a </mi>
<mi> b </mi>
</mfrac>
<mfrac>
<mi> c </mi>
<mi> d </mi>
</mfrac>
</mfrac>
</math>';
$reader = new MathML();
$math = $reader->read($content);
$this->assertInstanceOf(Math::class, $math);
$elements = $math->getElements();
$this->assertCount(1, $elements);
$this->assertInstanceOf(Element\Fraction::class, $elements[0]);
/** @var Element\Fraction $element */
$element = $elements[0];
$this->assertInstanceOf(Element\Fraction::class, $element->getNumerator());
/** @var Element\Fraction $subElement */
$subElement = $element->getNumerator();
/** @var Element\Identifier $numerator */
$numerator = $subElement->getNumerator();
$this->assertInstanceOf(Element\Identifier::class, $numerator);
$this->assertEquals('a', $numerator->getValue());
/** @var Element\Identifier $denominator */
$denominator = $subElement->getDenominator();
$this->assertInstanceOf(Element\Identifier::class, $denominator);
$this->assertEquals('b', $denominator->getValue());
$this->assertInstanceOf(Element\Fraction::class, $element->getDenominator());
/** @var Element\Fraction $subElement */
$subElement = $element->getDenominator();
/** @var Element\Identifier $numerator */
$numerator = $subElement->getNumerator();
$this->assertInstanceOf(Element\Identifier::class, $numerator);
$this->assertEquals('c', $numerator->getValue());
/** @var Element\Identifier $denominator */
$denominator = $subElement->getDenominator();
$this->assertInstanceOf(Element\Identifier::class, $denominator);
$this->assertEquals('d', $denominator->getValue());
}
public function testReadFractionInvalid(): void
{
$this->expectException(InvalidInputException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\MathML::getElement : The tag `mfrac` has not two subelements');
$content = '<?xml version="1.0" encoding="UTF-8"?>
<!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> a </mi>
</mfrac>
</math>';
$reader = new MathML();
$math = $reader->read($content);
}
public function testReadSuperscriptInvalid(): void
{
$this->expectException(InvalidInputException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\MathML::getElement : The tag `msup` has not two subelements');
$content = '<?xml version="1.0" encoding="UTF-8"?>
<!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">
<msup>
<mi> a </mi>
</msup>
</math>';
$reader = new MathML();
$math = $reader->read($content);
}
public function testReadSemantics(): void
{
$content = '<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<semantics>
<mrow>
<mfrac>
<mi>π</mi>
<mn>2</mn>
</mfrac>
<mo stretchy="false">+</mo>
<mrow>
<mi>a</mi>
<mo stretchy="false"></mo>
<mn>2</mn>
</mrow>
</mrow>
<annotation encoding="StarMath 5.0">{π} over {2} + { a } * 2 </annotation>
</semantics>
</math>';
$reader = new MathML();
$math = $reader->read($content);
$this->assertInstanceOf(Math::class, $math);
$elements = $math->getElements();
$this->assertCount(1, $elements);
$this->assertInstanceOf(Element\Semantics::class, $elements[0]);
/** @var Element\Semantics $element */
$element = $elements[0];
// Check MathML
$subElements = $element->getElements();
$this->assertCount(1, $subElements);
$this->assertInstanceOf(Element\Row::class, $subElements[0]);
// Check Annotation
$this->assertCount(1, $element->getAnnotations());
$this->assertEquals('{π} over {2} + { a } * 2', $element->getAnnotation('StarMath 5.0'));
}
public function testReadNotImplemented(): void
{
$this->expectException(NotImplementedException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\MathML::getElement : The tag `mnotexisting` is not implemented');
$content = '<?xml version="1.0" encoding="UTF-8"?>
<!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">
<mnotexisting>
<mi> a </mi>
</mnotexisting>
</math>';
$reader = new MathML();
$math = $reader->read($content);
}
}

View File

@@ -0,0 +1,197 @@
<?php
declare(strict_types=1);
namespace Tests\PhpOffice\Math\Reader;
use PhpOffice\Math\Element;
use PhpOffice\Math\Exception\InvalidInputException;
use PhpOffice\Math\Exception\NotImplementedException;
use PhpOffice\Math\Math;
use PhpOffice\Math\Reader\OfficeMathML;
use PHPUnit\Framework\TestCase;
class OfficeMathMLTest extends TestCase
{
public function testRead(): void
{
$content = '<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMath>
<m:f>
<m:num><m:r><m:t>2</m:t></m:r></m:num>
<m:den><m:r><m:t>π</m:t></m:r></m:den>
</m:f>
</m:oMath>
</m:oMathPara>';
$reader = new OfficeMathML();
$math = $reader->read($content);
$this->assertInstanceOf(Math::class, $math);
$elements = $math->getElements();
$this->assertCount(1, $elements);
$this->assertInstanceOf(Element\Row::class, $elements[0]);
/** @var Element\Row $element */
$element = $elements[0];
$subElements = $element->getElements();
$this->assertCount(1, $subElements);
$this->assertInstanceOf(Element\Fraction::class, $subElements[0]);
/** @var Element\Fraction $subElement */
$subElement = $subElements[0];
/** @var Element\Identifier $numerator */
$numerator = $subElement->getNumerator();
$this->assertInstanceOf(Element\Numeric::class, $numerator);
$this->assertEquals(2, $numerator->getValue());
/** @var Element\Numeric $denominator */
$denominator = $subElement->getDenominator();
$this->assertInstanceOf(Element\Identifier::class, $denominator);
$this->assertEquals('π', $denominator->getValue());
}
public function testReadWithWTag(): void
{
$content = '<m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:f>
<m:num>
<m:r>
<w:rPr><w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/></w:rPr>
<m:t xml:space="preserve">π</m:t>
</m:r>
</m:num>
<m:den>
<m:r>
<w:rPr><w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/></w:rPr>
<m:t xml:space="preserve">2</m:t>
</m:r>
</m:den>
</m:f>
<m:r>
<w:rPr><w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/></w:rPr>
<m:t xml:space="preserve">+</m:t>
</m:r>
<m:r>
<w:rPr><w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/></w:rPr>
<m:t xml:space="preserve">a</m:t>
</m:r>
<m:r>
<w:rPr><w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/></w:rPr>
<m:t xml:space="preserve"></m:t>
</m:r>
<m:r>
<w:rPr><w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/></w:rPr>
<m:t xml:space="preserve">2</m:t>
</m:r>
</m:oMath>';
$reader = new OfficeMathML();
$math = $reader->read($content);
$this->assertInstanceOf(Math::class, $math);
$elements = $math->getElements();
$this->assertCount(5, $elements);
/** @var Element\Fraction $element */
$element = $elements[0];
$this->assertInstanceOf(Element\Fraction::class, $element);
/** @var Element\Identifier $numerator */
$numerator = $element->getNumerator();
$this->assertInstanceOf(Element\Identifier::class, $numerator);
$this->assertEquals('π', $numerator->getValue());
/** @var Element\Numeric $denominator */
$denominator = $element->getDenominator();
$this->assertInstanceOf(Element\Numeric::class, $denominator);
$this->assertEquals(2, $denominator->getValue());
/** @var Element\Operator $element */
$element = $elements[1];
$this->assertInstanceOf(Element\Operator::class, $element);
$this->assertEquals('+', $element->getValue());
/** @var Element\Identifier $element */
$element = $elements[2];
$this->assertInstanceOf(Element\Identifier::class, $element);
$this->assertEquals('a', $element->getValue());
/** @var Element\Operator $element */
$element = $elements[3];
$this->assertInstanceOf(Element\Operator::class, $element);
$this->assertEquals('', $element->getValue());
/** @var Element\Numeric $element */
$element = $elements[4];
$this->assertInstanceOf(Element\Numeric::class, $element);
$this->assertEquals(2, $element->getValue());
}
public function testReadFractionNoNumerator(): void
{
$this->expectException(InvalidInputException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\OfficeMathML::getElement : The tag `m:f` has no numerator defined');
$content = '<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMath>
<m:f>
<m:den><m:r><m:t>2</m:t></m:r></m:den>
</m:f>
</m:oMath>
</m:oMathPara>';
$reader = new OfficeMathML();
$math = $reader->read($content);
}
public function testReadFractionNoDenominator(): void
{
$this->expectException(InvalidInputException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\OfficeMathML::getElement : The tag `m:f` has no denominator defined');
$content = '<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:f>
</m:oMath>
</m:oMathPara>';
$reader = new OfficeMathML();
$math = $reader->read($content);
}
public function testReadBasicNoText(): void
{
$this->expectException(InvalidInputException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\OfficeMathML::getElement : The tag `m:r` has no tag `m:t` defined');
$content = '<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMath>
<m:r>
a
</m:r>
</m:oMath>
</m:oMathPara>';
$reader = new OfficeMathML();
$math = $reader->read($content);
}
public function testReadNotImplemented(): void
{
$this->expectException(NotImplementedException::class);
$this->expectExceptionMessage('PhpOffice\Math\Reader\OfficeMathML::getElement : The tag `m:mnotexisting` is not implemented');
$content = '<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMath>
<m:mnotexisting>
<m:num><m:r><m:t>π</m:t></m:r></m:num>
</m:mnotexisting>
</m:oMath>
</m:oMathPara>';
$reader = new OfficeMathML();
$math = $reader->read($content);
}
}

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
));
}
}