AutoFilter unit tests

This commit is contained in:
Mark Baker 2012-08-19 23:17:12 +01:00
parent 61bc7c8e98
commit b00f6d4c4c
7 changed files with 343 additions and 30 deletions

View File

@ -154,7 +154,7 @@ class PHPExcel_Worksheet_AutoFilter
* @throws PHPExcel_Exception * @throws PHPExcel_Exception
* @return integer The column offset within the autofilter range * @return integer The column offset within the autofilter range
*/ */
protected function _testColumnInRange($column) { public function testColumnInRange($column) {
if (empty($this->_range)) { if (empty($this->_range)) {
throw new PHPExcel_Exception("No autofilter range is defined."); throw new PHPExcel_Exception("No autofilter range is defined.");
} }
@ -176,7 +176,7 @@ class PHPExcel_Worksheet_AutoFilter
* @return integer The offset of the specified column within the autofilter range * @return integer The offset of the specified column within the autofilter range
*/ */
public function getColumnOffset($pColumn) { public function getColumnOffset($pColumn) {
return $this->_testColumnInRange($pColumn); return $this->testColumnInRange($pColumn);
} }
/** /**
@ -187,7 +187,7 @@ class PHPExcel_Worksheet_AutoFilter
* @return PHPExcel_Worksheet_AutoFilter_Column * @return PHPExcel_Worksheet_AutoFilter_Column
*/ */
public function getColumn($pColumn) { public function getColumn($pColumn) {
$this->_testColumnInRange($pColumn); $this->testColumnInRange($pColumn);
if (!isset($this->_columns[$pColumn])) { if (!isset($this->_columns[$pColumn])) {
$this->_columns[$pColumn] = new PHPExcel_Worksheet_AutoFilter_Column($pColumn, $this); $this->_columns[$pColumn] = new PHPExcel_Worksheet_AutoFilter_Column($pColumn, $this);
@ -227,7 +227,7 @@ class PHPExcel_Worksheet_AutoFilter
} else { } else {
throw new PHPExcel_Exception("Column is not within the autofilter range."); throw new PHPExcel_Exception("Column is not within the autofilter range.");
} }
$this->_testColumnInRange($column); $this->testColumnInRange($column);
if (is_string($pColumn)) { if (is_string($pColumn)) {
$this->_columns[$pColumn] = new PHPExcel_Worksheet_AutoFilter_Column($pColumn, $this); $this->_columns[$pColumn] = new PHPExcel_Worksheet_AutoFilter_Column($pColumn, $this);
@ -239,6 +239,14 @@ class PHPExcel_Worksheet_AutoFilter
return $this; return $this;
} }
/**
* Test if cell value is in the defined set of values
*
* @param mixed $cellValue
* @param mixed[] $dataSet
* @return boolean
*/
private static function _filterTestInSimpleDataSet($cellValue,$dataSet) private static function _filterTestInSimpleDataSet($cellValue,$dataSet)
{ {
$dataSetValues = $dataSet['filterValues']; $dataSetValues = $dataSet['filterValues'];
@ -249,6 +257,13 @@ class PHPExcel_Worksheet_AutoFilter
return in_array($cellValue,$dataSetValues); return in_array($cellValue,$dataSetValues);
} }
/**
* Test if cell value is in the defined set of Excel date values
*
* @param mixed $cellValue
* @param mixed[] $dataSet
* @return boolean
*/
private static function _filterTestInDateGroupSet($cellValue,$dataSet) private static function _filterTestInDateGroupSet($cellValue,$dataSet)
{ {
$dateSet = $dataSet['filterValues']; $dateSet = $dataSet['filterValues'];
@ -256,6 +271,7 @@ class PHPExcel_Worksheet_AutoFilter
if (($cellValue == '') || ($cellValue === NULL)) { if (($cellValue == '') || ($cellValue === NULL)) {
return $blanks; return $blanks;
} }
if (is_numeric($cellValue)) { if (is_numeric($cellValue)) {
$dateValue = PHPExcel_Shared_Date::ExcelToPHP($cellValue); $dateValue = PHPExcel_Shared_Date::ExcelToPHP($cellValue);
if ($cellValue < 1) { if ($cellValue < 1) {
@ -281,16 +297,21 @@ class PHPExcel_Worksheet_AutoFilter
return FALSE; return FALSE;
} }
/**
* Test if cell value is within a set of values defined by a ruleset
*
* @param mixed $cellValue
* @param mixed[] $dataSet
* @return boolean
*/
private static function _filterTestInCustomDataSet($cellValue,$ruleSet) private static function _filterTestInCustomDataSet($cellValue,$ruleSet)
{ {
echo 'CALLING _filterTestInCustomDataSet',PHP_EOL; // Blank cells are always ignored, so return a FALSE
$dataSet = $ruleSet['filterRules'];
$join = $ruleSet['join'];
if (($cellValue == '') || ($cellValue === NULL)) { if (($cellValue == '') || ($cellValue === NULL)) {
echo 'EMPTY CELL',PHP_EOL;
return FALSE; return FALSE;
} }
$dataSet = $ruleSet['filterRules'];
$join = $ruleSet['join'];
$returnVal = ($join == PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND); $returnVal = ($join == PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND);
foreach($dataSet as $rule) { foreach($dataSet as $rule) {
@ -338,18 +359,23 @@ class PHPExcel_Worksheet_AutoFilter
return $returnVal; return $returnVal;
} }
private static function _filterTestInPeriodDateSet($cellValue,$testSet) /**
* Test if cell date value is matches a set of values defined by a set of months
*
* @param mixed $cellValue
* @param mixed[] $dataSet
* @return boolean
*/
private static function _filterTestInPeriodDateSet($cellValue,$monthSet)
{ {
echo 'CALLING _filterTestInPeriodDateSet',PHP_EOL; // Blank cells are always ignored, so return a FALSE
if (($cellValue == '') || ($cellValue === NULL)) { if (($cellValue == '') || ($cellValue === NULL)) {
echo 'EMPTY CELL',PHP_EOL;
return FALSE; return FALSE;
} }
if (is_numeric($cellValue)) { if (is_numeric($cellValue)) {
$dateValue = date('m',PHPExcel_Shared_Date::ExcelToPHP($cellValue)); $dateValue = date('m',PHPExcel_Shared_Date::ExcelToPHP($cellValue));
if (in_array($dateValue,$testSet)) { if (in_array($dateValue,$monthSet)) {
return TRUE; return TRUE;
} }
} }
@ -357,18 +383,28 @@ class PHPExcel_Worksheet_AutoFilter
return FALSE; return FALSE;
} }
private static function _filterTypeTopTenFilters($cellValue,$testSet) private static function _filterTypeTopTenFilters($cellValue,$testSet)
{ {
echo 'CALLING _filterTypeTopTenFilters',PHP_EOL; echo 'CALLING _filterTypeTopTenFilters',PHP_EOL;
return TRUE; return TRUE;
} }
/**
* Search/Replace arrays to convert Excel wildcard syntax to a regexp syntax for preg_matching
*
* @var array
*/
private static $_fromReplace = array('\*', '\?', '~~', '~.*', '~.?'); private static $_fromReplace = array('\*', '\?', '~~', '~.*', '~.?');
private static $_toReplace = array('.*', '.', '~', '\*', '\?'); private static $_toReplace = array('.*', '.', '~', '\*', '\?');
/**
* Convert a dynamic rule daterange to a custom filter range expression for ease of calculation
*
* @param string $dynamicRuleType
* @param PHPExcel_Worksheet_AutoFilter_Column $filterColumn
* @return mixed[]
*/
private function _dynamicFilterDateRange($dynamicRuleType, $filterColumn) private function _dynamicFilterDateRange($dynamicRuleType, $filterColumn)
{ {
$rDateType = PHPExcel_Calculation_Functions::getReturnDateType(); $rDateType = PHPExcel_Calculation_Functions::getReturnDateType();
@ -377,7 +413,7 @@ class PHPExcel_Worksheet_AutoFilter
$ruleValues = array(); $ruleValues = array();
$baseDate = PHPExcel_Calculation_DateTime::DATENOW(); $baseDate = PHPExcel_Calculation_DateTime::DATENOW();
// Calculate start/end dates for the required date range based on current date
switch ($dynamicRuleType) { switch ($dynamicRuleType) {
case PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK : case PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK :
$baseDate = strtotime('-7 days',$baseDate); $baseDate = strtotime('-7 days',$baseDate);

View File

@ -141,7 +141,7 @@ class PHPExcel_Worksheet_AutoFilter_Column
// Uppercase coordinate // Uppercase coordinate
$pColumn = strtoupper($pColumn); $pColumn = strtoupper($pColumn);
if ($this->_parent !== NULL) { if ($this->_parent !== NULL) {
$this->_parent->_testColumnInRange($pColumn); $this->_parent->testColumnInRange($pColumn);
} }
$this->_columnIndex = $pColumn; $this->_columnIndex = $pColumn;

View File

@ -0,0 +1,109 @@
<?php
class RuleTest extends PHPUnit_Framework_TestCase
{
private $_testAutoFilterRuleObject;
private $_mockAutoFilterColumnObject;
public function setUp()
{
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
$this->_mockAutoFilterColumnObject = $this->getMockBuilder('PHPExcel_Worksheet_AutoFilter_Column')
->disableOriginalConstructor()
->getMock();
$this->_mockAutoFilterColumnObject->expects($this->any())
->method('testColumnInRange')
->will($this->returnValue(3));
$this->_testAutoFilterRuleObject = new PHPExcel_Worksheet_AutoFilter_Column_Rule(
$this->_mockAutoFilterColumnObject
);
}
public function testGetRuleType()
{
$result = $this->_testAutoFilterRuleObject->getRuleType();
$this->assertEquals(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_FILTER, $result);
}
public function testSetRuleType()
{
$expectedResult = PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP;
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterRuleObject->setRuleType($expectedResult);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column_Rule', $result);
$result = $this->_testAutoFilterRuleObject->getRuleType();
$this->assertEquals($expectedResult, $result);
}
public function testSetValue()
{
$expectedResult = 100;
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterRuleObject->setValue($expectedResult);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column_Rule', $result);
$result = $this->_testAutoFilterRuleObject->getValue();
$this->assertEquals($expectedResult, $result);
}
public function testGetOperator()
{
$result = $this->_testAutoFilterRuleObject->getOperator();
$this->assertEquals(PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL, $result);
}
public function testSetOperator()
{
$expectedResult = PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_LESSTHAN;
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterRuleObject->setOperator($expectedResult);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column_Rule', $result);
$result = $this->_testAutoFilterRuleObject->getOperator();
$this->assertEquals($expectedResult, $result);
}
public function testSetGrouping()
{
$expectedResult = PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP_MONTH;
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterRuleObject->setGrouping($expectedResult);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column_Rule', $result);
$result = $this->_testAutoFilterRuleObject->getGrouping();
$this->assertEquals($expectedResult, $result);
}
public function testGetParent()
{
$result = $this->_testAutoFilterRuleObject->getParent();
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
}
public function testSetParent()
{
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterRuleObject->setParent($this->_mockAutoFilterColumnObject);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column_Rule', $result);
}
public function testClone()
{
$result = clone $this->_testAutoFilterRuleObject;
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column_Rule', $result);
}
}

View File

@ -7,6 +7,7 @@ class ColumnTest extends PHPUnit_Framework_TestCase
private $_testAutoFilterColumnObject; private $_testAutoFilterColumnObject;
private $_mockAutoFilterObject;
public function setUp() public function setUp()
{ {
@ -15,7 +16,18 @@ class ColumnTest extends PHPUnit_Framework_TestCase
} }
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
$this->_testAutoFilterColumnObject = new PHPExcel_Worksheet_AutoFilter_Column($this->_testInitialColumn); $this->_mockAutoFilterObject = $this->getMockBuilder('PHPExcel_Worksheet_AutoFilter')
->disableOriginalConstructor()
->getMock();
$this->_mockAutoFilterObject->expects($this->any())
->method('testColumnInRange')
->will($this->returnValue(3));
$this->_testAutoFilterColumnObject = new PHPExcel_Worksheet_AutoFilter_Column(
$this->_testInitialColumn,
$this->_mockAutoFilterObject
);
} }
public function testGetColumnIndex() public function testGetColumnIndex()
@ -36,6 +48,122 @@ class ColumnTest extends PHPUnit_Framework_TestCase
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
public function testGetParent()
{
$result = $this->_testAutoFilterColumnObject->getParent();
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
}
public function testSetParent()
{
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterColumnObject->setParent($this->_mockAutoFilterObject);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
}
public function testGetFilterType()
{
$result = $this->_testAutoFilterColumnObject->getFilterType();
$this->assertEquals(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER, $result);
}
public function testSetFilterType()
{
$result = $this->_testAutoFilterColumnObject->setFilterType(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
$result = $this->_testAutoFilterColumnObject->getFilterType();
$this->assertEquals(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER, $result);
}
/**
* @expectedException PHPExcel_Exception
*/
public function testSetInvalidFilterTypeThrowsException()
{
$expectedResult = 'Unfiltered';
$result = $this->_testAutoFilterColumnObject->setFilterType($expectedResult);
}
public function testGetJoin()
{
$result = $this->_testAutoFilterColumnObject->getJoin();
$this->assertEquals(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_OR, $result);
}
public function testSetJoin()
{
$result = $this->_testAutoFilterColumnObject->setJoin(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
$result = $this->_testAutoFilterColumnObject->getJoin();
$this->assertEquals(PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND, $result);
}
/**
* @expectedException PHPExcel_Exception
*/
public function testSetInvalidJoinThrowsException()
{
$expectedResult = 'Neither';
$result = $this->_testAutoFilterColumnObject->setJoin($expectedResult);
}
public function testSetAttributes()
{
$attributeSet = array( 'val' => 100,
'maxVal' => 200
);
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterColumnObject->setAttributes($attributeSet);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
}
public function testGetAttributes()
{
$attributeSet = array( 'val' => 100,
'maxVal' => 200
);
$this->_testAutoFilterColumnObject->setAttributes($attributeSet);
$result = $this->_testAutoFilterColumnObject->getAttributes();
$this->assertTrue(is_array($result));
$this->assertEquals(count($attributeSet), count($result));
}
public function testSetAttribute()
{
$attributeSet = array( 'val' => 100,
'maxVal' => 200
);
foreach($attributeSet as $attributeName => $attributeValue) {
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterColumnObject->setAttribute($attributeName,$attributeValue);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter_Column', $result);
}
}
public function testGetAttribute()
{
$attributeSet = array( 'val' => 100,
'maxVal' => 200
);
$this->_testAutoFilterColumnObject->setAttributes($attributeSet);
foreach($attributeSet as $attributeName => $attributeValue) {
$result = $this->_testAutoFilterColumnObject->getAttribute($attributeName);
$this->assertEquals($attributeValue, $result);
}
$result = $this->_testAutoFilterColumnObject->getAttribute('nonExistentAttribute');
$this->assertNull($result);
}
public function testClone() public function testClone()
{ {
$result = clone $this->_testAutoFilterColumnObject; $result = clone $this->_testAutoFilterColumnObject;

View File

@ -15,7 +15,14 @@ class AutoFilterTest extends PHPUnit_Framework_TestCase
} }
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
$this->_testAutoFilterObject = new PHPExcel_Worksheet_AutoFilter($this->_testInitialRange); $this->_mockWorksheetObject = $this->getMockBuilder('PHPExcel_Worksheet')
->disableOriginalConstructor()
->getMock();
$this->_testAutoFilterObject = new PHPExcel_Worksheet_AutoFilter(
$this->_testInitialRange,
$this->_mockWorksheetObject
);
} }
public function testToString() public function testToString()
@ -27,6 +34,19 @@ class AutoFilterTest extends PHPUnit_Framework_TestCase
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
public function testGetParent()
{
$result = $this->_testAutoFilterObject->getParent();
$this->assertInstanceOf('PHPExcel_Worksheet', $result);
}
public function testSetParent()
{
// Setters return the instance to implement the fluent interface
$result = $this->_testAutoFilterObject->setParent($this->_mockWorksheetObject);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
}
public function testGetRange() public function testGetRange()
{ {
$expectedResult = $this->_testInitialRange; $expectedResult = $this->_testInitialRange;
@ -38,15 +58,19 @@ class AutoFilterTest extends PHPUnit_Framework_TestCase
public function testSetRange() public function testSetRange()
{ {
$expectedResult = 'G1:J512'; $ranges = array('G1:J512' => 'Worksheet1!G1:J512',
'K1:N20' => 'K1:N20'
);
// Setters return the instance to implement the fluent interface foreach($ranges as $actualRange => $fullRange) {
$result = $this->_testAutoFilterObject->setRange($expectedResult); // Setters return the instance to implement the fluent interface
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result); $result = $this->_testAutoFilterObject->setRange($fullRange);
$this->assertInstanceOf('PHPExcel_Worksheet_AutoFilter', $result);
// Result should be the new autofilter range // Result should be the new autofilter range
$result = $this->_testAutoFilterObject->getRange(); $result = $this->_testAutoFilterObject->getRange();
$this->assertEquals($expectedResult, $result); $this->assertEquals($actualRange, $result);
}
} }
public function testClearRange() public function testClearRange()
@ -207,6 +231,22 @@ class AutoFilterTest extends PHPUnit_Framework_TestCase
} }
} }
public function testGetColumnByOffset()
{
$columnIndexes = array( 0 => 'H',
3 => 'K',
5 => 'M'
);
// If we request a specific column by its offset, we should
// get a PHPExcel_Worksheet_AutoFilter_Column object returned
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() public function testGetColumnIfNotSet()
{ {
// If we request a specific column by its column ID, we should // If we request a specific column by its column ID, we should

View File

@ -71,8 +71,9 @@
2009, -1, -1, 39751 2009, -1, -1, 39751
2010, 0, -1, 40146 2010, 0, -1, 40146
2010, 5, 31, 40329 2010, 5, 31, 40329
2010, 1, 21st, 40199 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date 2010, 1, '21st', 40199 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date
2010, "March",21st, 40168 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date 2010, "March",'21st', 40168 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date
2010, "March",21, 40168 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date
"ABC", 1, 21, "#VALUE!" "ABC", 1, 21, "#VALUE!"
2010, "DEF", 21, "#VALUE!" 2010, "DEF", 21, "#VALUE!"
2010, 3, "GHI", "#VALUE!" 2010, 3, "GHI", "#VALUE!"

View File

@ -3,4 +3,3 @@
65, "A" 65, "A"
123, "{" 123, "{"
126, "~" 126, "~"