PhpSpreadsheet/tests/PhpSpreadsheetTests/Worksheet/RowCellIteratorTest.php

101 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
2016-03-22 14:35:50 +00:00
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Worksheet\RowCellIterator;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class RowCellIteratorTest extends TestCase
{
public $mockWorksheet;
2018-01-28 06:59:38 +00:00
2017-10-07 17:00:30 +00:00
public $mockCell;
2020-04-27 10:28:36 +00:00
protected function setUp(): void
2015-05-17 13:00:02 +00:00
{
$this->mockCell = $this->getMockBuilder(Cell::class)
->disableOriginalConstructor()
->getMock();
$this->mockWorksheet = $this->getMockBuilder(Worksheet::class)
->disableOriginalConstructor()
->getMock();
2020-05-18 04:49:57 +00:00
$this->mockWorksheet->expects(self::any())
->method('getHighestColumn')
2020-05-18 04:49:57 +00:00
->willReturn('E');
$this->mockWorksheet->expects(self::any())
->method('getCellByColumnAndRow')
2020-05-18 04:49:57 +00:00
->willReturn($this->mockCell);
}
2020-05-18 04:49:57 +00:00
public function testIteratorFullRange(): void
2015-05-17 13:00:02 +00:00
{
$iterator = new RowCellIterator($this->mockWorksheet);
$RowCellIndexResult = 'A';
self::assertEquals($RowCellIndexResult, $iterator->key());
2015-05-17 13:00:02 +00:00
foreach ($iterator as $key => $RowCell) {
self::assertEquals($RowCellIndexResult++, $key);
self::assertInstanceOf(Cell::class, $RowCell);
}
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testIteratorStartEndRange(): void
2015-05-17 13:00:02 +00:00
{
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
$RowCellIndexResult = 'B';
self::assertEquals($RowCellIndexResult, $iterator->key());
2015-05-17 13:00:02 +00:00
foreach ($iterator as $key => $RowCell) {
self::assertEquals($RowCellIndexResult++, $key);
self::assertInstanceOf(Cell::class, $RowCell);
}
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testIteratorSeekAndPrev(): void
2015-05-17 13:00:02 +00:00
{
2015-05-17 17:26:34 +00:00
$ranges = range('A', 'E');
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
$RowCellIndexResult = 'D';
$iterator->seek('D');
self::assertEquals($RowCellIndexResult, $iterator->key());
for ($i = 1; $i < array_search($RowCellIndexResult, $ranges); ++$i) {
$iterator->prev();
$expectedResult = $ranges[array_search($RowCellIndexResult, $ranges) - $i];
self::assertEquals($expectedResult, $iterator->key());
}
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testSeekOutOfRange(): void
{
2018-01-28 06:59:38 +00:00
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
Apply Column and Row Styles to Existing Cells (#1721) * Apply Column and Row Styles to Existing Cells This is a fix for issue #1712. When a style is applied to an entire row or column, it is currently only effective for cells which don't already contain a value. The code needs to iterate through existing cells in the row/column in order to apply the style to them. This could be considered a breaking change, however, I believe that the change makes things operate as users would expect, and that the existing implementation is incomplete. The change also removes protected element conditionalStyles from the Style class. That element is an unused remnant, and can no longer be set or retrieved - methods getConditionalStyles and setConditionalStyles actually act on an element in the Worksheet class. Finally, additional tests are added so that Style, and in fact the entire Style directory, now has 100% test coverage. * Scrutinizer Changes Scrutinizer flagged 6 statements. 5 can be easily corrected. One is absolutely wrong (it thinks iterating through cells in column can return null). Let's see if we can satisfy it. * Remove Exception For CellIterator on Empty Row/Column For my first attempt at this change, which corrects a bug by updating styles for non-empty cells when a style is set on a row or column, I wished to make things more efficient by using setIterateOnlyExistingCells, something which the existing documentation recommends. This caused an exception to be generated when the row or column is empty. So I removed that part of the change while I researched what was going on. I have completed that research. The existing code does throw an exception when the row/column is empty and iterateOnlyExistingCells is true. However, that does not seem like a reasonable action. This situation is analagous to iterating over an empty array, and that action is legal and does not throw. The same should apply here. There were no tests for this situation, and now there are. I have added additional tests, and coverage for all of RowCellIterator, ColumnCellIterator, and CellIterator are all now 100%. Some of my new tests were added in new members, because the existing tests all relied on mocking, which was not the best choice for the new tests. One of the existing tests for RowCellIteratorTest (testSeekOutOfRange) was wrong; it issued the expected exception, but for the wrong reason. I have added an additional test to ensure that it fails "correctly". The existing documentation says that the default value for IterateOnlyExistingCells is true. In fact, the default value is false. I have corrected the documentation. * More Scrutinizer I believe its analysis is incorrect, but this should silence it. * DocBlock Correction ColumnCellIterator DocBlock for current indicated it could return null or Cell, but it can really return only Cell. This had caused Scrutinizer to complain earlier. * PHP8 Environment Appears to be Fixed Cosmetic change to Doc member. I suspect there is a way to rerun all the tests without another push, but I have been unable to figure out how.
2020-12-10 17:19:56 +00:00
$this->expectExceptionMessage('Column A is out of range');
2018-01-28 06:59:38 +00:00
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
Apply Column and Row Styles to Existing Cells (#1721) * Apply Column and Row Styles to Existing Cells This is a fix for issue #1712. When a style is applied to an entire row or column, it is currently only effective for cells which don't already contain a value. The code needs to iterate through existing cells in the row/column in order to apply the style to them. This could be considered a breaking change, however, I believe that the change makes things operate as users would expect, and that the existing implementation is incomplete. The change also removes protected element conditionalStyles from the Style class. That element is an unused remnant, and can no longer be set or retrieved - methods getConditionalStyles and setConditionalStyles actually act on an element in the Worksheet class. Finally, additional tests are added so that Style, and in fact the entire Style directory, now has 100% test coverage. * Scrutinizer Changes Scrutinizer flagged 6 statements. 5 can be easily corrected. One is absolutely wrong (it thinks iterating through cells in column can return null). Let's see if we can satisfy it. * Remove Exception For CellIterator on Empty Row/Column For my first attempt at this change, which corrects a bug by updating styles for non-empty cells when a style is set on a row or column, I wished to make things more efficient by using setIterateOnlyExistingCells, something which the existing documentation recommends. This caused an exception to be generated when the row or column is empty. So I removed that part of the change while I researched what was going on. I have completed that research. The existing code does throw an exception when the row/column is empty and iterateOnlyExistingCells is true. However, that does not seem like a reasonable action. This situation is analagous to iterating over an empty array, and that action is legal and does not throw. The same should apply here. There were no tests for this situation, and now there are. I have added additional tests, and coverage for all of RowCellIterator, ColumnCellIterator, and CellIterator are all now 100%. Some of my new tests were added in new members, because the existing tests all relied on mocking, which was not the best choice for the new tests. One of the existing tests for RowCellIteratorTest (testSeekOutOfRange) was wrong; it issued the expected exception, but for the wrong reason. I have added an additional test to ensure that it fails "correctly". The existing documentation says that the default value for IterateOnlyExistingCells is true. In fact, the default value is false. I have corrected the documentation. * More Scrutinizer I believe its analysis is incorrect, but this should silence it. * DocBlock Correction ColumnCellIterator DocBlock for current indicated it could return null or Cell, but it can really return only Cell. This had caused Scrutinizer to complain earlier. * PHP8 Environment Appears to be Fixed Cosmetic change to Doc member. I suspect there is a way to rerun all the tests without another push, but I have been unable to figure out how.
2020-12-10 17:19:56 +00:00
self::assertFalse($iterator->getIterateOnlyExistingCells());
self::assertEquals(2, $iterator->getCurrentColumnIndex());
$iterator->seek('A');
}
public function testSeekNotExisting(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$this->expectExceptionMessage('Cell does not exist');
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
$iterator->setIterateOnlyExistingCells(true);
$iterator->seek('B');
}
2020-05-18 04:49:57 +00:00
public function testPrevOutOfRange(): void
{
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
$iterator->prev();
self::assertFalse($iterator->valid());
}
}