Unit tests for row and column iterators

This commit is contained in:
MarkBaker 2015-04-27 22:47:47 +01:00
parent d2b0ef2ad9
commit f317842e2b
5 changed files with 186 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<?php
class ColumnTest extends PHPUnit_Framework_TestCase
class AutofilterColumnTest extends PHPUnit_Framework_TestCase
{
private $_testInitialColumn = 'H';

View File

@ -0,0 +1,87 @@
<?php
class ColumnCellIteratorTest extends PHPUnit_Framework_TestCase
{
public $mockWorksheet;
public $mockColumnCell;
public function setUp()
{
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
$this->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();
}
}

View File

@ -0,0 +1,89 @@
<?php
class RowCellIteratorTest extends PHPUnit_Framework_TestCase
{
public $mockWorksheet;
public $mockRowCell;
public function setUp()
{
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
$this->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();
}
}

View File

@ -1,6 +1,6 @@
<?php
class ColumnTest extends PHPUnit_Framework_TestCase
class WorksheetColumnTest extends PHPUnit_Framework_TestCase
{
public $mockWorksheet;
public $mockColumn;
@ -15,6 +15,9 @@ class ColumnTest extends PHPUnit_Framework_TestCase
$this->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);
}
}

View File

@ -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);
}
}