ColumnCellIterator2Test.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\ColumnCellIterator;
  5. use PHPUnit\Framework\TestCase;
  6. class ColumnCellIterator2Test extends TestCase
  7. {
  8. /**
  9. * @dataProvider providerExistingCell
  10. */
  11. public function testEndRange(?bool $existing, string $expectedResultFirst, string $expectedResultLast): void
  12. {
  13. $spreadsheet = new Spreadsheet();
  14. $sheet = $spreadsheet->getActiveSheet();
  15. $sheet->getCell('B2')->setValue('cellb2');
  16. $sheet->getCell('B6')->setValue('cellb6');
  17. $iterator = new ColumnCellIterator($sheet, 'B', 1, 8);
  18. if (isset($existing)) {
  19. $iterator->setIterateOnlyExistingCells($existing);
  20. }
  21. $lastCoordinate = '';
  22. $firstCoordinate = '';
  23. foreach ($iterator as $cell) {
  24. if ($cell !== null) {
  25. $lastCoordinate = $cell->getCoordinate();
  26. if (!$firstCoordinate) {
  27. $firstCoordinate = $lastCoordinate;
  28. }
  29. }
  30. }
  31. self::assertEquals($expectedResultFirst, $firstCoordinate);
  32. self::assertEquals($expectedResultLast, $lastCoordinate);
  33. }
  34. public function providerExistingCell(): array
  35. {
  36. return [
  37. [null, 'B1', 'B8'],
  38. [false, 'B1', 'B8'],
  39. [true, 'B2', 'B6'],
  40. ];
  41. }
  42. /**
  43. * @dataProvider providerEmptyColumn
  44. */
  45. public function testEmptyColumn(?bool $existing, int $expectedResult): void
  46. {
  47. $spreadsheet = new Spreadsheet();
  48. $sheet = $spreadsheet->getActiveSheet();
  49. $sheet->getCell('B2')->setValue('cellb2');
  50. $sheet->getCell('B6')->setValue('cellb6');
  51. $iterator = new ColumnCellIterator($sheet, 'C');
  52. if (isset($existing)) {
  53. $iterator->setIterateOnlyExistingCells($existing);
  54. }
  55. $numCells = 0;
  56. foreach ($iterator as $cell) {
  57. ++$numCells;
  58. }
  59. self::assertEquals($expectedResult, $numCells);
  60. }
  61. public function providerEmptyColumn(): array
  62. {
  63. return [
  64. [null, 6],
  65. [false, 6],
  66. [true, 0],
  67. ];
  68. }
  69. }