From f317842e2bdc59f3fce1ffb462c72d195e9703ca Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Mon, 27 Apr 2015 22:47:47 +0100 Subject: [PATCH] Unit tests for row and column iterators --- .../Worksheet/AutoFilter/ColumnTest.php | 2 +- .../Worksheet/ColumnCellIteratorTest.php | 87 ++++++++++++++++++ .../Worksheet/RowCellIteratorTest.php | 89 +++++++++++++++++++ .../Worksheet/WorksheetColumnTest.php | 7 +- .../PHPExcel/Worksheet/WorksheetRowTest.php | 5 +- 5 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 unitTests/Classes/PHPExcel/Worksheet/ColumnCellIteratorTest.php create mode 100644 unitTests/Classes/PHPExcel/Worksheet/RowCellIteratorTest.php diff --git a/unitTests/Classes/PHPExcel/Worksheet/AutoFilter/ColumnTest.php b/unitTests/Classes/PHPExcel/Worksheet/AutoFilter/ColumnTest.php index c8411f5e..3c1821ba 100644 --- a/unitTests/Classes/PHPExcel/Worksheet/AutoFilter/ColumnTest.php +++ b/unitTests/Classes/PHPExcel/Worksheet/AutoFilter/ColumnTest.php @@ -1,7 +1,7 @@ mockCell = $this->getMockBuilder('PHPExcel_Cell') + ->disableOriginalConstructor() + ->getMock(); + + $this->mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet') + ->disableOriginalConstructor() + ->getMock(); + + $this->mockWorksheet->expects($this->any()) + ->method('getHighestRow') + ->will($this->returnValue(5)); + $this->mockWorksheet->expects($this->any()) + ->method('getCellByColumnAndRow') + ->will($this->returnValue($this->mockCell)); + } + + + public function testIteratorFullRange() + { + $iterator = new PHPExcel_Worksheet_ColumnCellIterator($this->mockWorksheet, 'A'); + $ColumnCellIndexResult = 1; + $this->assertEquals($ColumnCellIndexResult, $iterator->key()); + + foreach($iterator as $key => $ColumnCell) { + $this->assertEquals($ColumnCellIndexResult++, $key); + $this->assertInstanceOf('PHPExcel_Cell', $ColumnCell); + } + } + + public function testIteratorStartEndRange() + { + $iterator = new PHPExcel_Worksheet_ColumnCellIterator($this->mockWorksheet, 'A', 2, 4); + $ColumnCellIndexResult = 2; + $this->assertEquals($ColumnCellIndexResult, $iterator->key()); + + foreach($iterator as $key => $ColumnCell) { + $this->assertEquals($ColumnCellIndexResult++, $key); + $this->assertInstanceOf('PHPExcel_Cell', $ColumnCell); + } + } + + public function testIteratorSeekAndPrev() + { + $iterator = new PHPExcel_Worksheet_ColumnCellIterator($this->mockWorksheet, 'A', 2, 4); + $columnIndexResult = 4; + $iterator->seek(4); + $this->assertEquals($columnIndexResult, $iterator->key()); + + for($i = 1; $i < $columnIndexResult-1; $i++) { + $iterator->prev(); + $this->assertEquals($columnIndexResult - $i, $iterator->key()); + } + } + + /** + * @expectedException PHPExcel_Exception + */ + public function testSeekOutOfRange() + { + $iterator = new PHPExcel_Worksheet_ColumnCellIterator($this->mockWorksheet, 'A', 2, 4); + $iterator->seek(1); + } + + /** + * @expectedException PHPExcel_Exception + */ + public function testPrevOutOfRange() + { + $iterator = new PHPExcel_Worksheet_ColumnCellIterator($this->mockWorksheet, 'A', 2, 4); + $iterator->prev(); + } + +} diff --git a/unitTests/Classes/PHPExcel/Worksheet/RowCellIteratorTest.php b/unitTests/Classes/PHPExcel/Worksheet/RowCellIteratorTest.php new file mode 100644 index 00000000..cdd3c84d --- /dev/null +++ b/unitTests/Classes/PHPExcel/Worksheet/RowCellIteratorTest.php @@ -0,0 +1,89 @@ +mockCell = $this->getMockBuilder('PHPExcel_Cell') + ->disableOriginalConstructor() + ->getMock(); + + $this->mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet') + ->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)); + } + + + public function testIteratorFullRange() + { + $iterator = new PHPExcel_Worksheet_RowCellIterator($this->mockWorksheet); + $RowCellIndexResult = 'A'; + $this->assertEquals($RowCellIndexResult, $iterator->key()); + + foreach($iterator as $key => $RowCell) { + $this->assertEquals($RowCellIndexResult++, $key); + $this->assertInstanceOf('PHPExcel_Cell', $RowCell); + } + } + + public function testIteratorStartEndRange() + { + $iterator = new PHPExcel_Worksheet_RowCellIterator($this->mockWorksheet, 2, 'B', 'D'); + $RowCellIndexResult = 'B'; + $this->assertEquals($RowCellIndexResult, $iterator->key()); + + foreach($iterator as $key => $RowCell) { + $this->assertEquals($RowCellIndexResult++, $key); + $this->assertInstanceOf('PHPExcel_Cell', $RowCell); + } + } + + public function testIteratorSeekAndPrev() + { + $ranges = range('A','E'); + $iterator = new PHPExcel_Worksheet_RowCellIterator($this->mockWorksheet, 2, 'B', 'D'); + $RowCellIndexResult = 'D'; + $iterator->seek('D'); + $this->assertEquals($RowCellIndexResult, $iterator->key()); + + for($i = 1; $i < array_search($RowCellIndexResult, $ranges); $i++) { + $iterator->prev(); + $expectedResult = $ranges[array_search($RowCellIndexResult, $ranges) - $i]; + $this->assertEquals($expectedResult, $iterator->key()); + } + } + + /** + * @expectedException PHPExcel_Exception + */ + public function testSeekOutOfRange() + { + $iterator = new PHPExcel_Worksheet_RowCellIterator($this->mockWorksheet, 2, 'B', 'D'); + $iterator->seek(1); + } + + /** + * @expectedException PHPExcel_Exception + */ + public function testPrevOutOfRange() + { + $iterator = new PHPExcel_Worksheet_RowCellIterator($this->mockWorksheet, 2, 'B', 'D'); + $iterator->prev(); + } + +} diff --git a/unitTests/Classes/PHPExcel/Worksheet/WorksheetColumnTest.php b/unitTests/Classes/PHPExcel/Worksheet/WorksheetColumnTest.php index a867bd7d..c70c38d3 100644 --- a/unitTests/Classes/PHPExcel/Worksheet/WorksheetColumnTest.php +++ b/unitTests/Classes/PHPExcel/Worksheet/WorksheetColumnTest.php @@ -1,6 +1,6 @@ mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet') ->disableOriginalConstructor() ->getMock(); + $this->mockWorksheet->expects($this->any()) + ->method('getHighestRow') + ->will($this->returnValue(5)); } @@ -38,6 +41,6 @@ class ColumnTest extends PHPUnit_Framework_TestCase { $column = new PHPExcel_Worksheet_Column($this->mockWorksheet); $cellIterator = $column->getCellIterator(); - $this->assertInstanceOf('PHPExcel_Worksheet_CellIterator', $cellIterator); + $this->assertInstanceOf('PHPExcel_Worksheet_ColumnCellIterator', $cellIterator); } } diff --git a/unitTests/Classes/PHPExcel/Worksheet/WorksheetRowTest.php b/unitTests/Classes/PHPExcel/Worksheet/WorksheetRowTest.php index dd80e0d5..2761b52b 100644 --- a/unitTests/Classes/PHPExcel/Worksheet/WorksheetRowTest.php +++ b/unitTests/Classes/PHPExcel/Worksheet/WorksheetRowTest.php @@ -15,6 +15,9 @@ class WorksheetRowTest extends PHPUnit_Framework_TestCase $this->mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet') ->disableOriginalConstructor() ->getMock(); + $this->mockWorksheet->expects($this->any()) + ->method('getHighestColumn') + ->will($this->returnValue('E')); } @@ -38,6 +41,6 @@ class WorksheetRowTest extends PHPUnit_Framework_TestCase { $row = new PHPExcel_Worksheet_Row($this->mockWorksheet); $cellIterator = $row->getCellIterator(); - $this->assertInstanceOf('PHPExcel_Worksheet_CellIterator', $cellIterator); + $this->assertInstanceOf('PHPExcel_Worksheet_RowCellIterator', $cellIterator); } }