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