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 Dec2BinTest extends AllSetupTeardown |
14
|
|
|
{ |
15
|
|
|
#[DataProvider('providerDEC2BIN')] |
16
|
|
|
public function testDirectCallToDEC2BIN(mixed $expectedResult, bool|float|int|string $value, null|float|int|string $digits = null): void |
17
|
|
|
{ |
18
|
|
|
$result = ($digits === null) ? ConvertDecimal::toBinary($value) : ConvertDecimal::toBinary($value, $digits); |
19
|
|
|
self::assertSame($expectedResult, $result); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
#[DataProvider('providerDEC2BIN')] |
23
|
|
|
public function testDEC2BINAsFormula(mixed $expectedResult, mixed ...$args): void |
24
|
|
|
{ |
25
|
|
|
$arguments = new FormulaArguments(...$args); |
26
|
|
|
|
27
|
|
|
$calculation = Calculation::getInstance(); |
28
|
|
|
$formula = "=DEC2BIN({$arguments})"; |
29
|
|
|
|
30
|
|
|
$result = $calculation->calculateFormula($formula); |
31
|
|
|
self::assertSame($expectedResult, $result); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
#[DataProvider('providerDEC2BIN')] |
35
|
|
|
public function testDEC2BINInWorksheet(mixed $expectedResult, mixed ...$args): void |
36
|
|
|
{ |
37
|
|
|
$arguments = new FormulaArguments(...$args); |
38
|
|
|
|
39
|
|
|
$worksheet = $this->getSheet(); |
40
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
41
|
|
|
$formula = "=DEC2BIN({$argumentCells})"; |
42
|
|
|
|
43
|
|
|
$result = $worksheet->setCellValue('A1', $formula) |
44
|
|
|
->getCell('A1') |
45
|
|
|
->getCalculatedValue(); |
46
|
|
|
self::assertSame($expectedResult, $result); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function providerDEC2BIN(): array |
50
|
|
|
{ |
51
|
|
|
return require 'tests/data/Calculation/Engineering/DEC2BIN.php'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
#[DataProvider('providerUnhappyDEC2BIN')] |
55
|
|
|
public function testDEC2BINUnhappyPath(string $expectedException, mixed ...$args): void |
56
|
|
|
{ |
57
|
|
|
$arguments = new FormulaArguments(...$args); |
58
|
|
|
|
59
|
|
|
$worksheet = $this->getSheet(); |
60
|
|
|
$argumentCells = $arguments->populateWorksheet($worksheet); |
61
|
|
|
$formula = "=DEC2BIN({$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 providerUnhappyDEC2BIN(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
['Formula Error: Wrong number of arguments for DEC2BIN() function'], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
#[DataProvider('providerDEC2BINOds')] |
78
|
|
|
public function testDEC2BINOds(mixed $expectedResult, bool|float|int|string $value, null|float|int|string $digits = null): void |
79
|
|
|
{ |
80
|
|
|
$this->setOpenOffice(); |
81
|
|
|
|
82
|
|
|
$result = ($digits === null) ? ConvertDecimal::toBinary($value) : ConvertDecimal::toBinary($value, $digits); |
83
|
|
|
self::assertSame($expectedResult, $result); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function providerDEC2BINOds(): array |
87
|
|
|
{ |
88
|
|
|
return require 'tests/data/Calculation/Engineering/DEC2BINOpenOffice.php'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testDEC2BINFrac(): void |
92
|
|
|
{ |
93
|
|
|
$calculation = Calculation::getInstance(); |
94
|
|
|
$formula = '=DEC2BIN(5.1)'; |
95
|
|
|
|
96
|
|
|
$this->setGnumeric(); |
97
|
|
|
$result = $calculation->calculateFormula($formula); |
98
|
|
|
self::assertSame('101', $result, 'Gnumeric'); |
99
|
|
|
|
100
|
|
|
$this->setOpenOffice(); |
101
|
|
|
$result = $calculation->calculateFormula($formula); |
102
|
|
|
self::assertSame('101', $result, 'OpenOffice'); |
103
|
|
|
|
104
|
|
|
$this->setExcel(); |
105
|
|
|
$result = $calculation->calculateFormula($formula); |
106
|
|
|
self::assertSame('101', $result, 'Excel'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @param mixed[] $expectedResult */ |
110
|
|
|
#[DataProvider('providerDec2BinArray')] |
111
|
|
|
public function testDec2BinArray(array $expectedResult, string $value): void |
112
|
|
|
{ |
113
|
|
|
$calculation = Calculation::getInstance(); |
114
|
|
|
|
115
|
|
|
$formula = "=DEC2BIN({$value})"; |
116
|
|
|
$result = $calculation->calculateFormula($formula); |
117
|
|
|
self::assertEquals($expectedResult, $result); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function providerDec2BinArray(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
'row/column vector' => [ |
124
|
|
|
[['100', '111', '111111', '10011001', '11001100', '101010101']], |
125
|
|
|
'{4, 7, 63, 153, 204, 341}', |
126
|
|
|
], |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|