2015-04-27 21:47:47 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
2016-03-22 14:35:50 +00:00
|
|
|
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\RowCellIterator;
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class RowCellIteratorTest extends TestCase
|
2015-04-27 21:47:47 +00:00
|
|
|
{
|
|
|
|
public $mockWorksheet;
|
2018-01-28 06:59:38 +00:00
|
|
|
|
2017-10-07 17:00:30 +00:00
|
|
|
public $mockCell;
|
2015-04-27 21:47:47 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->mockCell = $this->getMockBuilder(Cell::class)
|
2015-04-27 21:47:47 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->mockWorksheet = $this->getMockBuilder(Worksheet::class)
|
2015-04-27 21:47:47 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->mockWorksheet->expects($this->any())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('getHighestColumn')
|
|
|
|
->will($this->returnValue('E'));
|
2015-04-27 21:47:47 +00:00
|
|
|
$this->mockWorksheet->expects($this->any())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('getCellByColumnAndRow')
|
|
|
|
->will($this->returnValue($this->mockCell));
|
2015-04-27 21:47:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testIteratorFullRange()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$iterator = new RowCellIterator($this->mockWorksheet);
|
2015-04-27 21:47:47 +00:00
|
|
|
$RowCellIndexResult = 'A';
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($RowCellIndexResult, $iterator->key());
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
foreach ($iterator as $key => $RowCell) {
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($RowCellIndexResult++, $key);
|
|
|
|
self::assertInstanceOf(Cell::class, $RowCell);
|
2015-04-27 21:47:47 +00:00
|
|
|
}
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-27 21:47:47 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testIteratorStartEndRange()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
|
2015-04-27 21:47:47 +00:00
|
|
|
$RowCellIndexResult = 'B';
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($RowCellIndexResult, $iterator->key());
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
foreach ($iterator as $key => $RowCell) {
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($RowCellIndexResult++, $key);
|
|
|
|
self::assertInstanceOf(Cell::class, $RowCell);
|
2015-04-27 21:47:47 +00:00
|
|
|
}
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-27 21:47:47 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testIteratorSeekAndPrev()
|
|
|
|
{
|
2015-05-17 17:26:34 +00:00
|
|
|
$ranges = range('A', 'E');
|
2016-08-14 04:08:43 +00:00
|
|
|
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
|
2015-04-27 21:47:47 +00:00
|
|
|
$RowCellIndexResult = 'D';
|
|
|
|
$iterator->seek('D');
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($RowCellIndexResult, $iterator->key());
|
2015-04-27 21:47:47 +00:00
|
|
|
|
2016-08-16 15:33:57 +00:00
|
|
|
for ($i = 1; $i < array_search($RowCellIndexResult, $ranges); ++$i) {
|
2015-04-27 21:47:47 +00:00
|
|
|
$iterator->prev();
|
|
|
|
$expectedResult = $ranges[array_search($RowCellIndexResult, $ranges) - $i];
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $iterator->key());
|
2015-04-27 21:47:47 +00:00
|
|
|
}
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-27 21:47:47 +00:00
|
|
|
|
|
|
|
public function testSeekOutOfRange()
|
|
|
|
{
|
2018-01-28 06:59:38 +00:00
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
|
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
|
2015-04-27 21:47:47 +00:00
|
|
|
$iterator->seek(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrevOutOfRange()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
|
2015-04-27 21:47:47 +00:00
|
|
|
$iterator->prev();
|
2018-08-07 07:38:18 +00:00
|
|
|
self::assertFalse($iterator->valid());
|
2015-04-27 21:47:47 +00:00
|
|
|
}
|
|
|
|
}
|