ConditionalFormattingBasicTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
  3. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  4. use PhpOffice\PhpSpreadsheet\Style\Conditional;
  5. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  6. use PHPUnit\Framework\TestCase;
  7. class ConditionalFormattingBasicTest extends TestCase
  8. {
  9. /**
  10. * @var Worksheet
  11. */
  12. protected $sheet;
  13. protected function setUp(): void
  14. {
  15. $filename = 'tests/data/Reader/XLS/CF_Basic_Comparisons.xls';
  16. $reader = new Xls();
  17. $spreadsheet = $reader->load($filename);
  18. $this->sheet = $spreadsheet->getActiveSheet();
  19. }
  20. /**
  21. * @dataProvider conditionalFormattingProvider
  22. */
  23. public function testReadConditionalFormatting(string $expectedRange, array $expectedRules): void
  24. {
  25. $hasConditionalStyles = $this->sheet->conditionalStylesExists($expectedRange);
  26. self::assertTrue($hasConditionalStyles);
  27. $conditionalStyles = $this->sheet->getConditionalStyles($expectedRange);
  28. foreach ($conditionalStyles as $index => $conditionalStyle) {
  29. self::assertSame($expectedRules[$index]['type'], $conditionalStyle->getConditionType());
  30. self::assertSame($expectedRules[$index]['operator'], $conditionalStyle->getOperatorType());
  31. self::assertSame($expectedRules[$index]['conditions'], $conditionalStyle->getConditions());
  32. }
  33. }
  34. public function conditionalFormattingProvider(): array
  35. {
  36. return [
  37. [
  38. 'A2:E5',
  39. [
  40. [
  41. 'type' => Conditional::CONDITION_CELLIS,
  42. 'operator' => Conditional::OPERATOR_EQUAL,
  43. 'conditions' => [
  44. 0,
  45. ],
  46. ],
  47. [
  48. 'type' => Conditional::CONDITION_CELLIS,
  49. 'operator' => Conditional::OPERATOR_GREATERTHAN,
  50. 'conditions' => [
  51. 0,
  52. ],
  53. ],
  54. [
  55. 'type' => Conditional::CONDITION_CELLIS,
  56. 'operator' => Conditional::OPERATOR_LESSTHAN,
  57. 'conditions' => [
  58. 0,
  59. ],
  60. ],
  61. ],
  62. ],
  63. [
  64. 'A10:E13',
  65. [
  66. [
  67. 'type' => Conditional::CONDITION_CELLIS,
  68. 'operator' => Conditional::OPERATOR_EQUAL,
  69. 'conditions' => [
  70. '$H$9',
  71. ],
  72. ],
  73. [
  74. 'type' => Conditional::CONDITION_CELLIS,
  75. 'operator' => Conditional::OPERATOR_GREATERTHAN,
  76. 'conditions' => [
  77. '$H$9',
  78. ],
  79. ],
  80. [
  81. 'type' => Conditional::CONDITION_CELLIS,
  82. 'operator' => Conditional::OPERATOR_LESSTHAN,
  83. 'conditions' => [
  84. '$H$9',
  85. ],
  86. ],
  87. ],
  88. ],
  89. [
  90. 'A18:A20',
  91. [
  92. [
  93. 'type' => Conditional::CONDITION_CELLIS,
  94. 'operator' => Conditional::OPERATOR_BETWEEN,
  95. 'conditions' => [
  96. '$B1',
  97. '$C1',
  98. ],
  99. ],
  100. ],
  101. ],
  102. [
  103. 'A24:E27',
  104. [
  105. [
  106. 'type' => Conditional::CONDITION_CELLIS,
  107. 'operator' => Conditional::OPERATOR_BETWEEN,
  108. 'conditions' => [
  109. 'AVERAGE($A$24:$E$27)-STDEV($A$24:$E$27)',
  110. 'AVERAGE($A$24:$E$27)+STDEV($A$24:$E$27)',
  111. ],
  112. ],
  113. [
  114. 'type' => Conditional::CONDITION_CELLIS,
  115. 'operator' => Conditional::OPERATOR_GREATERTHAN,
  116. 'conditions' => [
  117. 'AVERAGE($A$24:$E$27)+STDEV($A$24:$E$27)',
  118. ],
  119. ],
  120. [
  121. 'type' => Conditional::CONDITION_CELLIS,
  122. 'operator' => Conditional::OPERATOR_LESSTHAN,
  123. 'conditions' => [
  124. 'AVERAGE($A$24:$E$27)-STDEV($A$24:$E$27)',
  125. ],
  126. ],
  127. ],
  128. ],
  129. [
  130. 'A31:A33',
  131. [
  132. [
  133. 'type' => Conditional::CONDITION_CELLIS,
  134. 'operator' => Conditional::OPERATOR_EQUAL,
  135. 'conditions' => [
  136. '"LOVE"',
  137. ],
  138. ],
  139. [
  140. 'type' => Conditional::CONDITION_CELLIS,
  141. 'operator' => Conditional::OPERATOR_EQUAL,
  142. 'conditions' => [
  143. '"PHP"',
  144. ],
  145. ],
  146. ],
  147. ],
  148. ];
  149. }
  150. }