| 1 |
<?php |
| 2 |
namespace Payplug; |
| 3 |
use Payplug; |
| 4 |
|
| 5 |
/** |
| 6 |
* @group unit |
| 7 |
* @group ci |
| 8 |
* @group recommended |
| 9 |
*/ |
| 10 |
class OneySimulationTest extends \PHPUnit_Framework_TestCase |
| 11 |
{ |
| 12 |
private $_requestMock; |
| 13 |
|
| 14 |
protected function setUp() |
| 15 |
{ |
| 16 |
$this->_configuration = new Payplug\Payplug('abc'); |
| 17 |
Payplug\Payplug::setDefaultConfiguration($this->_configuration); |
| 18 |
|
| 19 |
$this->_requestMock = $this->getMock('\Payplug\Core\IHttpRequest'); |
| 20 |
Core\HttpClient::$REQUEST_HANDLER = $this->_requestMock; |
| 21 |
} |
| 22 |
|
| 23 |
public function testGetOneyPaymentSimulation() |
| 24 |
{ |
| 25 |
$data = array( |
| 26 |
'amount' => 50500, |
| 27 |
'country' => 'FR', |
| 28 |
'operations' => array( |
| 29 |
'x3_with_fees', |
| 30 |
'x4_with_fees', |
| 31 |
), |
| 32 |
); |
| 33 |
|
| 34 |
$response = array( |
| 35 |
'x3_with_fees' => array( |
| 36 |
'effective_annual_percentage_rate' => 19.25, |
| 37 |
'nominal_annual_percentage_rate' => 17.74, |
| 38 |
'total_cost' => 732, |
| 39 |
'installments' => array( |
| 40 |
array ( |
| 41 |
'date' => '2020-03-06T01:00:00.000Z', |
| 42 |
'amount' => 16834, |
| 43 |
), |
| 44 |
array ( |
| 45 |
'date' => '2020-04-06T00:00:00.000Z', |
| 46 |
'amount' => 16833, |
| 47 |
), |
| 48 |
), |
| 49 |
'down_payment_amount' => 17565, |
| 50 |
), |
| 51 |
'x4_with_fees' => array( |
| 52 |
'effective_annual_percentage_rate' => 19.25, |
| 53 |
'nominal_annual_percentage_rate' => 17.74, |
| 54 |
'total_cost' => 732, |
| 55 |
'installments' => array( |
| 56 |
array ( |
| 57 |
'date' => '2020-03-06T01:00:00.000Z', |
| 58 |
'amount' => 16834, |
| 59 |
), |
| 60 |
array ( |
| 61 |
'date' => '2020-04-06T00:00:00.000Z', |
| 62 |
'amount' => 16833, |
| 63 |
), |
| 64 |
array ( |
| 65 |
'date' => '2020-05-06T00:00:00.000Z', |
| 66 |
'amount' => 16833, |
| 67 |
), |
| 68 |
), |
| 69 |
'down_payment_amount' => 17565, |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
$this->_requestMock |
| 74 |
->expects($this->once()) |
| 75 |
->method('exec') |
| 76 |
->will($this->returnValue(json_encode($response))); |
| 77 |
|
| 78 |
$this->_requestMock |
| 79 |
->expects($this->any()) |
| 80 |
->method('getinfo') |
| 81 |
->will($this->returnCallback(function($option) { |
| 82 |
switch($option) { |
| 83 |
case CURLINFO_HTTP_CODE: |
| 84 |
return 201; |
| 85 |
} |
| 86 |
return null; |
| 87 |
})); |
| 88 |
|
| 89 |
$simulations = OneySimulation::getSimulations($data); |
| 90 |
|
| 91 |
// check 3x with fees |
| 92 |
$this->assertArrayHasKey('x3_with_fees', $simulations); |
| 93 |
if (isset($simulations['x3_with_fees']) && $simulations['x3_with_fees']) { |
| 94 |
$x3_with_fees = $simulations['x3_with_fees']; |
| 95 |
|
| 96 |
$this->assertEquals(true, is_array($x3_with_fees)); |
| 97 |
$this->assertEquals(19.25, $x3_with_fees['effective_annual_percentage_rate']); |
| 98 |
$this->assertEquals(17.74, $x3_with_fees['nominal_annual_percentage_rate']); |
| 99 |
$this->assertEquals(732, $x3_with_fees['total_cost']); |
| 100 |
|
| 101 |
$installments_x3 = $x3_with_fees['installments']; |
| 102 |
|
| 103 |
$this->assertEquals(2, count($installments_x3)); |
| 104 |
$this->assertEquals('2020-03-06T01:00:00.000Z', $installments_x3[0]['date']); |
| 105 |
$this->assertEquals(16834, $installments_x3[0]['amount']); |
| 106 |
$this->assertEquals('2020-04-06T00:00:00.000Z', $installments_x3[1]['date']); |
| 107 |
$this->assertEquals(16833, $installments_x3[1]['amount']); |
| 108 |
|
| 109 |
$this->assertEquals(17565, $x3_with_fees['down_payment_amount']); |
| 110 |
} |
| 111 |
|
| 112 |
// check 4x with fees |
| 113 |
$this->assertArrayHasKey('x4_with_fees', $simulations); |
| 114 |
if (isset($simulations['x4_with_fees']) && $simulations['x4_with_fees']) { |
| 115 |
$x4_with_fees = $simulations['x4_with_fees']; |
| 116 |
|
| 117 |
$this->assertEquals(true, is_array($x4_with_fees)); |
| 118 |
$this->assertEquals(19.25, $x4_with_fees['effective_annual_percentage_rate']); |
| 119 |
$this->assertEquals(17.74, $x4_with_fees['nominal_annual_percentage_rate']); |
| 120 |
$this->assertEquals(732, $x4_with_fees['total_cost']); |
| 121 |
|
| 122 |
$installments_x4 = $x4_with_fees['installments']; |
| 123 |
|
| 124 |
$this->assertEquals(3, count($installments_x4)); |
| 125 |
|
| 126 |
$this->assertEquals('2020-03-06T01:00:00.000Z', $installments_x4[0]['date']); |
| 127 |
$this->assertEquals(16834, $installments_x4[0]['amount']); |
| 128 |
$this->assertEquals('2020-04-06T00:00:00.000Z', $installments_x4[1]['date']); |
| 129 |
$this->assertEquals(16833, $installments_x4[1]['amount']); |
| 130 |
$this->assertEquals('2020-05-06T00:00:00.000Z', $installments_x4[2]['date']); |
| 131 |
$this->assertEquals(16833, $installments_x4[2]['amount']); |
| 132 |
|
| 133 |
$this->assertEquals(17565, $x4_with_fees['down_payment_amount']); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|