CellTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Cell;
  3. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  4. use PhpOffice\PhpSpreadsheet\Exception;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Color;
  7. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
  8. use PhpOffice\PhpSpreadsheet\Style\Fill;
  9. use PhpOffice\PhpSpreadsheet\Style\Style;
  10. use PHPUnit\Framework\TestCase;
  11. class CellTest extends TestCase
  12. {
  13. /**
  14. * @dataProvider providerSetValueExplicit
  15. *
  16. * @param mixed $expected
  17. * @param mixed $value
  18. */
  19. public function testSetValueExplicit($expected, $value, string $dataType): void
  20. {
  21. $spreadsheet = new Spreadsheet();
  22. $cell = $spreadsheet->getActiveSheet()->getCell('A1');
  23. $cell->setValueExplicit($value, $dataType);
  24. self::assertSame($expected, $cell->getValue());
  25. $spreadsheet->disconnectWorksheets();
  26. }
  27. public function providerSetValueExplicit(): array
  28. {
  29. return require 'tests/data/Cell/SetValueExplicit.php';
  30. }
  31. public function testInvalidIsoDateSetValueExplicit(): void
  32. {
  33. $spreadsheet = new Spreadsheet();
  34. $cell = $spreadsheet->getActiveSheet()->getCell('A1');
  35. $dateValue = '2022-02-29'; // Invalid leap year
  36. $this->expectException(Exception::class);
  37. $this->expectExceptionMessage("Invalid string {$dateValue} supplied for datatype Date");
  38. $cell->setValueExplicit($dateValue, DataType::TYPE_ISO_DATE);
  39. $spreadsheet->disconnectWorksheets();
  40. }
  41. /**
  42. * @dataProvider providerSetValueExplicitException
  43. *
  44. * @param mixed $value
  45. */
  46. public function testSetValueExplicitException($value, string $dataType): void
  47. {
  48. $this->expectException(Exception::class);
  49. $spreadsheet = new Spreadsheet();
  50. $cell = $spreadsheet->getActiveSheet()->getCell('A1');
  51. $cell->setValueExplicit($value, $dataType);
  52. }
  53. public function providerSetValueExplicitException(): array
  54. {
  55. return require 'tests/data/Cell/SetValueExplicitException.php';
  56. }
  57. public function testNoChangeToActiveSheet(): void
  58. {
  59. $spreadsheet = new Spreadsheet();
  60. $sheet1 = $spreadsheet->getActiveSheet();
  61. $sheet1->setTitle('Sheet 1');
  62. $sheet3 = $spreadsheet->createSheet();
  63. $sheet3->setTitle('Sheet 3');
  64. $sheet1->setCellValue('C1', 123);
  65. $sheet1->setCellValue('D1', 124);
  66. $sheet3->setCellValue('A1', "='Sheet 1'!C1+'Sheet 1'!D1");
  67. $sheet1->setCellValue('A1', "='Sheet 3'!A1");
  68. $cell = 'A1';
  69. $spreadsheet->setActiveSheetIndex(0);
  70. self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
  71. $value = $spreadsheet->getActiveSheet()->getCell($cell)->getCalculatedValue();
  72. self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
  73. self::assertEquals(247, $value);
  74. $spreadsheet->disconnectWorksheets();
  75. }
  76. public function testDestroyWorksheet(): void
  77. {
  78. $spreadsheet = new Spreadsheet();
  79. $sheet = $spreadsheet->getActiveSheet();
  80. $cell = $sheet->getCell('A1');
  81. self::assertSame($sheet, $cell->getWorksheet());
  82. $this->expectException(Exception::class);
  83. $this->expectExceptionMessage('Worksheet no longer exists');
  84. $spreadsheet->disconnectWorksheets();
  85. $cell->getWorksheet();
  86. }
  87. public function testDestroyCell1(): void
  88. {
  89. $spreadsheet = new Spreadsheet();
  90. $sheet = $spreadsheet->getActiveSheet();
  91. $cell = $sheet->getCell('A1');
  92. self::assertSame('A1', $cell->getCoordinate());
  93. $this->expectException(Exception::class);
  94. $this->expectExceptionMessage('Coordinate no longer exists');
  95. $spreadsheet->disconnectWorksheets();
  96. $cell->getCoordinate();
  97. }
  98. public function testDestroyCell2(): void
  99. {
  100. $spreadsheet = new Spreadsheet();
  101. $sheet = $spreadsheet->getActiveSheet();
  102. $cell = $sheet->getCell('A1');
  103. self::assertSame('A1', $cell->getCoordinate());
  104. $this->expectException(Exception::class);
  105. $this->expectExceptionMessage('Coordinate no longer exists');
  106. $cell->getParent()->delete('A1');
  107. $cell->getCoordinate();
  108. }
  109. public function testAppliedStyleWithRange(): void
  110. {
  111. $spreadsheet = new Spreadsheet();
  112. $sheet = $spreadsheet->getActiveSheet();
  113. $sheet->setCellValue('A1', -1);
  114. $sheet->setCellValue('A2', 0);
  115. $sheet->setCellValue('A3', 1);
  116. $cellRange = 'A1:A3';
  117. $sheet->getStyle($cellRange)->getFont()->setBold(true);
  118. $yellowStyle = new Style(false, true);
  119. $yellowStyle->getFill()
  120. ->setFillType(Fill::FILL_SOLID)
  121. ->getEndColor()->setARGB(Color::COLOR_YELLOW);
  122. $greenStyle = new Style(false, true);
  123. $greenStyle->getFill()
  124. ->setFillType(Fill::FILL_SOLID)
  125. ->getEndColor()->setARGB(Color::COLOR_GREEN);
  126. $redStyle = new Style(false, true);
  127. $redStyle->getFill()
  128. ->setFillType(Fill::FILL_SOLID)
  129. ->getEndColor()->setARGB(Color::COLOR_RED);
  130. $conditionalStyles = [];
  131. $wizardFactory = new Wizard($cellRange);
  132. /** @var Wizard\CellValue $cellWizard */
  133. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  134. $cellWizard->equals(0)
  135. ->setStyle($yellowStyle);
  136. $conditionalStyles[] = $cellWizard->getConditional();
  137. $cellWizard->greaterThan(0)
  138. ->setStyle($greenStyle);
  139. $conditionalStyles[] = $cellWizard->getConditional();
  140. $cellWizard->lessThan(0)
  141. ->setStyle($redStyle);
  142. $conditionalStyles[] = $cellWizard->getConditional();
  143. $sheet->getStyle($cellWizard->getCellRange())
  144. ->setConditionalStyles($conditionalStyles);
  145. $style = $sheet->getCell('A1')->getAppliedStyle();
  146. self::assertTrue($style->getFont()->getBold());
  147. self::assertEquals($redStyle->getFill()->getFillType(), $style->getFill()->getFillType());
  148. self::assertEquals($redStyle->getFill()->getEndColor()->getARGB(), $style->getFill()->getEndColor()->getARGB());
  149. $style = $sheet->getCell('A2')->getAppliedStyle();
  150. self::assertTrue($style->getFont()->getBold());
  151. self::assertEquals($yellowStyle->getFill()->getFillType(), $style->getFill()->getFillType());
  152. self::assertEquals(
  153. $yellowStyle->getFill()->getEndColor()->getARGB(),
  154. $style->getFill()->getEndColor()->getARGB()
  155. );
  156. $style = $sheet->getCell('A3')->getAppliedStyle();
  157. self::assertTrue($style->getFont()->getBold());
  158. self::assertEquals($greenStyle->getFill()->getFillType(), $style->getFill()->getFillType());
  159. self::assertEquals(
  160. $greenStyle->getFill()->getEndColor()->getARGB(),
  161. $style->getFill()->getEndColor()->getARGB()
  162. );
  163. }
  164. /**
  165. * @dataProvider appliedStyling
  166. */
  167. public function testAppliedStyleSingleCell(string $cellAddress, string $fillStyle, ?string $fillColor): void
  168. {
  169. $spreadsheet = new Spreadsheet();
  170. $sheet = $spreadsheet->getActiveSheet();
  171. $sheet->setCellValue('A1', -1);
  172. $sheet->setCellValue('A2', 0);
  173. $sheet->setCellValue('B1', 0);
  174. $sheet->setCellValue('C1', 1);
  175. $sheet->setCellValue('C2', -1);
  176. $cellRange = 'A1:C2';
  177. $sheet->getStyle($cellRange)->getFont()->setBold(true);
  178. $yellowStyle = new Style(false, true);
  179. $yellowStyle->getFill()
  180. ->setFillType(Fill::FILL_SOLID)
  181. ->getEndColor()->setARGB(Color::COLOR_YELLOW);
  182. $redStyle = new Style(false, true);
  183. $redStyle->getFill()
  184. ->setFillType(Fill::FILL_SOLID)
  185. ->getEndColor()->setARGB(Color::COLOR_RED);
  186. $conditionalCellRange = 'A1:C1';
  187. $conditionalStyles = [];
  188. $wizardFactory = new Wizard($conditionalCellRange);
  189. /** @var Wizard\CellValue $cellWizard */
  190. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  191. $cellWizard->equals(0)
  192. ->setStyle($yellowStyle);
  193. $conditionalStyles[] = $cellWizard->getConditional();
  194. $cellWizard->lessThan(0)
  195. ->setStyle($redStyle);
  196. $conditionalStyles[] = $cellWizard->getConditional();
  197. $sheet->getStyle($cellWizard->getCellRange())
  198. ->setConditionalStyles($conditionalStyles);
  199. $style = $sheet->getCell($cellAddress)->getAppliedStyle();
  200. self::assertTrue($style->getFont()->getBold());
  201. self::assertEquals($fillStyle, $style->getFill()->getFillType());
  202. if ($fillStyle === Fill::FILL_SOLID) {
  203. self::assertEquals($fillColor, $style->getFill()->getEndColor()->getARGB());
  204. }
  205. }
  206. public function appliedStyling(): array
  207. {
  208. return [
  209. 'A1 - Conditional with Match' => ['A1', Fill::FILL_SOLID, Color::COLOR_RED],
  210. 'A2 - No Conditionals' => ['A2', Fill::FILL_NONE, null],
  211. 'B1 - Conditional with Match' => ['B1', Fill::FILL_SOLID, Color::COLOR_YELLOW],
  212. 'C1 - Conditionals, but No Match' => ['C1', Fill::FILL_NONE, null],
  213. 'C2 - No Conditionals' => ['C2', Fill::FILL_NONE, null],
  214. ];
  215. }
  216. }