Skip to content

Commit

Permalink
Unit tests for row and column iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Apr 27, 2015
1 parent d2b0ef2 commit f317842
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


class ColumnTest extends PHPUnit_Framework_TestCase
class AutofilterColumnTest extends PHPUnit_Framework_TestCase
{
private $_testInitialColumn = 'H';

Expand Down
87 changes: 87 additions & 0 deletions unitTests/Classes/PHPExcel/Worksheet/ColumnCellIteratorTest.php
Original file line number Diff line number Diff line change
@@ -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();
}

}
89 changes: 89 additions & 0 deletions unitTests/Classes/PHPExcel/Worksheet/RowCellIteratorTest.php
Original file line number Diff line number Diff line change
@@ -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();
}

}
7 changes: 5 additions & 2 deletions unitTests/Classes/PHPExcel/Worksheet/WorksheetColumnTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class ColumnTest extends PHPUnit_Framework_TestCase
class WorksheetColumnTest extends PHPUnit_Framework_TestCase
{
public $mockWorksheet;
public $mockColumn;
Expand All @@ -15,6 +15,9 @@ public function setUp()
$this->mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet')
->disableOriginalConstructor()
->getMock();
$this->mockWorksheet->expects($this->any())
->method('getHighestRow')
->will($this->returnValue(5));
}


Expand All @@ -38,6 +41,6 @@ public function testGetCellIterator()
{
$column = new PHPExcel_Worksheet_Column($this->mockWorksheet);
$cellIterator = $column->getCellIterator();
$this->assertInstanceOf('PHPExcel_Worksheet_CellIterator', $cellIterator);
$this->assertInstanceOf('PHPExcel_Worksheet_ColumnCellIterator', $cellIterator);
}
}
5 changes: 4 additions & 1 deletion unitTests/Classes/PHPExcel/Worksheet/WorksheetRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public function setUp()
$this->mockWorksheet = $this->getMockBuilder('PHPExcel_Worksheet')
->disableOriginalConstructor()
->getMock();
$this->mockWorksheet->expects($this->any())
->method('getHighestColumn')
->will($this->returnValue('E'));
}


Expand All @@ -38,6 +41,6 @@ public function testGetCellIterator()
{
$row = new PHPExcel_Worksheet_Row($this->mockWorksheet);
$cellIterator = $row->getCellIterator();
$this->assertInstanceOf('PHPExcel_Worksheet_CellIterator', $cellIterator);
$this->assertInstanceOf('PHPExcel_Worksheet_RowCellIterator', $cellIterator);
}
}

0 comments on commit f317842

Please sign in to comment.