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 32703f5
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 73 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 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
22 changes: 0 additions & 22 deletions src/Math/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,4 @@

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;
}
}
61 changes: 61 additions & 0 deletions tests/Math/Element/AbstractGroupElementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

use PhpOffice\Math\Element;
use PhpOffice\Math\Element\AbstractGroupElement;
use PhpOffice\Math\Math;
use PhpOffice\Math\Reader\MathML;
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());
}
}
62 changes: 62 additions & 0 deletions tests/Math/Element/FractionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

use PhpOffice\Math\Element;
use PhpOffice\Math\Element\Fraction;
use PhpOffice\Math\Math;
use PhpOffice\Math\Reader\MathML;
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());
}
}
23 changes: 23 additions & 0 deletions tests/Math/Element/IdentifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

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

class IdentifierTest extends TestCase
{
/**
* @covers \Identifier::__construct
* @covers \Identifier::getValue
*/
public function testConstruct(): void
{
$operator = new Identifier('x');

$this->assertEquals('x', $operator->getValue());
}
}
23 changes: 23 additions & 0 deletions tests/Math/Element/NumericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

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

class NumericTest extends TestCase
{
/**
* @covers \Numeric::__construct
* @covers \Numeric::getValue
*/
public function testConstruct(): void
{
$numeric = new Numeric(2);

$this->assertEquals(2, $numeric->getValue());
}
}
23 changes: 23 additions & 0 deletions tests/Math/Element/OperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

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

class OperatorTest extends TestCase
{
/**
* @covers \Operator::__construct
* @covers \Operator::getValue
*/
public function testConstruct(): void
{
$operator = new Operator('+');

$this->assertEquals('+', $operator->getValue());
}
}
45 changes: 45 additions & 0 deletions tests/Math/Element/SemanticsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Tests\PhpOffice\Math\Element;

use PhpOffice\Math\Element;
use PhpOffice\Math\Element\Semantics;
use PhpOffice\Math\Math;
use PhpOffice\Math\Reader\MathML;
use PHPUnit\Framework\TestCase;

class SemanticsTest extends TestCase
{
/**
* @covers \Semantics::__construct
*/
public function testConstruct(): void
{
$semantics = new Semantics();

$this->assertIsArray($semantics->getAnnotations());
$this->assertCount(0, $semantics->getAnnotations());
}

/**
* @covers \Superscript::addAnnotation
* @covers \Superscript::getAnnotation
* @covers \Superscript::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'));
}
}
Loading

0 comments on commit 32703f5

Please sign in to comment.