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\ConvertHex; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
13
|
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments; |
14
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class Hex2DecTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
private string $compatibilityMode; |
20
|
|
|
|
21
|
|
|
protected function setUp(): void |
22
|
|
|
{ |
23
|
|
|
$this->compatibilityMode = Functions::getCompatibilityMode(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function tearDown(): void |
27
|
|
|
{ |
28
|
|
|
Functions::setCompatibilityMode($this->compatibilityMode); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
#[DataProvider('providerHEX2DEC')] |
32
|
|
|
public function testDirectCallToHEX2DEC(mixed $expectedResult, bool|float|int|string $value): void |
33
|
|
|
{ |
34
|
|
|
$result = ConvertHex::toDecimal($value); |
35
|
|
|
self::assertSame($expectedResult, $result); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
#[DataProvider('providerHEX2DEC')] |
39
|
|
|
public function testHEX2DECAsFormula(mixed $expectedResult, mixed ...$args): void |
40
|
|
|
{ |
41
|
|
|
$arguments = new FormulaArguments(...$args); |
42
|
|
|
|
43
|
|
|
$calculation = Calculation::getInstance(); |
44
|
|
|
$formula = "=HEX2DEC({$arguments})"; |
45
|
|
|
|
46
|
|
|
/** @var float|int|string */ |
47
|
|
|
$result = $calculation->calculateFormula($formula); |
48
|
|
|
self::assertSame($expectedResult, $result); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
#[DataProvider('providerHEX2DEC')] |
52
|
|
|
public function testHEX2DECInWorksheet(mixed $expectedResult, mixed ...$args): void |
53
|
|
|
{ |
54
|
|
|
$arguments = new FormulaArguments(...$args); |
55
|
|
|
|
56
|
|
|
$spreadsheet = new Spreadsheet(); |
57
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
58
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
59
|
|
|
$formula = "=HEX2DEC({$argumentCells})"; |
60
|
|
|
|
61
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
62
|
|
|
->getCell('A1') |
63
|
|
|
->getCalculatedValue(); |
64
|
|
|
self::assertSame($expectedResult, $result); |
65
|
|
|
|
66
|
|
|
$spreadsheet->disconnectWorksheets(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function providerHEX2DEC(): array |
70
|
|
|
{ |
71
|
|
|
return require 'tests/data/Calculation/Engineering/HEX2DEC.php'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
#[DataProvider('providerUnhappyHEX2DEC')] |
75
|
|
|
public function testHEX2DECUnhappyPath(string $expectedException, mixed ...$args): void |
76
|
|
|
{ |
77
|
|
|
$arguments = new FormulaArguments(...$args); |
78
|
|
|
|
79
|
|
|
$spreadsheet = new Spreadsheet(); |
80
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
81
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
82
|
|
|
$formula = "=HEX2DEC({$argumentCells})"; |
83
|
|
|
|
84
|
|
|
$this->expectException(CalculationException::class); |
85
|
|
|
$this->expectExceptionMessage($expectedException); |
86
|
|
|
$worksheet->setCellValue('A1', $formula) |
87
|
|
|
->getCell('A1') |
88
|
|
|
->getCalculatedValue(); |
89
|
|
|
|
90
|
|
|
$spreadsheet->disconnectWorksheets(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function providerUnhappyHEX2DEC(): array |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
['Formula Error: Wrong number of arguments for HEX2DEC() function'], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
#[DataProvider('providerHEX2DECOds')] |
101
|
|
|
public function testHEX2DECOds(mixed $expectedResult, bool|float|int|string $value): void |
102
|
|
|
{ |
103
|
|
|
Functions::setCompatibilityMode( |
104
|
|
|
Functions::COMPATIBILITY_OPENOFFICE |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$result = ConvertHex::toDecimal($value); |
108
|
|
|
self::assertSame($expectedResult, $result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public static function providerHEX2DECOds(): array |
112
|
|
|
{ |
113
|
|
|
return require 'tests/data/Calculation/Engineering/HEX2DECOpenOffice.php'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testHEX2DECFrac(): void |
117
|
|
|
{ |
118
|
|
|
$calculation = Calculation::getInstance(); |
119
|
|
|
$formula = '=HEX2DEC(10.1)'; |
120
|
|
|
|
121
|
|
|
Functions::setCompatibilityMode( |
122
|
|
|
Functions::COMPATIBILITY_GNUMERIC |
123
|
|
|
); |
124
|
|
|
/** @var float|int|string */ |
125
|
|
|
$result = $calculation->calculateFormula($formula); |
126
|
|
|
self::assertSame(16, $result, 'Gnumeric'); |
127
|
|
|
|
128
|
|
|
Functions::setCompatibilityMode( |
129
|
|
|
Functions::COMPATIBILITY_OPENOFFICE |
130
|
|
|
); |
131
|
|
|
/** @var float|int|string */ |
132
|
|
|
$result = $calculation->calculateFormula($formula); |
133
|
|
|
self::assertSame(ExcelError::NAN(), $result, 'OpenOffice'); |
134
|
|
|
|
135
|
|
|
Functions::setCompatibilityMode( |
136
|
|
|
Functions::COMPATIBILITY_EXCEL |
137
|
|
|
); |
138
|
|
|
/** @var float|int|string */ |
139
|
|
|
$result = $calculation->calculateFormula($formula); |
140
|
|
|
self::assertSame(ExcelError::NAN(), $result, 'Excel'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
#[DataProvider('providerHex2DecArray')] |
144
|
|
|
public function testHex2DecArray(array $expectedResult, string $value): void |
145
|
|
|
{ |
146
|
|
|
$calculation = Calculation::getInstance(); |
147
|
|
|
|
148
|
|
|
$formula = "=HEX2DEC({$value})"; |
149
|
|
|
$result = $calculation->calculateFormula($formula); |
150
|
|
|
self::assertEquals($expectedResult, $result); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public static function providerHex2DecArray(): array |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
'row/column vector' => [ |
157
|
|
|
[[4, 7, 63, 153, 204, 341]], |
158
|
|
|
'{"4", "7", "3F", "99", "CC", "155"}', |
159
|
|
|
], |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|