PhpSpreadsheet/tests/PhpSpreadsheetTests/Worksheet/RowIteratorTest.php

83 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
2016-03-22 14:35:50 +00:00
use PhpOffice\PhpSpreadsheet\Worksheet\Row;
use PhpOffice\PhpSpreadsheet\Worksheet\RowIterator;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class RowIteratorTest extends TestCase
{
public $mockWorksheet;
2018-01-28 06:59:38 +00:00
public $mockRow;
2020-04-27 10:28:36 +00:00
protected function setUp(): void
2015-05-17 13:00:02 +00:00
{
$this->mockRow = $this->getMockBuilder(Row::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('getHighestRow')
2020-05-18 04:49:57 +00:00
->willReturn(5);
}
2020-05-18 04:49:57 +00:00
public function testIteratorFullRange(): void
2015-05-17 13:00:02 +00:00
{
$iterator = new RowIterator($this->mockWorksheet);
$rowIndexResult = 1;
self::assertEquals($rowIndexResult, $iterator->key());
2015-05-17 13:00:02 +00:00
foreach ($iterator as $key => $row) {
self::assertEquals($rowIndexResult++, $key);
self::assertInstanceOf(Row::class, $row);
}
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 RowIterator($this->mockWorksheet, 2, 4);
$rowIndexResult = 2;
self::assertEquals($rowIndexResult, $iterator->key());
2015-05-17 13:00:02 +00:00
foreach ($iterator as $key => $row) {
self::assertEquals($rowIndexResult++, $key);
self::assertInstanceOf(Row::class, $row);
}
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
{
$iterator = new RowIterator($this->mockWorksheet, 2, 4);
$columnIndexResult = 4;
$iterator->seek(4);
self::assertEquals($columnIndexResult, $iterator->key());
for ($i = 1; $i < $columnIndexResult - 1; ++$i) {
$iterator->prev();
self::assertEquals($columnIndexResult - $i, $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);
$iterator = new RowIterator($this->mockWorksheet, 2, 4);
$iterator->seek(1);
}
2020-05-18 04:49:57 +00:00
public function testPrevOutOfRange(): void
{
$iterator = new RowIterator($this->mockWorksheet, 2, 4);
$iterator->prev();
self::assertFalse($iterator->valid());
}
}