Skip to content

Commit 93547ad

Browse files
authored
Merge pull request #8 from PHPOffice/unittest
PHPUnit : Improved Unit Tests
2 parents e9f9588 + 77df9c2 commit 93547ad

29 files changed

+564
-166
lines changed

.github/workflows/deploy.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Setup PHP
2626
uses: shivammathur/setup-php@v2
2727
with:
28-
php-version: 7.1
28+
php-version: 7.2
2929
extensions: dom, xml
3030
coverage: xdebug
3131
- name: Create directory public/coverage
@@ -35,12 +35,13 @@ jobs:
3535
- name: Build Coverage Report
3636
run: XDEBUG_MODE=coverage ./vendor/bin/phpunit -c ./ --coverage-text --coverage-html ./public/coverage
3737
### PHPDoc
38-
#- name: Create directory public/docs
39-
# run: mkdir ./public/docs
40-
#- name: Install PhpDocumentor
41-
# run: wget https://phpdoc.org/phpDocumentor.phar && chmod +x phpDocumentor.phar
42-
#- name: Build Documentation
43-
# run: ./phpDocumentor.phar run -d ./src -t ./public/docs
38+
- name: Create directory public/docs
39+
run: mkdir ./public/docs
40+
- name: Install PhpDocumentor
41+
## Support PHP 7.2
42+
run: wget https://github.com/phpDocumentor/phpDocumentor/releases/download/v3.0.0/phpDocumentor.phar && chmod +x phpDocumentor.phar
43+
- name: Build Documentation
44+
run: ./phpDocumentor.phar run -d ./src -t ./public/docs
4445

4546
### Deploy
4647
- name: Deploy

.github/workflows/php.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,23 @@ jobs:
8181
with:
8282
php-version: ${{ matrix.php }}
8383
extensions: xml
84-
coverage: ${{ (matrix.php == '7.1') && 'xdebug' || 'none' }}
84+
coverage: ${{ (matrix.php == '8.1') && 'xdebug' || 'none' }}
8585

8686
- uses: actions/checkout@v4
8787

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

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

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

9999
- name: Upload coverage results to Coveralls
100-
if: matrix.php == '7.1'
100+
if: matrix.php == '8.1'
101101
env:
102102
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
103103
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.php_cs.cache
2+
.php-cs-fixer.cache
23
.phpunit.cache
34
.phpunit.result.cache
45
composer.lock

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "phpoffice/math",
33
"description": "Math - Manipulate Math Formula",
44
"keywords": ["PHP","mathml", "officemathml"],
5+
"homepage": "https://phpoffice.github.io/Math/",
56
"type": "library",
67
"license": "MIT",
78
"autoload": {
@@ -22,7 +23,7 @@
2223
"ext-xml": "*"
2324
},
2425
"require-dev": {
25-
"phpunit/phpunit": ">=7.0",
26+
"phpunit/phpunit": "^7.0 || ^9.0",
2627
"phpstan/phpstan": "^0.12.88 || ^1.0.0"
2728
}
2829
}

docs/changes/0.1.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## Bug fixes
910

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
<log type="coverage-html" target="./build/coverage" />
2222
<log type="coverage-clover" target="./build/clover.xml" />
2323
</logging>
24+
<coverage>
25+
<include>
26+
<directory suffix=".php">src</directory>
27+
</include>
28+
</coverage>
2429
</phpunit>

src/Math/Element/AbstractElement.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22

33
namespace PhpOffice\Math\Element;
44

5-
use PhpOffice\Math\Math;
6-
75
abstract class AbstractElement
86
{
9-
/**
10-
* @var Math|AbstractGroupElement|null
11-
*/
12-
protected $parent;
13-
14-
/**
15-
* @param Math|AbstractGroupElement|null $parent
16-
*/
17-
public function setParent($parent): self
18-
{
19-
$this->parent = $parent;
20-
21-
return $this;
22-
}
23-
24-
/**
25-
* @return Math|AbstractGroupElement|null
26-
*/
27-
public function getParent()
28-
{
29-
return $this->parent;
30-
}
317
}

