PhpSpreadsheet/tests/PhpSpreadsheetTests/Worksheet/RowCellIteratorTest.php

89 lines
2.7 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;
2015-05-17 13:00:02 +00:00
public function setUp()
{
$this->mockCell = $this->getMockBuilder(Cell::class)
->disableOriginalConstructor()
->getMock();
$this->mockWorksheet = $this->getMockBuilder(Worksheet::class)
->disableOriginalConstructor()
->getMock();
$this->mockWorksheet->expects($this->any())
->method('getHighestColumn')
->will($this->returnValue('E'));
$this->mockWorksheet->expects($this->any())
->method('getCellByColumnAndRow')
->will($this->returnValue($this->mockCell));
}
2015-05-17 13:00:02 +00:00
public function testIteratorFullRange()
{
$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
}
2015-05-17 13:00:02 +00:00
public function testIteratorStartEndRange()
{
$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
}
2015-05-17 13:00:02 +00:00
public function testIteratorSeekAndPrev()
{
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
}
public function testSeekOutOfRange()
{
2018-01-28 06:59:38 +00:00
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
$iterator->seek(1);
}
public function testPrevOutOfRange()
{
2018-01-28 06:59:38 +00:00
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$iterator = new RowCellIterator($this->mockWorksheet, 2, 'B', 'D');
$iterator->prev();
}
}