Failed Conditions
Pull Request — master (#6)
by Kauri
02:35
created

testConstructorArgumentCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Kauri\Loan\Test;
4
5
6
use Kauri\Loan\InterestAmountCalculator;
7
use Kauri\Loan\PaymentAmountCalculator\AnnuityPaymentAmountCalculator;
8
use Kauri\Loan\PaymentAmountCalculator\EqualPrincipalPaymentAmountCalculator;
9
use Kauri\Loan\PaymentAmountCalculatorInterface;
10
use PHPUnit\Framework\TestCase;
11
12
class PaymentAmountCalculatorTest extends TestCase
13
{
14
    /**
15
     * @dataProvider loanData
16
     * @param $presentValue
17
     * @param $yearlyInterestRate
18
     * @param $periodsLengths
19
     * @param $expected
20
     * @param PaymentAmountCalculatorInterface $calculator
21
     * @param int $futureValue
22
     */
23
    public function testGetPaymentAmount(
24
        $presentValue,
25
        $yearlyInterestRate,
26
        $periodsLengths,
27
        $expected,
28
        PaymentAmountCalculatorInterface $calculator,
29
        $futureValue = 0
30
    ) {
31
        $paymentAmounts = $calculator->getPaymentAmounts($periodsLengths, $presentValue, $yearlyInterestRate,
32
            $futureValue);
33
        $paymentAmount = current($paymentAmounts);
34
35
        $this->assertEquals($expected, round($paymentAmount, 2));
36
    }
37
38
    /**
39
     * @expectedException \ArgumentCountError
40
     */
41
    public function testConstructorArgumentCount()
42
    {
43
        new AnnuityPaymentAmountCalculator();
0 ignored issues
show
Bug introduced by
The call to AnnuityPaymentAmountCalculator::__construct() misses a required argument $interestAmountCalculator.

This check looks for function calls that miss required arguments.

Loading history...
44
    }
45
46
    /**
47
     * @expectedException \TypeError
48
     */
49
    public function testConstructorArgumentType()
50
    {
51
        new AnnuityPaymentAmountCalculator(false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a object<Kauri\Loan\Intere...untCalculatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function loanData(): array
58
    {
59
        $interestCalculator = new InterestAmountCalculator();
60
61
        $annuityCalculator = new AnnuityPaymentAmountCalculator($interestCalculator);
62
        $equalCalculator = new EqualPrincipalPaymentAmountCalculator($interestCalculator);
63
64
        return [
65
            [100, 0, [1 => 30], 100, $annuityCalculator],
66
            [300, 0, [1 => 30, 30, 30], 100, $annuityCalculator],
67
            [100, 360, [1 => 30], 130, $annuityCalculator],
68
            [200, 360, [1 => 30, 30], 146.96, $annuityCalculator],
69
            [200, 360, [1 => 30, 31], 147.93, $annuityCalculator],
70
            [200, 360, [1 => 31, 31], 148.58, $annuityCalculator],
71
72
            [100, 0, [1 => 30], 100, $equalCalculator],
73
            [300, 0, [1 => 30, 30, 30], 100, $equalCalculator],
74
            [300, 0, [1 => 30, 30, 30], 50, $equalCalculator, 150],
75
            [300, 360, [1 => 30, 30, 30], 140, $equalCalculator, 150],
76
        ];
77
    }
78
}
79