1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xls\Parser; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xls\Workbook; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
|
13
|
|
|
class WorkbookTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
private Workbook $workbook; |
16
|
|
|
|
17
|
|
|
private ?Spreadsheet $spreadsheet = null; |
18
|
|
|
|
19
|
|
|
protected function tearDown(): void |
20
|
|
|
{ |
21
|
|
|
if ($this->spreadsheet !== null) { |
22
|
|
|
$this->spreadsheet->disconnectWorksheets(); |
23
|
|
|
$this->spreadsheet = null; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function setUpWorkbook(): void |
28
|
|
|
{ |
29
|
|
|
if ($this->spreadsheet !== null) { |
30
|
|
|
$this->spreadsheet->disconnectWorksheets(); |
31
|
|
|
} |
32
|
|
|
$this->spreadsheet = $spreadsheet = new Spreadsheet(); |
33
|
|
|
$strTotal = 0; |
34
|
|
|
$strUnique = 0; |
35
|
|
|
$str_table = []; |
36
|
|
|
$colors = []; |
37
|
|
|
$parser = new Parser($spreadsheet); |
38
|
|
|
|
39
|
|
|
$this->workbook = new Workbook($spreadsheet, $strTotal, $strUnique, $str_table, $colors, $parser); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string[] $testColors |
44
|
|
|
* @param string[] $expectedResult |
45
|
|
|
*/ |
46
|
|
|
public function xtestAddColor(array $testColors, array $expectedResult): void |
47
|
|
|
{ |
48
|
|
|
$workbookReflection = new ReflectionClass(Workbook::class); |
49
|
|
|
$methodAddColor = $workbookReflection->getMethod('addColor'); |
50
|
|
|
$propertyPalette = $workbookReflection->getProperty('palette'); |
51
|
|
|
$methodAddColor->setAccessible(true); |
52
|
|
|
$propertyPalette->setAccessible(true); |
53
|
|
|
|
54
|
|
|
foreach ($testColors as $testColor) { |
55
|
|
|
$methodAddColor->invoke($this->workbook, $testColor); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$palette = $propertyPalette->getValue($this->workbook); |
59
|
|
|
|
60
|
|
|
self::assertEquals($expectedResult, $palette); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testAddColor(): void |
64
|
|
|
{ |
65
|
|
|
$i = 0; |
66
|
|
|
$arrayEntries = $this->arrayAddColor(); |
67
|
|
|
while ($i < count($arrayEntries)) { |
68
|
|
|
/** @var string[] */ |
69
|
|
|
$entry0 = $arrayEntries[$i][0]; |
70
|
|
|
/** @var string[] */ |
71
|
|
|
$entry1 = $arrayEntries[$i][1]; |
72
|
|
|
$this->xtestAddColor($entry0, $entry1); |
73
|
|
|
++$i; |
74
|
|
|
$arrayEntries = $this->arrayAddColor(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @return array<int, array<int, array<mixed>>> */ |
79
|
|
|
public function arrayAddColor(): array |
80
|
|
|
{ |
81
|
|
|
$this->setUpWorkbook(); |
82
|
|
|
|
83
|
|
|
$workbookReflection = new ReflectionClass(Workbook::class); |
84
|
|
|
$propertyPalette = $workbookReflection->getProperty('palette'); |
85
|
|
|
$propertyPalette->setAccessible(true); |
86
|
|
|
|
87
|
|
|
$palette = $propertyPalette->getValue($this->workbook); |
88
|
|
|
self::assertIsArray($palette); |
89
|
|
|
|
90
|
|
|
$newColor1 = [0x00, 0x00, 0x01, 0x00]; |
91
|
|
|
$newColor2 = [0x00, 0x00, 0x02, 0x00]; |
92
|
|
|
$newColor3 = [0x00, 0x00, 0x03, 0x00]; |
93
|
|
|
|
94
|
|
|
// Add one new color |
95
|
|
|
$paletteTestOne = $palette; |
96
|
|
|
$paletteTestOne[8] = $newColor1; |
97
|
|
|
|
98
|
|
|
// Add one new color + one existing color after index 8 |
99
|
|
|
$paletteTestTwo = $paletteTestOne; |
100
|
|
|
|
101
|
|
|
// Add one new color + one existing color before index 9 |
102
|
|
|
$paletteTestThree = $paletteTestOne; |
103
|
|
|
$paletteTestThree[9] = $palette[8]; |
104
|
|
|
|
105
|
|
|
// Add three new color |
106
|
|
|
$paletteTestFour = $palette; |
107
|
|
|
$paletteTestFour[8] = $newColor1; |
108
|
|
|
$paletteTestFour[9] = $newColor2; |
109
|
|
|
$paletteTestFour[10] = $newColor3; |
110
|
|
|
|
111
|
|
|
// Add all existing color |
112
|
|
|
$colorsAdd = array_map([$this, 'paletteToColor'], $palette); |
113
|
|
|
$paletteTestFive = $palette; |
114
|
|
|
|
115
|
|
|
// Add new color after all existing color |
116
|
|
|
$colorsAddTwo = array_map([$this, 'paletteToColor'], $palette); |
117
|
|
|
$colorsAddTwo[] = $this->paletteToColor($newColor1); |
118
|
|
|
$paletteTestSix = $palette; |
119
|
|
|
|
120
|
|
|
// Add one existing color |
121
|
|
|
$paletteTestSeven = $palette; |
122
|
|
|
|
123
|
|
|
// Add two existing color |
124
|
|
|
$paletteTestHeight = $palette; |
125
|
|
|
|
126
|
|
|
// Add last existing color and add one new color |
127
|
|
|
$keyPalette = array_keys($palette); |
128
|
|
|
$last = end($keyPalette); |
129
|
|
|
self::assertIsArray($palette[8]); |
130
|
|
|
self::assertIsArray($palette[10]); |
131
|
|
|
self::assertIsArray($palette[12]); |
132
|
|
|
self::assertIsArray($palette[25]); |
133
|
|
|
self::assertIsArray($palette[$last]); |
134
|
|
|
$lastColor = $this->paletteToColor($palette[$last]); |
135
|
|
|
$paletteTestNine = $palette; |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
[[$this->paletteToColor($newColor1)], $paletteTestOne], |
139
|
|
|
[[$this->paletteToColor($newColor1), $this->paletteToColor($palette[12])], $paletteTestTwo], |
140
|
|
|
[[$this->paletteToColor($newColor1), $this->paletteToColor($palette[8])], $paletteTestThree], |
141
|
|
|
[[$this->paletteToColor($newColor1), $this->paletteToColor($newColor2), $this->paletteToColor($newColor3)], $paletteTestFour], |
142
|
|
|
[$colorsAdd, $paletteTestFive], |
143
|
|
|
[$colorsAddTwo, $paletteTestSix], |
144
|
|
|
[[$this->paletteToColor($palette[8])], $paletteTestSeven], |
145
|
|
|
[[$this->paletteToColor($palette[25]), $this->paletteToColor($palette[10])], $paletteTestHeight], |
146
|
|
|
[[$lastColor, $this->paletteToColor($newColor1)], $paletteTestNine], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Change palette color to rgb string. |
152
|
|
|
* |
153
|
|
|
* @param array<mixed, mixed> $palette |
154
|
|
|
*/ |
155
|
|
|
private function paletteToColor(array $palette): string |
156
|
|
|
{ |
157
|
|
|
return $this->right('00' . self::dec2hex($palette[0]), 2) |
158
|
|
|
. $this->right('00' . self::dec2hex($palette[1]), 2) |
159
|
|
|
. $this->right('00' . self::dec2hex($palette[2]), 2); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private static function dec2hex(mixed $value): string |
163
|
|
|
{ |
164
|
|
|
return is_numeric($value) ? dechex((int) $value) : '0'; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return n right character in string. |
169
|
|
|
* |
170
|
|
|
* @param string $value text to get right character |
171
|
|
|
* @param int $nbchar number of char at right of string |
172
|
|
|
*/ |
173
|
|
|
private function right(string $value, int $nbchar): string |
174
|
|
|
{ |
175
|
|
|
return mb_substr($value, mb_strlen($value) - $nbchar, $nbchar); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|