Skip to content

Commit

Permalink
- added JSON serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
uruba committed Aug 22, 2015
1 parent f741167 commit e4f6fc4
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 37 deletions.
22 changes: 22 additions & 0 deletions src/utils/serializers/JSONSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace FinanCalc\Utils\Serializers {

use FinanCalc\Interfaces\Serializer\SerializerInterface;

/**
* Class JSONSerializer
* @package FinanCalc\Utils\Serializers
*/
class JSONSerializer implements SerializerInterface {

/**
* @param array $inputArray
* @return mixed
*/
public static function serializeArray(array $inputArray)
{
return json_encode($inputArray, JSON_PRETTY_PRINT);
}
}
}
1 change: 0 additions & 1 deletion src/utils/serializers/XMLSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class XMLSerializer implements SerializerInterface {


/**
* @param array $inputArray
* @return mixed
Expand Down
225 changes: 189 additions & 36 deletions tests/SerializersTest.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
<?php

use FinanCalc\FinanCalc;
use FinanCalc\Utils\Serializers\JSONSerializer;
use FinanCalc\Utils\Serializers\XMLSerializer;

/**
* Class SerializersTest
*/
class SerializersTest extends PHPUnit_Framework_TestCase
{
// we will test the serializer on the most complex class with nested elements
// the example is the same as in the DebtAmortizatorTest
private $debtAmortizatorFactory;

public function testXMLSerializer() {
// we will test the serializer on the most complex class with nested elements
// the example is the same as in the DebtAmortizatorTest
$debtAmortizatorFactory = FinanCalc
::getInstance()
->getFactory('DebtAmortizatorFactory')
->newYearlyDebtAmortization(
40000,
6,
0.12);

$xml_output = $debtAmortizatorFactory
->getSerializedResult(new XMLSerializer());
$xml_output = $this
->debtAmortizatorFactory
->getSerializedResult(new XMLSerializer());

$xmlObject = new SimpleXMLElement($xml_output);

$round2DP = function ($roundedObject) {
return round((float)$roundedObject, 2);
};

// WE WILL TEST EACH AND EVERY PROPERTY OF THIS OBJECT

// Debt principal
Expand Down Expand Up @@ -61,7 +53,7 @@ public function testXMLSerializer() {
// Debt discount factor
$this->assertEquals(
"0.89",
$round2DP($xmlObject->debtDiscountFactor)
$this->round2DP($xmlObject->debtDiscountFactor)
);
// Debt duration
$this->assertEquals(
Expand All @@ -80,85 +72,246 @@ public function testXMLSerializer() {
$INDIVIDUAL_REPAYMENT = "9729.03";
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtSingleRepayment)
$this->round2DP($xmlObject->debtSingleRepayment)
);
// Debt repayments (principal part, interest part, total = principal part + interest part)
$this->assertEquals(
"4929.03",
$this->round2DP($xmlObject->debtRepayments->_1->principalAmount)
);
$this->assertEquals(
"4800.00",
$this->round2DP($xmlObject->debtRepayments->_1->interestAmount)
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($xmlObject->debtRepayments->_1->totalAmount)
);

$this->assertEquals(
"5520.51",
$this->round2DP($xmlObject->debtRepayments->_2->principalAmount)
);
$this->assertEquals(
"4208.52",
$this->round2DP($xmlObject->debtRepayments->_2->interestAmount)
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($xmlObject->debtRepayments->_2->totalAmount)
);

$this->assertEquals(
"6182.97",
$this->round2DP($xmlObject->debtRepayments->_3->principalAmount)
);
$this->assertEquals(
"3546.06",
$this->round2DP($xmlObject->debtRepayments->_3->interestAmount)
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($xmlObject->debtRepayments->_3->totalAmount)
);

$this->assertEquals(
"6924.93",
$this->round2DP($xmlObject->debtRepayments->_4->principalAmount)
);
$this->assertEquals(
"2804.10",
$this->round2DP($xmlObject->debtRepayments->_4->interestAmount)
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($xmlObject->debtRepayments->_4->totalAmount)
);

$this->assertEquals(
"7755.92",
$this->round2DP($xmlObject->debtRepayments->_5->principalAmount)
);
$this->assertEquals(
"1973.11",
$this->round2DP($xmlObject->debtRepayments->_5->interestAmount)
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($xmlObject->debtRepayments->_5->totalAmount)
);

$this->assertEquals(
"8686.63",
$this->round2DP($xmlObject->debtRepayments->_6->principalAmount)
);
$this->assertEquals(
"1042.4",
$this->round2DP($xmlObject->debtRepayments->_6->interestAmount)
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($xmlObject->debtRepayments->_6->totalAmount)
);
}

public function testJSONSerializer() {
$json_output = $this
->debtAmortizatorFactory
->getSerializedResult(new JSONSerializer());

$jsonObject = json_decode($json_output, true);

// WE WILL TEST EACH AND EVERY PROPERTY OF THIS OBJECT

// Debt principal
$this->assertEquals(
"40000",
$jsonObject["debtPrincipal"]
);
// Debt number of compounding periods
$this->assertEquals(
"6",
$jsonObject["debtNoOfCompoundingPeriods"]
);
// Debt length period
$this->assertEquals(
"1",
$jsonObject["debtPeriodLength"]["years"]
);
$this->assertEquals(
"12",
$jsonObject["debtPeriodLength"]["months"]
);
$this->assertEquals(
"360",
$jsonObject["debtPeriodLength"]["days"]
);
// Debt interest
$this->assertEquals(
"0.12",
$jsonObject["debtInterest"]
);
// Debt discount factor
$this->assertEquals(
"0.89",
$this->round2DP($jsonObject["debtDiscountFactor"])
);
// Debt duration
$this->assertEquals(
"6",
$jsonObject["debtDuration"]["years"]
);
$this->assertEquals(
"72",
$jsonObject["debtDuration"]["months"]
);
$this->assertEquals(
"2160",
$jsonObject["debtDuration"]["days"]
);
// Debt amount of single repayment
$INDIVIDUAL_REPAYMENT = "9729.03";
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$this->round2DP($jsonObject["debtSingleRepayment"])
);
// Debt repayments (principal part, interest part, total = principal part + interest part)
$this->assertEquals(
"4929.03",
$round2DP($xmlObject->debtRepayments->_1->principalAmount)
$this->round2DP($jsonObject["debtRepayments"]["1"]["principalAmount"])
);
$this->assertEquals(
"4800.00",
$round2DP($xmlObject->debtRepayments->_1->interestAmount)
$this->round2DP($jsonObject["debtRepayments"]["1"]["interestAmount"])
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtRepayments->_1->totalAmount)
$this->round2DP($jsonObject["debtRepayments"]["1"]["totalAmount"])
);

$this->assertEquals(
"5520.51",
$round2DP($xmlObject->debtRepayments->_2->principalAmount)
$this->round2DP($jsonObject["debtRepayments"]["2"]["principalAmount"])
);
$this->assertEquals(
"4208.52",
$round2DP($xmlObject->debtRepayments->_2->interestAmount)
$this->round2DP($jsonObject["debtRepayments"]["2"]["interestAmount"])
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtRepayments->_2->totalAmount)
$this->round2DP($jsonObject["debtRepayments"]["2"]["totalAmount"])
);

$this->assertEquals(
"6182.97",
$round2DP($xmlObject->debtRepayments->_3->principalAmount)
$this->round2DP($jsonObject["debtRepayments"]["3"]["principalAmount"])
);
$this->assertEquals(
"3546.06",
$round2DP($xmlObject->debtRepayments->_3->interestAmount)
$this->round2DP($jsonObject["debtRepayments"]["3"]["interestAmount"])
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtRepayments->_3->totalAmount)
$this->round2DP($jsonObject["debtRepayments"]["3"]["totalAmount"])
);

