Implemented price and tests

This commit is contained in:
Kevin Frantz 2018-07-14 12:02:33 +02:00
parent 523d8f68ec
commit ac7d0aae21
3 changed files with 90 additions and 5 deletions

View File

@ -2,6 +2,7 @@
namespace entity\price; namespace entity\price;
use entity\currency\CurrencyInterface; use entity\currency\CurrencyInterface;
use entity\currency\Euro;
/** /**
* *
@ -10,14 +11,43 @@ use entity\currency\CurrencyInterface;
*/ */
final class Price implements PriceInterface final class Price implements PriceInterface
{ {
/**
*
* @var int|null
*/
private $tax;
/**
*
* @var int
*/
private $netto;
public function getGross(): CurrencyInterface public function getGross(): CurrencyInterface
{} {
$gross = new Euro();
$gross->setCents($this->netto->getCents()*(100+$this->tax)/100);
return $gross;
}
public function getNetto(): CurrencyInterface public function getNetto(): CurrencyInterface
{} {
return $this->netto;
}
public function setPrice(CurrencyInterface $price): void public function setPrice(CurrencyInterface $netto): void
{} {
$this->netto = $netto;
}
public function setTax(int $percent): void
{
$this->tax = $percent;
}
public function getTax(): int
{
return $this->tax;
}
} }

View File

@ -14,7 +14,7 @@ interface PriceInterface
* Sets the price * Sets the price
* @param CurrencyInterface $price * @param CurrencyInterface $price
*/ */
public function setPrice(CurrencyInterface $price):void; public function setPrice(CurrencyInterface $netto):void;
/** /**
* Returns the netto price * Returns the netto price
@ -27,5 +27,9 @@ interface PriceInterface
* @return int * @return int
*/ */
public function getGross():CurrencyInterface; public function getGross():CurrencyInterface;
public function setTax(int $percent):void;
public function getTax():int;
} }

View File

@ -0,0 +1,51 @@
<?php
namespace entity\price;
use PHPUnit\Framework\TestCase;
use entity\currency\Euro;
/**
*
* @author kevinfrantz
*
*/
class PriceTest extends TestCase
{
const NETTO = 100;
const GROSS = 119;
const TAX = 19;
/**
*
* @var Price
*/
protected $price;
/**
*
* @var Euro
*/
protected $nettoEuro;
protected function setUp():void{
$this->nettoEuro = new Euro();
$this->nettoEuro->setCents(100);
$this->price = new Price();
$this->price->setTax(self::TAX);
$this->price->setPrice($this->nettoEuro);
}
public function testNetto():void{
$this->assertEquals(self::NETTO, $this->price->getNetto()->getCents());
}
public function testGross():void{
$this->assertEquals(self::GROSS, $this->price->getGross()->getCents());
}
public function testTax():void{
$this->assertEquals(self::TAX, $this->price->getTax());
}
}