MathsHelperTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 109
c 2
b 0
f 1
dl 0
loc 216
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testRound() 0 60 1
A testRoundUp() 0 60 1
A calculateTax() 0 3 1
A testRoundDown() 0 60 1
1
<?php
2
3
namespace SilverCommerce\TaxAdmin\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverCommerce\TaxAdmin\Helpers\MathsHelper;
7
8
/**
9
 * Test functionality of postage extension
10
 *
11
 */
12
class MathsHelperTest extends SapphireTest
13
{
14
    protected $price_one = 82.83;
15
16
    protected $price_two = 49.62;
17
18
    protected $price_three = 28.34;
19
20
    /**
21
     * Quickly calculate tax at 20%
22
     *
23
     * @return float
24
     */
25
    protected function calculateTax($value)
26
    {
27
        return ($value / 100 * 20);
28
    }
29
30
    /**
31
     * Test that standard rounding works to different decimals.
32
     *
33
     */
34
    public function testRound()
35
    {
36
        $this->assertEquals(
37
            17,
38
            MathsHelper::round($this->calculateTax($this->price_one))
39
        );
40
41
        $this->assertEquals(
42
            16.6,
43
            MathsHelper::round($this->calculateTax($this->price_one), 1)
44
        );
45
46
        $this->assertEquals(
47
            16.57,
48
            MathsHelper::round($this->calculateTax($this->price_one), 2)
49
        );
50
51
        $this->assertEquals(
52
            16.566,
53
            MathsHelper::round($this->calculateTax($this->price_one), 3)
54
        );
55
56
        $this->assertEquals(
57
            10,
58
            MathsHelper::round($this->calculateTax($this->price_two))
59
        );
60
61
        $this->assertEquals(
62
            9.9,
63
            MathsHelper::round($this->calculateTax($this->price_two), 1)
64
        );
65
66
        $this->assertEquals(
67
            9.92,
68
            MathsHelper::round($this->calculateTax($this->price_two), 2)
69
        );
70
71
        $this->assertEquals(
72
            9.924,
73
            MathsHelper::round($this->calculateTax($this->price_two), 3)
74
        );
75
76
        $this->assertEquals(
77
            6,
78
            MathsHelper::round($this->calculateTax($this->price_three))
79
        );
80
81
        $this->assertEquals(
82
            5.7,
83
            MathsHelper::round($this->calculateTax($this->price_three), 1)
84
        );
85
86
        $this->assertEquals(
87
            5.67,
88
            MathsHelper::round($this->calculateTax($this->price_three), 2)
89
        );
90
91
        $this->assertEquals(
92
            5.668,
93
            MathsHelper::round($this->calculateTax($this->price_three), 3)
94
        );
95
    }
96
97
    /**
98
     * Test forcing rounding down
99
     *
100
     */
101
    public function testRoundDown()
102
    {
103
        $this->assertEquals(
104
            16,
105
            MathsHelper::round_down($this->calculateTax($this->price_one))
106
        );
107
108
        $this->assertEquals(
109
            16.5,
110
            MathsHelper::round_down($this->calculateTax($this->price_one), 1)
111
        );
112
113
        $this->assertEquals(
114
            16.56,
115
            MathsHelper::round_down($this->calculateTax($this->price_one), 2)
116
        );
117
118
        $this->assertEquals(
119
            16.566,
120
            MathsHelper::round_down($this->calculateTax($this->price_one), 3)
121
        );
122
123
        $this->assertEquals(
124
            9,
125
            MathsHelper::round_down($this->calculateTax($this->price_two))
126
        );
127
128
        $this->assertEquals(
129
            9.9,
130
            MathsHelper::round_down($this->calculateTax($this->price_two), 1)
131
        );
132
133
        $this->assertEquals(
134
            9.92,
135
            MathsHelper::round_down($this->calculateTax($this->price_two), 2)
136
        );
137
138
        $this->assertEquals(
139
            9.924,
140
            MathsHelper::round_down($this->calculateTax($this->price_two), 3)
141
        );
142
143
        $this->assertEquals(
144
            5,
145
            MathsHelper::round_down($this->calculateTax($this->price_three))
146
        );
147
148
        $this->assertEquals(
149
            5.6,
150
            MathsHelper::round_down($this->calculateTax($this->price_three), 1)
151
        );
152
153
        $this->assertEquals(
154
            5.66,
155
            MathsHelper::round_down($this->calculateTax($this->price_three), 2)
156
        );
157
158
        $this->assertEquals(
159
            5.668,
160
            MathsHelper::round_down($this->calculateTax($this->price_three), 3)
161
        );
162
    }
163
164
    /**
165
     * Test forcing rounding up.
166
     *
167
     */
168
    public function testRoundUp()
169
    {
170
        $this->assertEquals(
171
            17,
172
            MathsHelper::round_up($this->calculateTax($this->price_one))
173
        );
174
175
        $this->assertEquals(
176
            16.6,
177
            MathsHelper::round_up($this->calculateTax($this->price_one), 1)
178
        );
179
180
        $this->assertEquals(
181
            16.57,
182
            MathsHelper::round_up($this->calculateTax($this->price_one), 2)
183
        );
184
185
        $this->assertEquals(
186
            16.566,
187
            MathsHelper::round_up($this->calculateTax($this->price_one), 3)
188
        );
189
190
        $this->assertEquals(
191
            10,
192
            MathsHelper::round_up($this->calculateTax($this->price_two))
193
        );
194
195
        $this->assertEquals(
196
            10,
197
            MathsHelper::round_up($this->calculateTax($this->price_two), 1)
198
        );
199
200
        $this->assertEquals(
201
            9.93,
202
            MathsHelper::round_up($this->calculateTax($this->price_two), 2)
203
        );
204
205
        $this->assertEquals(
206
            9.924,
207
            MathsHelper::round_up($this->calculateTax($this->price_two), 3)
208
        );
209
210
        $this->assertEquals(
211
            6,
212
            MathsHelper::round_up($this->calculateTax($this->price_three))
213
        );
214
215
        $this->assertEquals(
216
            5.7,
217
            MathsHelper::round_up($this->calculateTax($this->price_three), 1)
218
        );
219
220
        $this->assertEquals(
221
            5.67,
222
            MathsHelper::round_up($this->calculateTax($this->price_three), 2)
223
        );
224
225
        $this->assertEquals(
226
            5.668,
227
            MathsHelper::round_up($this->calculateTax($this->price_three), 3)
228
        );
229
    }
230
}
231