2015-04-26 12:00:58 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
2016-08-13 17:22:11 +00:00
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Column;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnIterator;
|
2017-05-17 22:02:17 +00:00
|
|
|
use PHPUnit_Framework_TestCase;
|
2015-05-30 11:25:17 +00:00
|
|
|
|
2017-05-17 22:02:17 +00:00
|
|
|
class ColumnIteratorTest extends PHPUnit_Framework_TestCase
|
2015-04-26 12:00:58 +00:00
|
|
|
{
|
|
|
|
public $mockWorksheet;
|
|
|
|
public $mockColumn;
|
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->mockColumn = $this->getMockBuilder(Column::class)
|
2015-04-26 12:00:58 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->mockWorksheet = $this->getMockBuilder(Worksheet::class)
|
2015-04-26 12:00:58 +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-26 12:00:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testIteratorFullRange()
|
|
|
|
{
|
2015-05-30 11:25:17 +00:00
|
|
|
$iterator = new ColumnIterator($this->mockWorksheet);
|
2015-04-26 12:00:58 +00:00
|
|
|
$columnIndexResult = 'A';
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($columnIndexResult, $iterator->key());
|
2016-08-13 17:22:11 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
foreach ($iterator as $key => $column) {
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($columnIndexResult++, $key);
|
|
|
|
self::assertInstanceOf(Column::class, $column);
|
2015-04-26 12:00:58 +00:00
|
|
|
}
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-26 12:00:58 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testIteratorStartEndRange()
|
|
|
|
{
|
2015-05-30 11:25:17 +00:00
|
|
|
$iterator = new ColumnIterator($this->mockWorksheet, 'B', 'D');
|
2015-04-26 12:00:58 +00:00
|
|
|
$columnIndexResult = 'B';
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($columnIndexResult, $iterator->key());
|
2016-08-13 17:22:11 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
foreach ($iterator as $key => $column) {
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($columnIndexResult++, $key);
|
|
|
|
self::assertInstanceOf(Column::class, $column);
|
2015-04-26 12:00:58 +00:00
|
|
|
}
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-26 12:00:58 +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');
|
2015-05-30 11:25:17 +00:00
|
|
|
$iterator = new ColumnIterator($this->mockWorksheet, 'B', 'D');
|
2015-04-26 12:00:58 +00:00
|
|
|
$columnIndexResult = 'D';
|
|
|
|
$iterator->seek('D');
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($columnIndexResult, $iterator->key());
|
2015-04-26 12:00:58 +00:00
|
|
|
|
2016-08-16 15:33:57 +00:00
|
|
|
for ($i = 1; $i < array_search($columnIndexResult, $ranges); ++$i) {
|
2015-04-26 12:00:58 +00:00
|
|
|
$iterator->prev();
|
|
|
|
$expectedResult = $ranges[array_search($columnIndexResult, $ranges) - $i];
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $iterator->key());
|
2015-04-26 12:00:58 +00:00
|
|
|
}
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-26 12:00:58 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-31 17:18:12 +00:00
|
|
|
* @expectedException \PhpOffice\PhpSpreadsheet\Exception
|
2015-04-26 12:00:58 +00:00
|
|
|
*/
|
|
|
|
public function testSeekOutOfRange()
|
|
|
|
{
|
2015-05-30 11:25:17 +00:00
|
|
|
$iterator = new ColumnIterator($this->mockWorksheet, 'B', 'D');
|
2015-04-27 22:07:30 +00:00
|
|
|
$iterator->seek('A');
|
2015-04-26 12:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-31 17:18:12 +00:00
|
|
|
* @expectedException \PhpOffice\PhpSpreadsheet\Exception
|
2015-04-26 12:00:58 +00:00
|
|
|
*/
|
|
|
|
public function testPrevOutOfRange()
|
|
|
|
{
|
2015-05-30 11:25:17 +00:00
|
|
|
$iterator = new ColumnIterator($this->mockWorksheet, 'B', 'D');
|
2015-04-26 12:00:58 +00:00
|
|
|
$iterator->prev();
|
|
|
|
}
|
|
|
|
}
|