src/Math/Element/AbstractGroupElement.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ abstract class AbstractGroupElement extends AbstractElement
1212
public function add(AbstractElement $element): self
1313
{
1414
$this->elements[] = $element;
15-
$element->setParent($this);
1615

1716
return $this;
1817
}
@@ -23,8 +22,6 @@ public function remove(AbstractElement $element): self
2322
return $child != $element;
2423
});
2524

26-
$element->setParent(null);
27-
2825
return $this;
2926
}
3027

src/Math/Element/Fraction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class Fraction extends AbstractElement
1414
*/
1515
protected $numerator;
1616

17+
public function __construct(AbstractElement $numerator, AbstractElement $denominator)
18+
{
19+
$this->setNumerator($numerator);
20+
$this->setDenominator($denominator);
21+
}
22+
1723
public function getDenominator(): AbstractElement
1824
{
1925
return $this->denominator;

src/Math/Element/Superscript.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class Superscript extends AbstractElement
1414
*/
1515
protected $superscript;
1616

17+
public function __construct(AbstractElement $base, AbstractElement $superscript)
18+
{
19+
$this->setBase($base);
20+
$this->setSuperscript($superscript);
21+
}
22+
1723
public function getBase(): AbstractElement
1824
{
1925
return $this->base;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PhpOffice\Math\Exception;
4+
5+
class InvalidInputException extends MathException
6+
{
7+
}

src/Math/Exception/MathException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace PhpOffice\Math\Exception;
4+
5+
use Exception;
6+
7+
class MathException extends Exception
8+
{
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PhpOffice\Math\Exception;
4+
5+
class NotImplementedException extends MathException
6+
{
7+
}

src/Math/Math.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,8 @@
22

33
namespace PhpOffice\Math;
44

5-
use PhpOffice\Math\Element\AbstractElement;
5+
use PhpOffice\Math\Element\AbstractGroupElement;
66

7-
class Math
7+
class Math extends AbstractGroupElement
88
{
9-
/**
10-
* @var AbstractElement[]
11-
*/
12-
protected $elements = [];
13-
14-
/**
15-
* @param Element\AbstractElement $element
16-
*
17-
* @return self
18-
*/
19-
public function add(Element\AbstractElement $element): self
20-
{
21-
$this->elements[] = $element;
22-
$element->setParent($this);
23-
24-
return $this;
25-
}
26-
27-
/**
28-
* @param Element\AbstractElement $element
29-
*
30-
* @return self
31-
*/
32-
public function remove(Element\AbstractElement $element): self
33-
{
34-
$this->elements = array_filter($this->elements, function ($child) use ($element) {
35-
return $child != $element;
36-
});
37-
$element->setParent(null);
38-
39-
return $this;
40-
}
41-
42-
/**
43-
* @return AbstractElement[]
44-
*/
45-
public function getElements(): array
46-
{
47-
return $this->elements;
48-
}
499
}

src/Math/Reader/MathML.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use DOMElement;
77
use DOMNode;
88
use DOMXPath;
9-
use Exception;
109
use PhpOffice\Math\Element;
10+
use PhpOffice\Math\Exception\InvalidInputException;
11+
use PhpOffice\Math\Exception\NotImplementedException;
1112
use PhpOffice\Math\Math;
1213

1314
class MathML implements ReaderInterface
@@ -74,15 +75,19 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
7475
$nodeValue = trim($nodeElement->nodeValue);
7576
switch ($nodeElement->nodeName) {
7677
case 'mfrac':
77-
$element = new Element\Fraction();
7878
$nodeList = $this->xpath->query('*', $nodeElement);
7979
if ($nodeList && $nodeList->length == 2) {
80-
$element
81-
->setNumerator($this->getElement($nodeList->item(0)))
82-
->setDenominator($this->getElement($nodeList->item(1)));
80+
return new Element\Fraction(
81+
$this->getElement($nodeList->item(0)),
82+
$this->getElement($nodeList->item(1))
83+
);
8384
}
8485

85-
return $element;
86+
throw new InvalidInputException(sprintf(
87+
'%s : The tag `%s` has not two subelements',
88+
__METHOD__,
89+
$nodeElement->nodeName
90+
));
8691
case 'mi':
8792
return new Element\Identifier($nodeValue);
8893
case 'mn':
@@ -105,19 +110,23 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
105110
case 'mrow':
106111
return new Element\Row();
107112
case 'msup':
108-
$element = new Element\Superscript();
109113
$nodeList = $this->xpath->query('*', $nodeElement);
110114
if ($nodeList && $nodeList->length == 2) {
111-
$element
112-
->setBase($this->getElement($nodeList->item(0)))
113-
->setSuperscript($this->getElement($nodeList->item(1)));
115+
return new Element\Superscript(
116+
$this->getElement($nodeList->item(0)),
117+
$this->getElement($nodeList->item(1))
118+
);
114119
}
115120

116-
return $element;
121+
throw new InvalidInputException(sprintf(
122+
'%s : The tag `%s` has not two subelements',
123+
__METHOD__,
124+
$nodeElement->nodeName
125+
));
117126
case 'semantics':
118127
return new Element\Semantics();
119128
default:
120-
throw new Exception(sprintf(
129+
throw new NotImplementedException(sprintf(
121130
'%s : The tag `%s` is not implemented',
122131
__METHOD__,
123132
$nodeElement->nodeName

src/Math/Reader/OfficeMathML.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use DOMDocument;
66
use DOMNode;
77
use DOMXPath;
8-
use Exception;
98
use PhpOffice\Math\Element;
9+
use PhpOffice\Math\Exception\InvalidInputException;
10+
use PhpOffice\Math\Exception\NotImplementedException;
1011
use PhpOffice\Math\Math;
1112

1213
class OfficeMathML implements ReaderInterface
@@ -66,29 +67,40 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
6667
{
6768
switch ($nodeElement->nodeName) {
6869
case 'm:f':
69-
$element = new Element\Fraction();
7070
// Numerator
7171
$nodeNumerator = $this->xpath->query('m:num/m:r/m:t', $nodeElement);
7272
if ($nodeNumerator && $nodeNumerator->length == 1) {
7373
$value = $nodeNumerator->item(0)->nodeValue;
7474
if (is_numeric($value)) {
75-
$element->setNumerator(new Element\Numeric(floatval($value)));
75+
$numerator = new Element\Numeric(floatval($value));
7676
} else {
77-
$element->setNumerator(new Element\Identifier($value));
77+
$numerator = new Element\Identifier($value);
7878
}
79+
} else {
80+
throw new InvalidInputException(sprintf(
81+
'%s : The tag `%s` has no numerator defined',
82+
__METHOD__,
83+
$nodeElement->nodeName
84+
));
7985
}
8086
// Denominator
8187
$nodeDenominator = $this->xpath->query('m:den/m:r/m:t', $nodeElement);
8288
if ($nodeDenominator && $nodeDenominator->length == 1) {
8389
$value = $nodeDenominator->item(0)->nodeValue;
8490
if (is_numeric($value)) {
85-
$element->setDenominator(new Element\Numeric(floatval($value)));
91+
$denominator = new Element\Numeric(floatval($value));
8692
} else {
87-
$element->setDenominator(new Element\Identifier($value));
93+
$denominator = new Element\Identifier($value);
8894
}
95+
} else {
96+
throw new InvalidInputException(sprintf(
97+
'%s : The tag `%s` has no denominator defined',
98+
__METHOD__,
99+
$nodeElement->nodeName
100+
));
89101
}
90102

91-
return $element;
103+
return new Element\Fraction($numerator, $denominator);
92104
case 'm:r':
93105
$nodeText = $this->xpath->query('m:t', $nodeElement);
94106
if ($nodeText && $nodeText->length == 1) {
@@ -103,11 +115,15 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
103115
return new Element\Identifier($value);
104116
}
105117

106-
return new Element\Identifier('');
118+
throw new InvalidInputException(sprintf(
119+
'%s : The tag `%s` has no tag `m:t` defined',
120+
__METHOD__,
121+
$nodeElement->nodeName
122+
));
107123
case 'm:oMath':
108124
return new Element\Row();
109125
default:
110-
throw new Exception(sprintf(
126+
throw new NotImplementedException(sprintf(
111127
'%s : The tag `%s` is not implemented',
112128
__METHOD__,
113129
$nodeElement->nodeName

0 commit comments

Comments
 (0)