Failed Conditions
Pull Request — master (#4571)
by Owen
15:04
created

Dec2OctTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 57
dl 0
loc 113
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testDEC2OCTFrac() 0 16 1
A providerUnhappyDEC2OCT() 0 4 1
A testDEC2OCTUnhappyPath() 0 14 1
A testDec2OctArray() 0 8 1
A testDEC2OCTInWorksheet() 0 13 1
A providerDEC2OCT() 0 3 1
A testDEC2OCTOds() 0 7 2
A testDEC2OCTAsFormula() 0 10 1
A testDirectCallToDEC2OCT() 0 5 2
A providerDEC2OCTOds() 0 3 1
A providerDec2OctArray() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering;
6
7
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
8
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ConvertDecimal;
9
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
10
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
11
use PHPUnit\Framework\Attributes\DataProvider;
12
13
class Dec2OctTest extends AllSetupTeardown
14
{
15
    #[DataProvider('providerDEC2OCT')]
16
    public function testDirectCallToDEC2OCT(mixed $expectedResult, bool|float|int|string $value, ?int $digits = null): void
17
    {
18
        $result = ($digits === null) ? ConvertDecimal::toOctal($value) : ConvertDecimal::toOctal($value, $digits);
19
        self::assertSame($expectedResult, $result);
20
    }
21
22
    #[DataProvider('providerDEC2OCT')]
23
    public function testDEC2OCTAsFormula(mixed $expectedResult, mixed ...$args): void
24
    {
25
        $arguments = new FormulaArguments(...$args);
26
27
        $calculation = Calculation::getInstance();
28
        $formula = "=DEC2OCT({$arguments})";
29
30
        $result = $calculation->calculateFormula($formula);
31
        self::assertSame($expectedResult, $result);
32
    }
33
34
    #[DataProvider('providerDEC2OCT')]
35
    public function testDEC2OCTInWorksheet(mixed $expectedResult, mixed ...$args): void
36
    {
37
        $arguments = new FormulaArguments(...$args);
38
39
        $worksheet = $this->getSheet();
40
        $argumentCells = $arguments->populateWorksheet($worksheet);
41
        $formula = "=DEC2OCT({$argumentCells})";
42
43
        $result = $worksheet->setCellValue('A1', $formula)
44
            ->getCell('A1')
45
            ->getCalculatedValue();
46
        self::assertSame($expectedResult, $result);
47
    }
48
49
    public static function providerDEC2OCT(): array
50
    {
51
        return require 'tests/data/Calculation/Engineering/DEC2OCT.php';
52
    }
53
54
    #[DataProvider('providerUnhappyDEC2OCT')]
55
    public function testDEC2OCTUnhappyPath(string $expectedException, mixed ...$args): void
56
    {
57
        $arguments = new FormulaArguments(...$args);
58
59
        $worksheet = $this->getSheet();
60
        $argumentCells = $arguments->populateWorksheet($worksheet);
61
        $formula = "=DEC2OCT({$argumentCells})";
62
63
        $this->expectException(CalculationException::class);
64
        $this->expectExceptionMessage($expectedException);
65
        $worksheet->setCellValue('A1', $formula)
66
            ->getCell('A1')
67
            ->getCalculatedValue();
68
    }
69
70
    public static function providerUnhappyDEC2OCT(): array
71
    {
72
        return [
73
            ['Formula Error: Wrong number of arguments for DEC2OCT() function'],
74
        ];
75
    }
76
77
    #[DataProvider('providerDEC2OCTOds')]
78
    public function testDEC2OCTOds(mixed $expectedResult, bool|float|int|string $value, ?int $digits = null): void
79
    {
80
        $this->setOpenOffice();
81
82
        $result = ($digits === null) ? ConvertDecimal::toOctal($value) : ConvertDecimal::toOctal($value, $digits);
83
        self::assertSame($expectedResult, $result);
84
    }
85
86
    public static function providerDEC2OCTOds(): array
87
    {
88
        return require 'tests/data/Calculation/Engineering/DEC2OCTOpenOffice.php';
89
    }
90
91
    public function testDEC2OCTFrac(): void
92
    {
93
        $calculation = Calculation::getInstance();
94
        $formula = '=DEC2OCT(17.1)';
95
96
        $this->setGnumeric();
97
        $result = $calculation->calculateFormula($formula);
98
        self::assertSame('21', $result, 'Gnumeric');
99
100
        $this->setOpenOffice();
101
        $result = $calculation->calculateFormula($formula);
102
        self::assertSame('21', $result, 'OpenOffice');
103
104
        $this->setExcel();
105
        $result = $calculation->calculateFormula($formula);
106
        self::assertSame('21', $result, 'Excel');
107
    }
108
109
    /** @param mixed[] $expectedResult */
110
    #[DataProvider('providerDec2OctArray')]
111
    public function testDec2OctArray(array $expectedResult, string $value): void
112
    {
113
        $calculation = Calculation::getInstance();
114
115
        $formula = "=DEC2OCT({$value})";
116
        $result = $calculation->calculateFormula($formula);
117
        self::assertEquals($expectedResult, $result);
118
    }
119
120
    public static function providerDec2OctArray(): array
121
    {
122
        return [
123
            'row/column vector' => [
124
                [['4', '7', '77', '231', '314', '525']],
125
                '{4, 7, 63, 153, 204, 341}',
126
            ],
127
        ];
128
    }
129
}
130