Completed
Push — master ( 9f49d7...5b6cd1 )
by Tobias
02:51
created

CalculatorSpecifiedPaymentsTest::payments1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 9.3571
c 0
b 0
f 0
cc 1
eloc 62
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Nyholm\EffectiveInterest\Test;
4
5
use Nyholm\EffectiveInterest\Calculator;
6
use PHPUnit\Framework\TestCase;
7
8
class CalculatorSpecifiedPaymentsTest extends TestCase
9
{
10
11
    public function testReadmeExample()
12
    {
13
14
        $principal = 100000;
15
        $payment = 2400;
16
        $guess = 0.03;
17
        $startDate = '2017-04-30';
18
        $calculator = new Calculator();
19
20
        $payments = [
21
            '2017-04-30' => $payment + 400,
22
            '2017-05-31' => $payment,
23
            '2017-06-30' => $payment,
24
            '2017-07-31' => $payment,
25
            '2017-08-31' => $payment,
26
            '2017-09-30' => $payment,
27
            '2017-10-31' => $payment,
28
            '2017-11-30' => $payment,
29
            '2017-12-31' => $payment,
30
            '2018-01-31' => $payment,
31
            '2018-02-28' => $payment,
32
            '2018-03-31' => $payment,
33
            '2018-04-30' => $payment,
34
            '2018-05-31' => $payment,
35
            '2018-06-30' => $payment,
36
            '2018-07-31' => $payment,
37
            '2018-08-31' => $payment,
38
            '2018-09-30' => $payment,
39
            '2018-10-31' => $payment,
40
            '2018-11-30' => $payment,
41
            '2018-12-31' => $payment,
42
            '2019-01-31' => $payment,
43
            '2019-02-28' => $payment,
44
            '2019-03-31' => $payment,
45
            '2019-04-30' => $payment,
46
            '2019-05-31' => $payment,
47
            '2019-06-30' => $payment,
48
            '2019-07-31' => $payment,
49
            '2019-08-31' => $payment,
50
            '2019-09-30' => $payment,
51
            '2019-10-31' => $payment,
52
            '2019-11-30' => $payment,
53
            '2019-12-31' => $payment,
54
            '2020-01-31' => $payment,
55
            '2020-02-28' => $payment,
56
            '2020-03-31' => 31200,
57
        ];
58
59
        $interest = $calculator->withSpecifiedPayments($principal, $startDate, $payments, $guess);
60
        $this->assertEquals(0.084870, $interest, 'Failed to calculate effective interest with specified payments.', 0.0001);
61
    }
62
63
    /**
64
     * @dataProvider generator
65
     */
66
    public function testWithSpecifiedPayments(float $correctValue, int $principal, string $startDate, array $payments, float $guess)
67
    {
68
        $calculator = new Calculator();
69
        $interest = $calculator->withSpecifiedPayments($principal, $startDate, $payments, $guess);
70
        $this->assertEquals($correctValue, $interest, 'Failed to calculate effective interest with specified payments.', 0.0001);
71
    }
72
73
    public function generator()
74
    {
75
        return [
76
            [0.0233, 336000, '2017-01-30', $this->payments0(), 0.02],
77
            [0.0805, 171920, '2017-01-30', $this->payments1(), 0.07],
78
            [0.1045, 52800, '2017-01-30', $this->payments2(), 0.09],
79
            [0.0549, 115000, '2017-01-30', $this->payments3(), 0.04],
80
        ];
81
    }
82
83
    /**
84
     *
85
     * @return array
86
     */
87 View Code Duplication
    private function payments0(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        return [
90
            '17-02-28' => 4607,
91
            '17-03-31' => 4012,
92
            '17-04-30' => 4012,
93
            '17-05-31' => 4012,
94
            '17-06-30' => 4012,
95
            '17-07-31' => 4012,
96
            '17-08-31' => 4012,
97
            '17-09-30' => 4012,
98
            '17-10-31' => 4012,
99
            '17-11-30' => 4012,
100
            '17-12-31' => 4012,
101
            '18-01-31' => 4012,
102
            '18-02-28' => 4012,
103
            '18-03-31' => 4012,
104
            '18-04-30' => 4012,
105
            '18-05-31' => 4012,
106
            '18-06-30' => 4012,
107
            '18-07-31' => 4012,
108
            '18-08-31' => 4012,
109
            '18-09-30' => 4012,
110
            '18-10-31' => 4012,
111
            '18-11-30' => 4012,
112
            '18-12-31' => 4012,
113
            '19-01-31' => 4012,
114
            '19-02-28' => 4012,
115
            '19-03-31' => 4012,
116
            '19-04-30' => 4012,
117
            '19-05-31' => 4012,
118
            '19-06-30' => 4012,
119
            '19-07-31' => 4012,
120
            '19-08-31' => 4012,
121
            '19-09-30' => 4012,
122
            '19-10-31' => 4012,
123
            '19-11-30' => 4012,
124
            '19-12-31' => 4012,
125
            '20-01-31' => 214012,
126
        ];
127
    }/**
128
     *
129
     * @return array
130
     */
131
    private function payments1(): array
132
    {
133
        return [
134
            '17-02-28' => 4050,
135
            '17-03-31' => 3455,
136
            '17-04-30' => 3455,
137
            '17-05-31' => 3455,
138
            '17-06-30' => 3455,
139
            '17-07-31' => 3455,
140
            '17-08-31' => 3455,
141
            '17-09-30' => 3455,
142
            '17-10-31' => 3455,
143
            '17-11-30' => 3455,
144
            '17-12-31' => 3455,
145
            '18-01-31' => 3455,
146
            '18-02-28' => 3455,
147
            '18-03-31' => 3455,
148
            '18-04-30' => 3455,
149
            '18-05-31' => 3455,
150
            '18-06-30' => 3455,
151
            '18-07-31' => 3455,
152
            '18-08-31' => 3455,
153
            '18-09-30' => 3455,
154
            '18-10-31' => 3455,
155
            '18-11-30' => 3455,
156
            '18-12-31' => 3455,
157
            '19-01-31' => 3455,
158
            '19-02-28' => 3455,
159
            '19-03-31' => 3455,
160
            '19-04-30' => 3455,
161
            '19-05-31' => 3455,
162
            '19-06-30' => 3455,
163
            '19-07-31' => 3455,
164
            '19-08-31' => 3455,
165
            '19-09-30' => 3455,
166
            '19-10-31' => 3455,
167
            '19-11-30' => 3455,
168
            '19-12-31' => 3455,
169
            '20-01-31' => 3455,
170
            '20-02-29' => 3455,
171
            '20-03-31' => 3455,
172
            '20-04-30' => 3455,
173
            '20-05-31' => 3455,
174
            '20-06-30' => 3455,
175
            '20-07-31' => 3455,
176
            '20-08-31' => 3455,
177
            '20-09-30' => 3455,
178
            '20-10-31' => 3455,
179
            '20-11-30' => 3455,
180
            '20-12-31' => 3455,
181
            '21-01-31' => 3455,
182
            '21-02-28' => 3455,
183
            '21-03-31' => 3455,
184
            '21-04-30' => 3455,
185
            '21-05-31' => 3455,
186
            '21-06-30' => 3455,
187
            '21-07-31' => 3455,
188
            '21-08-31' => 3455,
189
            '21-09-30' => 3455,
190
            '21-10-31' => 3455,
191
            '21-11-30' => 3455,
192
            '21-12-31' => 3455,
193
            '22-01-31' => 3455,
194
        ];
195
    }/**
196
     *
197
     * @return array
198
     */
199 View Code Duplication
    private function payments2(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201
        return [
202
            '17-02-28' => 2279,
203
            '17-03-31' => 1684,
204
            '17-04-30' => 1684,
205
            '17-05-31' => 1684,
206
            '17-06-30' => 1684,
207
            '17-07-31' => 1684,
208
            '17-08-31' => 1684,
209
            '17-09-30' => 1684,
210
            '17-10-31' => 1684,
211
            '17-11-30' => 1684,
212
            '17-12-31' => 1684,
213
            '18-01-31' => 1684,
214
            '18-02-28' => 1684,
215
            '18-03-31' => 1684,
216
            '18-04-30' => 1684,
217
            '18-05-31' => 1684,
218
            '18-06-30' => 1684,
219
            '18-07-31' => 1684,
220
            '18-08-31' => 1684,
221
            '18-09-30' => 1684,
222
            '18-10-31' => 1684,
223
            '18-11-30' => 1684,
224
            '18-12-31' => 1684,
225
            '19-01-31' => 1684,
226
            '19-02-28' => 1684,
227
            '19-03-31' => 1684,
228
            '19-04-30' => 1684,
229
            '19-05-31' => 1684,
230
            '19-06-30' => 1684,
231
            '19-07-31' => 1684,
232
            '19-08-31' => 1684,
233
            '19-09-30' => 1684,
234
            '19-10-31' => 1684,
235
            '19-11-30' => 1684,
236
            '19-12-31' => 1684,
237
            '20-01-31' => 1684,
238
        ];
239
    }/**
240
     *
241
     * @return array
242
     */
243
    private function payments3(): array
244
    {
245
        return [
246
            '17-02-28' => 2457,
247
            '17-03-31' => 1862,
248
            '17-04-30' => 1862,
249
            '17-05-31' => 1862,
250
            '17-06-30' => 1862,
251
            '17-07-31' => 1862,
252
            '17-08-31' => 1862,
253
            '17-09-30' => 1862,
254
            '17-10-31' => 1862,
255
            '17-11-30' => 1862,
256
            '17-12-31' => 1862,
257
            '18-01-31' => 1862,
258
            '18-02-28' => 1862,
259
            '18-03-31' => 1862,
260
            '18-04-30' => 1862,
261
            '18-05-31' => 1862,
262
            '18-06-30' => 1862,
263
            '18-07-31' => 1862,
264
            '18-08-31' => 1862,
265
            '18-09-30' => 1862,
266
            '18-10-31' => 1862,
267
            '18-11-30' => 1862,
268
            '18-12-31' => 1862,
269
            '19-01-31' => 1862,
270
            '19-02-28' => 1862,
271
            '19-03-31' => 1862,
272
            '19-04-30' => 1862,
273
            '19-05-31' => 1862,
274
            '19-06-30' => 1862,
275
            '19-07-31' => 1862,
276
            '19-08-31' => 1862,
277
            '19-09-30' => 1862,
278
            '19-10-31' => 1862,
279
            '19-11-30' => 1862,
280
            '19-12-31' => 1862,
281
            '20-01-31' => 1862,
282
            '20-02-29' => 1862,
283
            '20-03-31' => 1862,
284
            '20-04-30' => 1862,
285
            '20-05-31' => 1862,
286
            '20-06-30' => 1862,
287
            '20-07-31' => 1862,
288
            '20-08-31' => 1862,
289
            '20-09-30' => 1862,
290
            '20-10-31' => 1862,
291
            '20-11-30' => 1862,
292
            '20-12-31' => 1862,
293
            '21-01-31' => 1862,
294
            '21-02-28' => 1862,
295
            '21-03-31' => 1862,
296
            '21-04-30' => 1862,
297
            '21-05-31' => 1862,
298
            '21-06-30' => 1862,
299
            '21-07-31' => 1862,
300
            '21-08-31' => 1862,
301
            '21-09-30' => 1862,
302
            '21-10-31' => 1862,
303
            '21-11-30' => 1862,
304
            '21-12-31' => 1862,
305
            '22-01-31' => 1862,
306
            '22-02-28' => 1862,
307
            '22-03-31' => 1862,
308
            '22-04-30' => 1862,
309
            '22-05-31' => 1862,
310
            '22-06-30' => 1862,
311
            '22-07-31' => 1862,
312
            '22-08-31' => 1862,
313
            '22-09-30' => 1862,
314
            '22-10-31' => 1862,
315
            '22-11-30' => 1862,
316
            '22-12-31' => 1862,
317
            '23-01-31' => 1862,
318
        ];
319
    }
320
}
321