Passed
Push — master ( c380b2...9239b3 )
by Adrien
10:06
created

SubTotalTest::testHiddenSUBTOTAL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 45
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 51
rs 9.2

1 Method

Rating   Name   Duplication   Size   Complexity  
A SubTotalTest::providerSUBTOTALHIDDEN() 0 3 1

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 PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
4
5
class SubTotalTest extends AllSetupTeardown
6
{
7
    /**
8
     * @dataProvider providerSUBTOTAL
9
     *
10
     * @param mixed $expectedResult
11
     * @param mixed $type expect an integer
12
     */
13
    public function testSubtotal($expectedResult, $type): void
14
    {
15
        $this->mightHaveException($expectedResult);
16
        $sheet = $this->sheet;
17
        $sheet->fromArray([[0], [1], [1], [2], [3], [5], [8], [13], [21], [34], [55], [89]], null, 'A1', true);
18
        $maxCol = $sheet->getHighestColumn();
19
        $maxRow = $sheet->getHighestRow();
20
        $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
21
        $result = $sheet->getCell('D2')->getCalculatedValue();
22
        self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
23
    }
24
25
    public function providerSUBTOTAL()
26
    {
27
        return require 'tests/data/Calculation/MathTrig/SUBTOTAL.php';
28
    }
29
30
    /**
31
     * @dataProvider providerSUBTOTAL
32
     *
33
     * @param mixed $expectedResult
34
     * @param mixed $type expect an integer
35
     */
36
    public function testSubtotalColumnHidden($expectedResult, $type): void
37
    {
38
        // Hidden columns don't affect calculation, only hidden rows
39
        $this->mightHaveException($expectedResult);
40
        $sheet = $this->sheet;
41
        $sheet->fromArray([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89], null, 'A1', true);
42
        $maxCol = $sheet->getHighestColumn();
43
        $maxRow = $sheet->getHighestRow();
44
        $hiddenColumns = [
45
            'A' => false,
46
            'B' => true,
47
            'C' => false,
48
            'D' => true,
49
            'E' => false,
50
            'F' => false,
51
            'G' => false,
52
            'H' => true,
53
            'I' => false,
54
            'J' => true,
55
            'K' => true,
56
            'L' => false,
57
        ];
58
        foreach ($hiddenColumns as $col => $hidden) {
59
            $sheet->getColumnDimension($col)->setVisible($hidden);
60
        }
61
        $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
62
        $result = $sheet->getCell('D2')->getCalculatedValue();
63
        self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
64
    }
65
66
    /**
67
     * @dataProvider providerSUBTOTALHIDDEN
68
     *
69
     * @param mixed $expectedResult
70
     * @param mixed $type expect an integer
71
     */
72
    public function testSubtotalRowHidden($expectedResult, $type): void
73
    {
74
        $this->mightHaveException($expectedResult);
75
        $sheet = $this->sheet;
76
        $sheet->fromArray([[0], [1], [1], [2], [3], [5], [8], [13], [21], [34], [55], [89]], null, 'A1', true);
77
        $maxCol = $sheet->getHighestColumn();
78
        $maxRow = $sheet->getHighestRow();
79
        $visibleRows = [
80
            '1' => false,
81
            '2' => true,
82
            '3' => false,
83
            '4' => true,
84
            '5' => false,
85
            '6' => false,
86
            '7' => false,
87
            '8' => true,
88
            '9' => false,
89
            '10' => true,
90
            '11' => true,
91
            '12' => false,
92
        ];
93
        foreach ($visibleRows as $row => $visible) {
94
            $sheet->getRowDimension($row)->setVisible($visible);
95
        }
96
        $sheet->getCell('D2')->setValue("=SUBTOTAL($type, A1:$maxCol$maxRow)");
97
        $result = $sheet->getCell('D2')->getCalculatedValue();
98
        self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
99
    }
100
101
    public function providerSUBTOTALHIDDEN()
102
    {
103
        return require 'tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php';
104
    }
105
106
    public function testSubtotalNested(): void
107
    {
108
        $sheet = $this->sheet;
109
        $sheet->fromArray(
110
            [
111
                [123],
112
                [234],
113
                ['=SUBTOTAL(1,A1:A2)'],
114
                ['=ROMAN(SUBTOTAL(1, A1:A2))'],
115
                ['This is text containing "=" and "SUBTOTAL("'],
116
                ['=AGGREGATE(1, 0, A1:A2)'],
117
                ['=SUM(2, 3)'],
118
            ],
119
            null,
120
            'A1',
121
            true
122
        );
123
        $maxCol = $sheet->getHighestColumn();
124
        $maxRow = $sheet->getHighestRow();
125
        $sheet->getCell('H1')->setValue("=SUBTOTAL(9, A1:$maxCol$maxRow)");
126
        self::assertEquals(362, $sheet->getCell('H1')->getCalculatedValue());
127
    }
128
}
129