Skip to content

Commit

Permalink
PHPUnit : Improved Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Sep 25, 2023
1 parent e9f9588 commit 5694073
Show file tree
Hide file tree
Showing 20 changed files with 354 additions and 104 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ jobs:
- name: Build Coverage Report
run: XDEBUG_MODE=coverage ./vendor/bin/phpunit -c ./ --coverage-text --coverage-html ./public/coverage
### PHPDoc
#- name: Create directory public/docs
# run: mkdir ./public/docs
#- name: Install PhpDocumentor
# run: wget https://phpdoc.org/phpDocumentor.phar && chmod +x phpDocumentor.phar
#- name: Build Documentation
# run: ./phpDocumentor.phar run -d ./src -t ./public/docs
- name: Create directory public/docs
run: mkdir ./public/docs
- name: Install PhpDocumentor
## Support PHP 7.1
run: wget https://github.com/phpDocumentor/phpDocumentor/releases/download/v3.1.0/phpDocumentor.phar && chmod +x phpDocumentor.phar
- name: Build Documentation
run: ./phpDocumentor.phar run -d ./src -t ./public/docs

### Deploy
- name: Deploy
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.php_cs.cache
.php-cs-fixer.cache
.phpunit.cache
.phpunit.result.cache
composer.lock
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "phpoffice/math",
"description": "Math - Manipulate Math Formula",
"keywords": ["PHP","mathml", "officemathml"],
"homepage": "https://phpoffice.github.io/Math/",
"type": "library",
"license": "MIT",
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions docs/changes/0.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Initial version by [@Progi1984](https://github/Progi1984)
- MathML Reader : Support for Semantics by [@Progi1984](https://github/Progi1984) in [#4](https://github.com/PHPOffice/Math/pull/4)
- PHPUnit : Improved Unit Tests by [@Progi1984](https://github/Progi1984) in [#8](https://github.com/PHPOffice/Math/pull/8)

## Bug fixes

Expand Down
24 changes: 0 additions & 24 deletions src/Math/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@

namespace PhpOffice\Math\Element;

use PhpOffice\Math\Math;

abstract class AbstractElement
{
/**
* @var Math|AbstractGroupElement|null
*/
protected $parent;

/**
* @param Math|AbstractGroupElement|null $parent
*/
public function setParent($parent): self
{
$this->parent = $parent;

return $this;
}

/**
* @return Math|AbstractGroupElement|null
*/
public function getParent()
{
return $this->parent;
}
}
3 changes: 0 additions & 3 deletions src/Math/Element/AbstractGroupElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ abstract class AbstractGroupElement extends AbstractElement
public function add(AbstractElement $element): self
{
$this->elements[] = $element;
$element->setParent($this);

return $this;
}
Expand All @@ -23,8 +22,6 @@ public function remove(AbstractElement $element): self
return $child != $element;
});

$element->setParent(null);

return $this;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Math/Element/Fraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Fraction extends AbstractElement
*/
protected $numerator;

public function __construct(AbstractElement $numerator, AbstractElement $denominator)
{
$this->setNumerator($numerator);
$this->setDenominator($denominator);
}

public function getDenominator(): AbstractElement
{
return $this->denominator;
Expand Down
6 changes: 6 additions & 0 deletions src/Math/Element/Superscript.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Superscript extends AbstractElement
*/
protected $superscript;

public function __construct(AbstractElement $base, AbstractElement $superscript)
{
$this->setBase($base);
$this->setSuperscript($superscript);
}

public function getBase(): AbstractElement
{
return $this->base;
Expand Down
44 changes: 2 additions & 42 deletions src/Math/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,8 @@

namespace PhpOffice\Math;

use PhpOffice\Math\Element\AbstractElement;
use PhpOffice\Math\Element\AbstractGroupElement;

class Math
class Math extends AbstractGroupElement
{
/**
* @var AbstractElement[]
*/
protected $elements = [];

/**
* @param Element\AbstractElement $element
*
* @return self
*/
public function add(Element\AbstractElement $element): self
{
$this->elements[] = $element;
$element->setParent($this);

return $this;
}

/**
* @param Element\AbstractElement $element
*
* @return self
*/
public function remove(Element\AbstractElement $element): self
{
$this->elements = array_filter($this->elements, function ($child) use ($element) {
return $child != $element;
});
$element->setParent(null);

return $this;
}

/**
* @return AbstractElement[]
*/
public function getElements(): array
{
return $this->elements;
}
}
28 changes: 18 additions & 10 deletions src/Math/Reader/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
$nodeValue = trim($nodeElement->nodeValue);
switch ($nodeElement->nodeName) {
case 'mfrac':
$element = new Element\Fraction();
$nodeList = $this->xpath->query('*', $nodeElement);
if ($nodeList && $nodeList->length == 2) {
$element
->setNumerator($this->getElement($nodeList->item(0)))
->setDenominator($this->getElement($nodeList->item(1)));
return new Element\Fraction(
$this->getElement($nodeList->item(0)),
$this->getElement($nodeList->item(1))
);
}

return $element;
throw new Exception(sprintf(
'%s : The tag `%s` has not two subelements',
__METHOD__,
$nodeElement->nodeName
));
case 'mi':
return new Element\Identifier($nodeValue);
case 'mn':
Expand All @@ -105,15 +109,19 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
case 'mrow':
return new Element\Row();
case 'msup':
$element = new Element\Superscript();
$nodeList = $this->xpath->query('*', $nodeElement);
if ($nodeList && $nodeList->length == 2) {
$element
->setBase($this->getElement($nodeList->item(0)))
->setSuperscript($this->getElement($nodeList->item(1)));
return new Element\Superscript(
$this->getElement($nodeList->item(0)),
$this->getElement($nodeList->item(1))
);
}

return $element;
throw new Exception(sprintf(
'%s : The tag `%s` has not two subelements',
__METHOD__,
$nodeElement->nodeName
));
case 'semantics':
return new Element\Semantics();
default:
Expand Down
23 changes: 17 additions & 6 deletions src/Math/Reader/OfficeMathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,40 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
{
switch ($nodeElement->nodeName) {
case 'm:f':
$element = new Element\Fraction();
// Numerator
$nodeNumerator = $this->xpath->query('m:num/m:r/m:t', $nodeElement);
if ($nodeNumerator && $nodeNumerator->length == 1) {
$value = $nodeNumerator->item(0)->nodeValue;
if (is_numeric($value)) {
$element->setNumerator(new Element\Numeric(floatval($value)));
$numerator = new Element\Numeric(floatval($value));
} else {
$element->setNumerator(new Element\Identifier($value));
$numerator = new Element\Identifier($value);
}
} else {
throw new Exception(sprintf(
'%s : The tag `%s` has no numerator defined',
__METHOD__,
$nodeElement->nodeName
));
}
// Denominator
$nodeDenominator = $this->xpath->query('m:den/m:r/m:t', $nodeElement);
if ($nodeDenominator && $nodeDenominator->length == 1) {
$value = $nodeDenominator->item(0)->nodeValue;
if (is_numeric($value)) {
$element->setDenominator(new Element\Numeric(floatval($value)));
$denominator = new Element\Numeric(floatval($value));
} else {
$element->setDenominator(new Element\Identifier($value));
$denominator = new Element\Identifier($value);
}
} else {
throw new Exception(sprintf(
'%s : The tag `%s` has no denominator defined',
__METHOD__,
$nodeElement->nodeName
));
}

return $element;
return new Element\Fraction($numerator, $denominator);
case 'm:r':
$nodeText = $this->xpath->query('m:t', $nodeElement);
if ($nodeText && $nodeText->length == 1) {
Expand Down
58 changes: 58 additions & 0 deletions tests/Math/Element/AbstractGroupElementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

use PhpOffice\Math\Element;
use PHPUnit\Framework\TestCase;

class AbstractGroupElementTest extends TestCase
{
/**
* @covers \AbstractGroupElement::__construct
* @covers \AbstractGroupElement::getElements
*/
public function testConstruct(): void
{
$row = new Element\Row();

$this->assertIsArray($row->getElements());
$this->assertCount(0, $row->getElements());
}

/**
* @covers \AbstractGroupElement::add
* @covers \AbstractGroupElement::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());
}

/**
* @covers \AbstractGroupElement::add
* @covers \AbstractGroupElement::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());
}
}
60 changes: 60 additions & 0 deletions tests/Math/Element/FractionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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
{
/**
* @covers \Fraction::__construct
*/
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());
}

/**
* @covers \Fraction::getNumerator
* @covers \Fraction::setNumerator
*/
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());
}

/**
* @covers \Fraction::getDenominator
* @covers \Fraction::setDenominator
*/
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());
}
}
Loading

0 comments on commit 5694073

Please sign in to comment.