ColumnIteratorEmptyTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator;
  6. use PhpOffice\PhpSpreadsheet\Worksheet\ColumnIterator;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  8. use PHPUnit\Framework\TestCase;
  9. class ColumnIteratorEmptyTest extends TestCase
  10. {
  11. private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet
  12. {
  13. $sheet = $spreadsheet->getActiveSheet();
  14. $sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING);
  15. $sheet->setCellValueExplicit('C2', null, DataType::TYPE_NULL);
  16. $sheet->setCellValueExplicit('D2', '', DataType::TYPE_STRING);
  17. $sheet->setCellValueExplicit('E2', null, DataType::TYPE_NULL);
  18. $sheet->setCellValueExplicit('E3', '', DataType::TYPE_STRING);
  19. $sheet->setCellValueExplicit('F2', null, DataType::TYPE_NULL);
  20. $sheet->setCellValueExplicit('F3', 'PHP', DataType::TYPE_STRING);
  21. $sheet->setCellValueExplicit('G2', '', DataType::TYPE_STRING);
  22. $sheet->setCellValueExplicit('G3', 'PHP', DataType::TYPE_STRING);
  23. $sheet->setCellValueExplicit('H2', null, DataType::TYPE_NULL);
  24. $sheet->setCellValueExplicit('H3', '', DataType::TYPE_STRING);
  25. $sheet->setCellValueExplicit('H4', 'PHP', DataType::TYPE_STRING);
  26. return $sheet;
  27. }
  28. /**
  29. * @dataProvider emptyColumnBasic
  30. */
  31. public function testIteratorEmptyColumn(string $columnId, bool $expectedEmpty): void
  32. {
  33. $spreadsheet = new Spreadsheet();
  34. $sheet = self::getPopulatedSheet($spreadsheet);
  35. $iterator = new ColumnIterator($sheet, 'A', 'I');
  36. $iterator->seek($columnId);
  37. $row = $iterator->current();
  38. $isEmpty = $row->isEmpty();
  39. self::assertSame($expectedEmpty, $isEmpty);
  40. $spreadsheet->disconnectWorksheets();
  41. }
  42. public function emptyColumnBasic(): array
  43. {
  44. return [
  45. ['A', false],
  46. ['B', true],
  47. ['C', false],
  48. ['D', false],
  49. ['E', false],
  50. ['F', false],
  51. ['G', false],
  52. ['H', false],
  53. ['I', true],
  54. ];
  55. }
  56. /**
  57. * @dataProvider emptyColumnNullAsEmpty
  58. */
  59. public function testIteratorEmptyColumnWithNull(string $columnId, bool $expectedEmpty): void
  60. {
  61. $spreadsheet = new Spreadsheet();
  62. $sheet = self::getPopulatedSheet($spreadsheet);
  63. $iterator = new ColumnIterator($sheet, 'A', 'I');
  64. $iterator->seek($columnId);
  65. $row = $iterator->current();
  66. $isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
  67. self::assertSame($expectedEmpty, $isEmpty);
  68. $spreadsheet->disconnectWorksheets();
  69. }
  70. public function emptyColumnNullAsEmpty(): array
  71. {
  72. return [
  73. ['A', false],
  74. ['B', true],
  75. ['C', true],
  76. ['D', false],
  77. ['E', false],
  78. ['F', false],
  79. ['G', false],
  80. ['H', false],
  81. ['I', true],
  82. ];
  83. }
  84. /**
  85. * @dataProvider emptyColumnEmptyStringAsEmpty
  86. */
  87. public function testIteratorEmptyColumnWithEmptyString(string $columnId, bool $expectedEmpty): void
  88. {
  89. $spreadsheet = new Spreadsheet();
  90. $sheet = self::getPopulatedSheet($spreadsheet);
  91. $iterator = new ColumnIterator($sheet, 'A', 'I');
  92. $iterator->seek($columnId);
  93. $row = $iterator->current();
  94. $isEmpty = $row->isEmpty(CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
  95. self::assertSame($expectedEmpty, $isEmpty);
  96. $spreadsheet->disconnectWorksheets();
  97. }
  98. public function emptyColumnEmptyStringAsEmpty(): array
  99. {
  100. return [
  101. ['A', false],
  102. ['B', true],
  103. ['C', false],
  104. ['D', true],
  105. ['E', false],
  106. ['F', false],
  107. ['G', false],
  108. ['H', false],
  109. ['I', true],
  110. ];
  111. }
  112. /**
  113. * @dataProvider emptyColumnNullAndEmptyStringAsEmpty
  114. */
  115. public function testIteratorEmptyColumnWithNullAndEmptyString(string $columnId, bool $expectedEmpty): void
  116. {
  117. $spreadsheet = new Spreadsheet();
  118. $sheet = self::getPopulatedSheet($spreadsheet);
  119. $iterator = new ColumnIterator($sheet, 'A', 'I');
  120. $iterator->seek($columnId);
  121. $row = $iterator->current();
  122. $isEmpty = $row->isEmpty(
  123. CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
  124. );
  125. self::assertSame($expectedEmpty, $isEmpty);
  126. $spreadsheet->disconnectWorksheets();
  127. }
  128. public function emptyColumnNullAndEmptyStringAsEmpty(): array
  129. {
  130. return [
  131. ['A', false],
  132. ['B', true],
  133. ['C', true],
  134. ['D', true],
  135. ['E', true],
  136. ['F', false],
  137. ['G', false],
  138. ['H', false],
  139. ['I', true],
  140. ];
  141. }
  142. }