$this->assertEquals(
"6924.93",
$round2DP($xmlObject->debtRepayments->_4->principalAmount)
$this->round2DP($jsonObject["debtRepayments"]["4"]["principalAmount"])
);
$this->assertEquals(
"2804.10",
$round2DP($xmlObject->debtRepayments->_4->interestAmount)
$this->round2DP($jsonObject["debtRepayments"]["4"]["interestAmount"])
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtRepayments->_4->totalAmount)
$this->round2DP($jsonObject["debtRepayments"]["4"]["totalAmount"])
);

$this->assertEquals(
"7755.92",
$round2DP($xmlObject->debtRepayments->_5->principalAmount)
$this->round2DP($jsonObject["debtRepayments"]["5"]["principalAmount"])
);
$this->assertEquals(
"1973.11",
$round2DP($xmlObject->debtRepayments->_5->interestAmount)
$this->round2DP($jsonObject["debtRepayments"]["5"]["interestAmount"])
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtRepayments->_5->totalAmount)
$this->round2DP($jsonObject["debtRepayments"]["5"]["totalAmount"])
);

$this->assertEquals(
"8686.63",
$round2DP($xmlObject->debtRepayments->_6->principalAmount)
$this->round2DP($jsonObject["debtRepayments"]["6"]["principalAmount"])
);
$this->assertEquals(
"1042.4",
$round2DP($xmlObject->debtRepayments->_6->interestAmount)
$this->round2DP($jsonObject["debtRepayments"]["6"]["interestAmount"])
);
$this->assertEquals(
$INDIVIDUAL_REPAYMENT,
$round2DP($xmlObject->debtRepayments->_6->totalAmount)
$this->round2DP($jsonObject["debtRepayments"]["6"]["totalAmount"])
);
}

/**
* @param $roundedObject
* @return float
*/
private function round2DP($roundedObject) {
return round((float)$roundedObject, 2);
}

protected function setUp() {
$this->debtAmortizatorFactory = FinanCalc
::getInstance()
->getFactory('DebtAmortizatorFactory')
->newYearlyDebtAmortization(
40000,
6,
0.12);

parent::setUp();
}
}

0 comments on commit e4f6fc4

Please sign in to comment.