View source
<?php
class AutoFilterTest extends PHPUnit_Framework_TestCase {
private $_testInitialRange = 'H2:O256';
private $_testAutoFilterObject;
public function setUp() {
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php';
$this->_mockWorksheetObject = $this
->getMockBuilder('PHPExcel_Worksheet')
->disableOriginalConstructor()
->getMock();
$this->_mockCacheController = $this
->getMockBuilder('PHPExcel_CachedObjectStorage_Memory')
->disableOriginalConstructor()
->getMock();
$this->_mockWorksheetObject
->expects($this
->any())
->method('getCellCacheController')
->will($this
->returnValue($this->_mockCacheController));
$this->_testAutoFilterObject = new PHPExcel_Worksheet_AutoFilter($this->_testInitialRange, $this->_mockWorksheetObject);
}
public function testToString() {
$expectedResult = $this->_testInitialRange;
$result = $this->_testAutoFilterObject;
$this
->assertEquals($expectedResult, $result);
}
public function testGetParent() {
$result = $this->_testAutoFilterObject
->getParent();
$this
->assertInstanceOf('PHPExcel_Worksheet', $result);
}
public function testSetParent() {
$result = $this->_testAutoFilterObject
->setParent($this->_mockWorksheetObject);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
}
public function testGetRange() {
$expectedResult = $this->_testInitialRange;
$result = $this->_testAutoFilterObject
->getRange();
$this
->assertEquals($expectedResult, $result);
}
public function testSetRange() {
$ranges = array(
'G1:J512' => 'Worksheet1!G1:J512',
'K1:N20' => 'K1:N20',
);
foreach ($ranges as $actualRange => $fullRange) {
$result = $this->_testAutoFilterObject
->setRange($fullRange);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
$result = $this->_testAutoFilterObject
->getRange();
$this
->assertEquals($actualRange, $result);
}
}
public function testClearRange() {
$expectedResult = '';
$result = $this->_testAutoFilterObject
->setRange();
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
$result = $this->_testAutoFilterObject
->getRange();
$this
->assertEquals($expectedResult, $result);
}
public function testSetRangeInvalidRange() {
$expectedResult = 'A1';
$result = $this->_testAutoFilterObject
->setRange($expectedResult);
}
public function testGetColumnsEmpty() {
$result = $this->_testAutoFilterObject
->getColumns();
$this
->assertInternalType('array', $result);
$this
->assertEquals(0, count($result));
}
public function testGetColumnOffset() {
$columnIndexes = array(
'H' => 0,
'K' => 3,
'M' => 5,
);
foreach ($columnIndexes as $columnIndex => $columnOffset) {
$result = $this->_testAutoFilterObject
->getColumnOffset($columnIndex);
$this
->assertEquals($columnOffset, $result);
}
}
public function testGetInvalidColumnOffset() {
$invalidColumn = 'G';
$result = $this->_testAutoFilterObject
->getColumnOffset($invalidColumn);
}
public function testSetColumnWithString() {
$expectedResult = 'L';
$result = $this->_testAutoFilterObject
->setColumn($expectedResult);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
$result = $this->_testAutoFilterObject
->getColumns();
$this
->assertInternalType('array', $result);
$this
->assertEquals(1, count($result));
$this
->assertArrayHasKey($expectedResult, $result);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result[$expectedResult]);
}
public function testSetInvalidColumnWithString() {
$invalidColumn = 'A';
$result = $this->_testAutoFilterObject
->setColumn($invalidColumn);
}
public function testSetColumnWithColumnObject() {
$expectedResult = 'M';
$columnObject = new PHPExcel_Worksheet_AutoFilter_Column($expectedResult);
$result = $this->_testAutoFilterObject
->setColumn($columnObject);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
$result = $this->_testAutoFilterObject
->getColumns();
$this
->assertInternalType('array', $result);
$this
->assertEquals(1, count($result));
$this
->assertArrayHasKey($expectedResult, $result);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result[$expectedResult]);
}
public function testSetInvalidColumnWithObject() {
$invalidColumn = 'E';
$columnObject = new PHPExcel_Worksheet_AutoFilter_Column($invalidColumn);
$result = $this->_testAutoFilterObject
->setColumn($invalidColumn);
}
public function testSetColumnWithInvalidDataType() {
$invalidColumn = 123.456;
$columnObject = new PHPExcel_Worksheet_AutoFilter_Column($invalidColumn);
$result = $this->_testAutoFilterObject
->setColumn($invalidColumn);
}
public function testGetColumns() {
$columnIndexes = array(
'L',
'M',
);
foreach ($columnIndexes as $columnIndex) {
$this->_testAutoFilterObject
->setColumn($columnIndex);
}
$result = $this->_testAutoFilterObject
->getColumns();
$this
->assertInternalType('array', $result);
$this
->assertEquals(count($columnIndexes), count($result));
foreach ($columnIndexes as $columnIndex) {
$this
->assertArrayHasKey($columnIndex, $result);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result[$columnIndex]);
}
}
public function testGetColumn() {
$columnIndexes = array(
'L',
'M',
);
foreach ($columnIndexes as $columnIndex) {
$this->_testAutoFilterObject
->setColumn($columnIndex);
}
foreach ($columnIndexes as $columnIndex) {
$result = $this->_testAutoFilterObject
->getColumn($columnIndex);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
}
}
public function testGetColumnByOffset() {
$columnIndexes = array(
0 => 'H',
3 => 'K',
5 => 'M',
);
foreach ($columnIndexes as $columnIndex => $columnID) {
$result = $this->_testAutoFilterObject
->getColumnByOffset($columnIndex);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
$this
->assertEquals($result
->getColumnIndex(), $columnID);
}
}
public function testGetColumnIfNotSet() {
$result = $this->_testAutoFilterObject
->getColumn('K');
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
}
public function testGetColumnWithoutRangeSet() {
$result = $this->_testAutoFilterObject
->setRange();
$result = $this->_testAutoFilterObject
->getColumn('A');
}
public function testClearRangeWithExistingColumns() {
$expectedResult = '';
$columnIndexes = array(
'L',
'M',
'N',
);
foreach ($columnIndexes as $columnIndex) {
$this->_testAutoFilterObject
->setColumn($columnIndex);
}
$result = $this->_testAutoFilterObject
->setRange();
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
$result = $this->_testAutoFilterObject
->getRange();
$this
->assertEquals($expectedResult, $result);
$result = $this->_testAutoFilterObject
->getColumns();
$this
->assertInternalType('array', $result);
$this
->assertEquals(0, count($result));
}
public function testSetRangeWithExistingColumns() {
$expectedResult = 'G1:J512';
$columnIndexes1 = array(
'I',
'J',
);
foreach ($columnIndexes1 as $columnIndex) {
$this->_testAutoFilterObject
->setColumn($columnIndex);
}
$columnIndexes2 = array(
'K',
'L',
'M',
);
foreach ($columnIndexes2 as $columnIndex) {
$this->_testAutoFilterObject
->setColumn($columnIndex);
}
$result = $this->_testAutoFilterObject
->setRange($expectedResult);
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
$result = $this->_testAutoFilterObject
->getRange();
$this
->assertEquals($expectedResult, $result);
$result = $this->_testAutoFilterObject
->getColumns();
$this
->assertInternalType('array', $result);
$this
->assertEquals(count($columnIndexes1), count($result));
}
public function testClone() {
$columnIndexes = array(
'L',
'M',
);
foreach ($columnIndexes as $columnIndex) {
$this->_testAutoFilterObject
->setColumn($columnIndex);
}
$result = clone $this->_testAutoFilterObject;
$this
->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
}
}