1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Engineering; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ConvertBinary; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
11
|
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class Bin2HexTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $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
|
|
|
/** |
32
|
|
|
* @dataProvider providerBIN2HEX |
33
|
|
|
* |
34
|
|
|
* @param mixed $expectedResult |
35
|
|
|
*/ |
36
|
|
|
public function testDirectCallToBIN2HEX($expectedResult, ...$args): void |
37
|
|
|
{ |
38
|
|
|
/** @scrutinizer ignore-call */ |
39
|
|
|
$result = ConvertBinary::toHex(...$args); |
40
|
|
|
self::assertSame($expectedResult, $result); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function trimIfQuoted(string $value): string |
44
|
|
|
{ |
45
|
|
|
return trim($value, '"'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider providerBIN2HEX |
50
|
|
|
* |
51
|
|
|
* @param mixed $expectedResult |
52
|
|
|
*/ |
53
|
|
|
public function testBIN2HEXAsFormula($expectedResult, ...$args): void |
54
|
|
|
{ |
55
|
|
|
$arguments = new FormulaArguments(...$args); |
56
|
|
|
|
57
|
|
|
$calculation = Calculation::getInstance(); |
58
|
|
|
$formula = "=BIN2HEX({$arguments})"; |
59
|
|
|
|
60
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
61
|
|
|
self::assertSame($expectedResult, $this->trimIfQuoted((string) $result)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider providerBIN2HEX |
66
|
|
|
* |
67
|
|
|
* @param mixed $expectedResult |
68
|
|
|
*/ |
69
|
|
|
public function testBIN2HEXInWorksheet($expectedResult, ...$args): void |
70
|
|
|
{ |
71
|
|
|
$arguments = new FormulaArguments(...$args); |
72
|
|
|
|
73
|
|
|
$spreadsheet = new Spreadsheet(); |
74
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
75
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
76
|
|
|
$formula = "=BIN2HEX({$argumentCells})"; |
77
|
|
|
|
78
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
79
|
|
|
->getCell('A1') |
80
|
|
|
->getCalculatedValue(); |
81
|
|
|
self::assertSame($expectedResult, $result); |
82
|
|
|
|
83
|
|
|
$spreadsheet->disconnectWorksheets(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function providerBIN2HEX(): array |
87
|
|
|
{ |
88
|
|
|
return require 'tests/data/Calculation/Engineering/BIN2HEX.php'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider providerUnhappyBIN2HEX |
93
|
|
|
*/ |
94
|
|
|
public function testBIN2HEXUnhappyPath(string $expectedException, ...$args): void |
95
|
|
|
{ |
96
|
|
|
$arguments = new FormulaArguments(...$args); |
97
|
|
|
|
98
|
|
|
$spreadsheet = new Spreadsheet(); |
99
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
100
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
101
|
|
|
$formula = "=BIN2HEX({$argumentCells})"; |
102
|
|
|
|
103
|
|
|
$this->expectException(CalculationException::class); |
104
|
|
|
$this->expectExceptionMessage($expectedException); |
105
|
|
|
$worksheet->setCellValue('A1', $formula) |
106
|
|
|
->getCell('A1') |
107
|
|
|
->getCalculatedValue(); |
108
|
|
|
|
109
|
|
|
$spreadsheet->disconnectWorksheets(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function providerUnhappyBIN2HEX(): array |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
['Formula Error: Wrong number of arguments for BIN2HEX() function'], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @dataProvider providerBIN2HEXOds |
121
|
|
|
* |
122
|
|
|
* @param mixed $expectedResult |
123
|
|
|
*/ |
124
|
|
|
public function testBIN2HEXOds($expectedResult, ...$args): void |
125
|
|
|
{ |
126
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); |
127
|
|
|
|
128
|
|
|
/** @scrutinizer ignore-call */ |
129
|
|
|
$result = ConvertBinary::toDecimal(...$args); |
130
|
|
|
self::assertSame($expectedResult, $result); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function providerBIN2HEXOds(): array |
134
|
|
|
{ |
135
|
|
|
return require 'tests/data/Calculation/Engineering/BIN2HEXOpenOffice.php'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testBIN2HEXFractional(): void |
139
|
|
|
{ |
140
|
|
|
$calculation = Calculation::getInstance(); |
141
|
|
|
$formula = '=BIN2HEX(101.1)'; |
142
|
|
|
|
143
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC); |
144
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
145
|
|
|
self::assertSame('5', $this->trimIfQuoted((string) $result), 'Gnumeric'); |
146
|
|
|
|
147
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); |
148
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
149
|
|
|
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'OpenOffice'); |
150
|
|
|
|
151
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); |
152
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
153
|
|
|
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'Excel'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @dataProvider providerBin2HexArray |
158
|
|
|
*/ |
159
|
|
|
public function testBin2HexArray(array $expectedResult, string $value): void |
160
|
|
|
{ |
161
|
|
|
$calculation = Calculation::getInstance(); |
162
|
|
|
|
163
|
|
|
$formula = "=BIN2HEX({$value})"; |
164
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
165
|
|
|
self::assertEquals($expectedResult, $result); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function providerBin2HexArray(): array |
169
|
|
|
{ |
170
|
|
|
return [ |
171
|
|
|
'row/column vector' => [ |
172
|
|
|
[['4', '7', '3F', '99', 'CC', '155']], |
173
|
|
|
'{"100", "111", "111111", "10011001", "11001100", "101010101"}', |
174
|
|
|
], |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|