RowIteratorEmptyTest.php 4.8 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\RowIterator;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  8. use PHPUnit\Framework\TestCase;
  9. class RowIteratorEmptyTest 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('B3', null, DataType::TYPE_NULL);
  16. $sheet->setCellValueExplicit('B4', '', DataType::TYPE_STRING);
  17. $sheet->setCellValueExplicit('B5', null, DataType::TYPE_NULL);
  18. $sheet->setCellValueExplicit('C5', '', DataType::TYPE_STRING);
  19. $sheet->setCellValueExplicit('B6', null, DataType::TYPE_NULL);
  20. $sheet->setCellValueExplicit('C6', 'PHP', DataType::TYPE_STRING);
  21. $sheet->setCellValueExplicit('B7', '', DataType::TYPE_STRING);
  22. $sheet->setCellValueExplicit('C7', 'PHP', DataType::TYPE_STRING);
  23. $sheet->setCellValueExplicit('B8', null, DataType::TYPE_NULL);
  24. $sheet->setCellValueExplicit('C8', '', DataType::TYPE_STRING);
  25. $sheet->setCellValueExplicit('D8', 'PHP', DataType::TYPE_STRING);
  26. return $sheet;
  27. }
  28. /**
  29. * @dataProvider emptyRowBasic
  30. */
  31. public function testIteratorEmptyRow(int $rowId, bool $expectedEmpty): void
  32. {
  33. $spreadsheet = new Spreadsheet();
  34. $sheet = self::getPopulatedSheet($spreadsheet);
  35. $iterator = new RowIterator($sheet, 1, 9);
  36. $iterator->seek($rowId);
  37. $row = $iterator->current();
  38. $isEmpty = $row->isEmpty();
  39. self::assertSame($expectedEmpty, $isEmpty);
  40. $spreadsheet->disconnectWorksheets();
  41. }
  42. public function emptyRowBasic(): array
  43. {
  44. return [
  45. [1, false],
  46. [2, true],
  47. [3, false],
  48. [4, false],
  49. [5, false],
  50. [6, false],
  51. [7, false],
  52. [8, false],
  53. [9, true],
  54. ];
  55. }
  56. /**
  57. * @dataProvider emptyRowNullAsEmpty
  58. */
  59. public function testIteratorEmptyRowWithNull(int $rowId, bool $expectedEmpty): void
  60. {
  61. $spreadsheet = new Spreadsheet();
  62. $sheet = self::getPopulatedSheet($spreadsheet);
  63. $iterator = new RowIterator($sheet, 1, 9);
  64. $iterator->seek($rowId);
  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 emptyRowNullAsEmpty(): array
  71. {
  72. return [
  73. [1, false],
  74. [2, true],
  75. [3, true],
  76. [4, false],
  77. [5, false],
  78. [6, false],
  79. [7, false],
  80. [8, false],
  81. [9, true],
  82. ];
  83. }
  84. /**
  85. * @dataProvider emptyRowEmptyStringAsEmpty
  86. */
  87. public function testIteratorEmptyRowWithEmptyString(int $rowId, bool $expectedEmpty): void
  88. {
  89. $spreadsheet = new Spreadsheet();
  90. $sheet = self::getPopulatedSheet($spreadsheet);
  91. $iterator = new RowIterator($sheet, 1, 9);
  92. $iterator->seek($rowId);
  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 emptyRowEmptyStringAsEmpty(): array
  99. {
  100. return [
  101. [1, false],
  102. [2, true],
  103. [3, false],
  104. [4, true],
  105. [5, false],
  106. [6, false],
  107. [7, false],
  108. [8, false],
  109. [9, true],
  110. ];
  111. }
  112. /**
  113. * @dataProvider emptyRowNullAndEmptyStringAsEmpty
  114. */
  115. public function testIteratorEmptyRowWithNullAndEmptyString(int $rowId, bool $expectedEmpty): void
  116. {
  117. $spreadsheet = new Spreadsheet();
  118. $sheet = self::getPopulatedSheet($spreadsheet);
  119. $iterator = new RowIterator($sheet, 1, 9);
  120. $iterator->seek($rowId);
  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 emptyRowNullAndEmptyStringAsEmpty(): array
  129. {
  130. return [
  131. [1, false],
  132. [2, true],
  133. [3, true],
  134. [4, true],
  135. [5, true],
  136. [6, false],
  137. [7, false],
  138. [8, false],
  139. [9, true],
  140. ];
  141. }
  142. }