Skip to content

Commit

Permalink
Merge pull request #8 from PHPOffice/unittest
Browse files Browse the repository at this point in the history
PHPUnit : Improved Unit Tests
  • Loading branch information
Progi1984 committed Sep 25, 2023
2 parents e9f9588 + 77df9c2 commit 93547ad
Show file tree
Hide file tree
Showing 29 changed files with 564 additions and 166 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.1
php-version: 7.2
extensions: dom, xml
coverage: xdebug
- name: Create directory public/coverage
Expand All @@ -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.2
run: wget https://github.com/phpDocumentor/phpDocumentor/releases/download/v3.0.0/phpDocumentor.phar && chmod +x phpDocumentor.phar
- name: Build Documentation
run: ./phpDocumentor.phar run -d ./src -t ./public/docs

### Deploy
- name: Deploy
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,23 @@ jobs:
with:
php-version: ${{ matrix.php }}
extensions: xml
coverage: ${{ (matrix.php == '7.1') && 'xdebug' || 'none' }}
coverage: ${{ (matrix.php == '8.1') && 'xdebug' || 'none' }}

- uses: actions/checkout@v4

- name: Install dependencies
run: composer install --ansi --prefer-dist --no-interaction --no-progress

- name: Run PHPUnit
if: matrix.php != '7.1'
if: matrix.php != '8.1'
run: ./vendor/bin/phpunit -c phpunit.xml.dist

- name: Run PHPUnit (w CodeCoverage)
if: matrix.php == '7.1'
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml
if: matrix.php == '8.1'
run: XDEBUG_MODE=coverage ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml

- name: Upload coverage results to Coveralls
if: matrix.php == '7.1'
if: matrix.php == '8.1'
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
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
3 changes: 2 additions & 1 deletion 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 All @@ -22,7 +23,7 @@
"ext-xml": "*"
},
"require-dev": {
"phpunit/phpunit": ">=7.0",
"phpunit/phpunit": "^7.0 || ^9.0",
"phpstan/phpstan": "^0.12.88 || ^1.0.0"
}
}
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
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@
<log type="coverage-html" target="./build/coverage" />
<log type="coverage-clover" target="./build/clover.xml" />
</logging>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
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
7 changes: 7 additions & 0 deletions src/Math/Exception/InvalidInputException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PhpOffice\Math\Exception;

class InvalidInputException extends MathException
{
}
9 changes: 9 additions & 0 deletions src/Math/Exception/MathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpOffice\Math\Exception;

use Exception;

class MathException extends Exception
{
}
7 changes: 7 additions & 0 deletions src/Math/Exception/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PhpOffice\Math\Exception;

class NotImplementedException extends MathException
{
}
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;
}
}
33 changes: 21 additions & 12 deletions src/Math/Reader/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use DOMElement;
use DOMNode;
use DOMXPath;
use Exception;
use PhpOffice\Math\Element;
use PhpOffice\Math\Exception\InvalidInputException;
use PhpOffice\Math\Exception\NotImplementedException;
use PhpOffice\Math\Math;

class MathML implements ReaderInterface
Expand Down Expand Up @@ -74,15 +75,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 InvalidInputException(sprintf(
'%s : The tag `%s` has not two subelements',
__METHOD__,
$nodeElement->nodeName
));
case 'mi':
return new Element\Identifier($nodeValue);
case 'mn':
Expand All @@ -105,19 +110,23 @@ 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 InvalidInputException(sprintf(
'%s : The tag `%s` has not two subelements',
__METHOD__,
$nodeElement->nodeName
));
case 'semantics':
return new Element\Semantics();
default:
throw new Exception(sprintf(
throw new NotImplementedException(sprintf(
'%s : The tag `%s` is not implemented',
__METHOD__,
$nodeElement->nodeName
Expand Down
34 changes: 25 additions & 9 deletions src/Math/Reader/OfficeMathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use DOMDocument;
use DOMNode;
use DOMXPath;
use Exception;
use PhpOffice\Math\Element;
use PhpOffice\Math\Exception\InvalidInputException;
use PhpOffice\Math\Exception\NotImplementedException;
use PhpOffice\Math\Math;

class OfficeMathML implements ReaderInterface
Expand Down Expand Up @@ -66,29 +67,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 InvalidInputException(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 InvalidInputException(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 All @@ -103,11 +115,15 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
return new Element\Identifier($value);
}

return new Element\Identifier('');
throw new InvalidInputException(sprintf(
'%s : The tag `%s` has no tag `m:t` defined',
__METHOD__,
$nodeElement->nodeName
));
case 'm:oMath':
return new Element\Row();
default:
throw new Exception(sprintf(
throw new NotImplementedException(sprintf(
'%s : The tag `%s` is not implemented',
__METHOD__,
$nodeElement->nodeName
Expand Down
Loading

0 comments on commit 93547ad

Please sign in to comment.