ConditionalBoolTest.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Style;
  3. use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
  4. use PhpOffice\PhpSpreadsheet\Shared\File;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Conditional;
  7. use PhpOffice\PhpSpreadsheet\Style\Fill;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  9. use PHPUnit\Framework\TestCase;
  10. class ConditionalBoolTest extends TestCase
  11. {
  12. /** @var string */
  13. private $outfile = '';
  14. protected function tearDown(): void
  15. {
  16. if ($this->outfile !== '') {
  17. unlink($this->outfile);
  18. $this->outfile = '';
  19. }
  20. }
  21. public function testBool(): void
  22. {
  23. $spreadsheet = new Spreadsheet();
  24. $sheet = $spreadsheet->getActiveSheet();
  25. $condition1 = new Conditional();
  26. $condition1->setConditionType(Conditional::CONDITION_CELLIS);
  27. $condition1->setOperatorType(Conditional::OPERATOR_EQUAL);
  28. $condition1->addCondition(false);
  29. $condition1->getStyle()->getFill()
  30. ->setFillType(Fill::FILL_SOLID)
  31. ->getEndColor()->setARGB('FFFFFF00');
  32. $conditionalStyles = $sheet->getStyle('A1:A10')->getConditionalStyles();
  33. $conditionalStyles[] = $condition1;
  34. $sheet->getStyle('A1:A20')->setConditionalStyles($conditionalStyles);
  35. $sheet->setCellValue('A1', 1);
  36. $sheet->setCellValue('A2', true);
  37. $sheet->setCellValue('A3', false);
  38. $sheet->setCellValue('A4', 0.6);
  39. $sheet->setCellValue('A6', 0);
  40. $sheet->setSelectedCell('B1');
  41. $sheet = $spreadsheet->createSheet();
  42. $condition1 = new Conditional();
  43. $condition1->setConditionType(Conditional::CONDITION_CELLIS);
  44. $condition1->setOperatorType(Conditional::OPERATOR_EQUAL);
  45. $condition1->addCondition(true);
  46. $condition1->getStyle()->getFill()
  47. ->setFillType(Fill::FILL_SOLID)
  48. ->getEndColor()->setARGB('FF00FF00');
  49. $conditionalStyles = $sheet->getStyle('A1:A10')->getConditionalStyles();
  50. $conditionalStyles[] = $condition1;
  51. $sheet->getStyle('A1:A20')->setConditionalStyles($conditionalStyles);
  52. $sheet->setCellValue('A1', 1);
  53. $sheet->setCellValue('A2', true);
  54. $sheet->setCellValue('A3', false);
  55. $sheet->setCellValue('A4', 0.6);
  56. $sheet->setCellValue('A6', 0);
  57. $sheet->setSelectedCell('B1');
  58. $writer = new XlsxWriter($spreadsheet);
  59. $this->outfile = File::temporaryFilename();
  60. $writer->save($this->outfile);
  61. $spreadsheet->disconnectWorksheets();
  62. $file = 'zip://' . $this->outfile . '#xl/worksheets/sheet1.xml';
  63. $contents = file_get_contents($file);
  64. self::assertNotFalse($contents);
  65. self::assertStringContainsString('<formula>FALSE</formula>', $contents);
  66. $file = 'zip://' . $this->outfile . '#xl/worksheets/sheet2.xml';
  67. $contents = file_get_contents($file);
  68. self::assertNotFalse($contents);
  69. self::assertStringContainsString('<formula>TRUE</formula>', $contents);
  70. $reader = new XlsxReader();
  71. $spreadsheet2 = $reader->load($this->outfile);
  72. $sheet1 = $spreadsheet2->getSheet(0);
  73. $condArray = $sheet1->getStyle('A1:A20')->getConditionalStyles();
  74. self::assertNotEmpty($condArray);
  75. $cond1 = $condArray[0];
  76. self::assertSame(Conditional::CONDITION_CELLIS, $cond1->getConditionType());
  77. self::assertSame(Conditional::OPERATOR_EQUAL, $cond1->getOperatorType());
  78. self::assertFalse(($cond1->getConditions())[0]);
  79. $sheet2 = $spreadsheet2->getSheet(1);
  80. $condArray = $sheet2->getStyle('A1:A20')->getConditionalStyles();
  81. self::assertNotEmpty($condArray);
  82. $cond1 = $condArray[0];
  83. self::assertSame(Conditional::CONDITION_CELLIS, $cond1->getConditionType());
  84. self::assertSame(Conditional::OPERATOR_EQUAL, $cond1->getOperatorType());
  85. self::assertTrue(($cond1->getConditions())[0]);
  86. $spreadsheet2->disconnectWorksheets();
  87. }
  88. }