Skip to content

Commit

Permalink
added final tests for TaxManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorchi committed Mar 15, 2018
1 parent b48562f commit b952f27
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 37 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Very short description of the package

[![Latest Version on Packagist](https://img.shields.io/packagist/v/feimx/Tax.svg?style=flat-square)](https://packagist.org/packages/feimx/Tax)
[![Build Status](https://img.shields.io/travis/feimx/Tax/master.svg?style=flat-square)](https://travis-ci.org/feimx/Tax)
[![Quality Score](https://img.shields.io/scrutinizer/g/feimx/Tax.svg?style=flat-square)](https://scrutinizer-ci.com/g/feimx/Tax)
[![Total Downloads](https://img.shields.io/packagist/dt/feimx/Tax.svg?style=flat-square)](https://packagist.org/packages/feimx/Tax)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/feimx/tax.svg?style=flat-square)](https://packagist.org/packages/feimx/tax)
[![Build Status](https://img.shields.io/travis/feimx/tax/master.svg?style=flat-square)](https://travis-ci.org/feimx/tax)
[![Quality Score](https://img.shields.io/scrutinizer/g/feimx/tax.svg?style=flat-square)](https://scrutinizer-ci.com/g/feimx/tax)
[![Total Downloads](https://img.shields.io/packagist/dt/feimx/tax.svg?style=flat-square)](https://packagist.org/packages/feimx/tax)

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.

Expand All @@ -12,7 +12,7 @@ This is where your description should go. Try and limit it to a paragraph or two
You can install the package via composer:

```bash
composer require feimx/Tax
composer require feimx/tax
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/TaxContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface TaxContract
*
* @return float Percetage
*/
public function calculate($amount): float;
public function calculate($amount);
}
41 changes: 38 additions & 3 deletions src/TaxManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function addTax($tax, $retention = false)
$tax = new $className($retention);
}

if (! in_array($tax, $this->taxes)) {
if (!in_array($tax, $this->taxes)) {
$this->taxes[] = $tax;
}

Expand Down Expand Up @@ -68,7 +68,7 @@ public function addTaxes(...$taxes)
*
* @return float Total amount
*/
public function get()
public function total()
{
$total = 0;
foreach ($this->taxes as $tax) {
Expand All @@ -78,6 +78,28 @@ public function get()
return number_format($this->amount + $total, 6, '.', '');
}

/**
* Get a list of taxes with amount calculated.
*
* @return float Total amount
*/
public function get()
{
$taxes = array_map(function ($tax) {
return [
'tax' => $tax->name,
'amount' => $tax->calculate($this->amount),
];
}, $this->taxes);


return array_merge([
'amount' => $this->amount,
'total' => $this->total,
'taxes' => $taxes,
]);
}

/**
* @param string $tax
*
Expand All @@ -86,10 +108,23 @@ public function get()
public function stringToClassName($tax)
{
$className = 'FeiMx\\Tax\\Taxes\\'.strtoupper($tax);
if (! class_exists($className)) {
if (!class_exists($className)) {
throw new TaxErrorException("The tax '{$tax}' is not valid");
}

return $className;
}

public function __get($property)
{
if ('total' == $property) {
return $this->total();
}

if (property_exists($this, $property)) {
return $this->{$property};
}

throw new \Exception('Property not exists');
}
}
4 changes: 2 additions & 2 deletions src/Taxes/IEPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class IEPS extends Tax implements TaxContract
*
* @return float Percetage
*/
public function calculate($amount): float
public function calculate($amount)
{
return $amount * $this->percentage();
return number_format($amount * $this->percentage(), 6, '.', '');
}
}
4 changes: 2 additions & 2 deletions src/Taxes/ISR.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class ISR extends Tax implements TaxContract
*
* @return float Percetage
*/
public function calculate($amount): float
public function calculate($amount)
{
// return -(($amount * 0.16) / 3) * 2;
return $amount * $this->percentage();
return number_format($amount * $this->percentage(), 6, '.', '');
}
}
6 changes: 3 additions & 3 deletions src/Taxes/IVA.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class IVA extends Tax implements TaxContract
/**
* Calculate tax percentage of a given amount.
*
* @param int $amount Amount for aclculate
* @param int $amount Amount for calculated
*
* @return float Percetage
*/
public function calculate($amount): float
public function calculate($amount)
{
return $amount * $this->percentage();
return number_format($amount * $this->percentage(), 6, '.', '');
}
}
9 changes: 9 additions & 0 deletions src/Taxes/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ protected function parseTypePercentage($type = 'default')
{
return $this->retention ? 'retention' : $type;
}

public function __get($property)
{
if ('name' == $property) {
return $this->getTaxName();
}

throw new \Exception('Property not exists');
}
}
83 changes: 62 additions & 21 deletions tests/TaxManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ public function testCanCalculateIVA()
{
$taxManager = new TaxManager(100);
$taxManager->addTax('iva');
$this->assertEquals(116.000000, $taxManager->get());
$this->assertEquals(116.000000, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('iva');
$this->assertEquals(6345.200000, $taxManager->get());
$this->assertEquals(6345.200000, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('iva');
$this->assertEquals(2669.044000, $taxManager->get());
$this->assertEquals(2669.044000, $taxManager->total());
}

public function testCanCalculateIVARetention()
Expand All @@ -109,30 +109,30 @@ public function testCanCalculateIVARetention()

$taxManager = new TaxManager(100);
$taxManager->addTax('iva', $retention);
$this->assertEquals(84.000000, $taxManager->get());
$this->assertEquals(84.000000, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('iva', $retention);
$this->assertEquals(4594.800000, $taxManager->get());
$this->assertEquals(4594.800000, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('iva', $retention);
$this->assertEquals(1932.756000, $taxManager->get());
$this->assertEquals(1932.756000, $taxManager->total());
}

public function testCanCalculateIEPS()
{
$taxManager = new TaxManager(100);
$taxManager->addTax('ieps');
$this->assertEquals(108.000000, $taxManager->get());
$this->assertEquals(108.000000, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('ieps');
$this->assertEquals(5907.600000, $taxManager->get());
$this->assertEquals(5907.600000, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('ieps');
$this->assertEquals(2484.972000, $taxManager->get());
$this->assertEquals(2484.972000, $taxManager->total());
}

public function testCanCalculateIEPSRetention()
Expand All @@ -141,65 +141,106 @@ public function testCanCalculateIEPSRetention()

$taxManager = new TaxManager(100);
$taxManager->addTax('ieps', $retention);
$this->assertEquals(92.000000, $taxManager->get());
$this->assertEquals(92.000000, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('ieps', $retention);
$this->assertEquals(5032.400000, $taxManager->get());
$this->assertEquals(5032.400000, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('ieps', $retention);
$this->assertEquals(2116.828000, $taxManager->get());
$this->assertEquals(2116.828000, $taxManager->total());
}

public function testCanCalculateISR()
{
$taxManager = new TaxManager(100);
$taxManager->addTax('isr');
$this->assertEquals(89.333300, $taxManager->get());
$this->assertEquals(89.333300, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('isr');
$this->assertEquals(4886.531510, $taxManager->get());
$this->assertEquals(4886.531510, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('isr');
$this->assertEquals(2055.469900, $taxManager->get());
$this->assertEquals(2055.469900, $taxManager->total());
}

public function testCanCalculateMultipleTaxes()
{
$taxManager = new TaxManager(100);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$this->assertEquals(105.333300, $taxManager->get());
$this->assertEquals(105.333300, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$this->assertEquals(5761.731510, $taxManager->get());
$this->assertEquals(5761.731510, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$this->assertEquals(2423.613900, $taxManager->get());
$this->assertEquals(2423.613900, $taxManager->total());

$taxManager = new TaxManager(100);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$taxManager->addTax('ieps');
$this->assertEquals(113.333300, $taxManager->get());
$this->assertEquals(113.333300, $taxManager->total());

$taxManager = new TaxManager(5470);
$taxManager->addTax('iva');
$taxManager->addTax('ieps');
$taxManager->addTax('isr');
$this->assertEquals(6199.331510, $taxManager->get());
$this->assertEquals(6199.331510, $taxManager->total());

$taxManager = new TaxManager(2300.90);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$taxManager->addTax('ieps');
$this->assertEquals(2607.685900, $taxManager->get());
$this->assertEquals(2607.685900, $taxManager->total());
}

public function testGetTotalByProperty()
{
$taxManager = new TaxManager(100);
$taxManager->addTax('iva');
$this->assertEquals(116.000000, $taxManager->total);
}

public function testGetAListOfTaxesCalculated()
{
$taxManager = new TaxManager(100);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$data = [
'amount' => 100,
'total' => '105.333300',
'taxes' => [
[
'tax' => 'iva',
'amount' => '16.000000',
],
[
'tax' => 'isr',
'amount' => '-10.666700',
],
],
];
$this->assertSame($data, $taxManager->get());
}

public function testCombineTraslateAndRetentions()
{
$retention = true;

$taxManager = new TaxManager(100);
$taxManager->addTax('iva');
$taxManager->addTax('isr');
$taxManager->addTax('iva', $retention);
$taxManager->addTax('ieps', $retention);
$this->assertEquals(81.333300, $taxManager->total());
}
}

0 comments on commit b952f27

Please sign in to comment.