WorkbookTest.php 4.6 KB

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