From 34f3e8b706aa8a88243c4353f6b004d528329637 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Thu, 19 Jan 2012 22:32:58 +0000 Subject: [PATCH] Initial phpunit unit test scripts git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@85526 2327b42d-5241-43d6-9e2a-de5ac946f064 --- unitTests/PHPExcel/AutoloaderTest.php | 56 ++++++ .../PHPExcel/Calculation/LogicalTest.php | 172 ++++++++++++++++++ unitTests/bootstrap.php | 44 +++++ unitTests/phpunit.xml | 38 ++++ .../rawTestData/Calculation/Logical/NOT.data | 20 ++ unitTests/testDataFileIterator.php | 79 ++++++++ 6 files changed, 409 insertions(+) create mode 100644 unitTests/PHPExcel/AutoloaderTest.php create mode 100644 unitTests/PHPExcel/Calculation/LogicalTest.php create mode 100644 unitTests/bootstrap.php create mode 100644 unitTests/phpunit.xml create mode 100644 unitTests/rawTestData/Calculation/Logical/NOT.data create mode 100644 unitTests/testDataFileIterator.php diff --git a/unitTests/PHPExcel/AutoloaderTest.php b/unitTests/PHPExcel/AutoloaderTest.php new file mode 100644 index 00000000..542be23f --- /dev/null +++ b/unitTests/PHPExcel/AutoloaderTest.php @@ -0,0 +1,56 @@ +assertTrue(is_bool($result)); + // ... indicating failure + $this->assertFalse($result); + } + + public function testAutoloaderInvalidPHPExcelClass() + { + $className = 'PHPExcel_Invalid_Class'; + + $result = PHPExcel_Autoloader::Load($className); + // Must return a boolean... + $this->assertTrue(is_bool($result)); + // ... indicating failure + $this->assertFalse($result); + } + + public function testAutoloadValidPHPExcelClass() + { + $className = 'PHPExcel_IOFactory'; + + $result = PHPExcel_Autoloader::Load($className); + // Check that class has been loaded + $this->assertTrue(class_exists($className)); + } + + public function testAutoloadInstantiateSuccess() + { + $result = new PHPExcel_Calculation_Function(1,2,3); + // Must return an object... + $this->assertTrue(is_object($result)); + // ... of the correct type + $this->assertTrue(is_a($result,'PHPExcel_Calculation_Function')); + } + +} \ No newline at end of file diff --git a/unitTests/PHPExcel/Calculation/LogicalTest.php b/unitTests/PHPExcel/Calculation/LogicalTest.php new file mode 100644 index 00000000..52ea7ec4 --- /dev/null +++ b/unitTests/PHPExcel/Calculation/LogicalTest.php @@ -0,0 +1,172 @@ +assertEquals(TRUE, $result); + } + + public function testFALSE() + { + $result = PHPExcel_Calculation_Logical::FALSE(); + $this->assertEquals(FALSE, $result); + } + + /** + * @dataProvider providerAND + */ + public function testAND() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Calculation_Logical','LOGICAL_AND'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerAND() + { + return array( + array( '#VALUE!' ), // No arguments + array( NULL, TRUE ), // NULL + array( TRUE, NULL, TRUE ), // Boolean TRUE and NULL + array( FALSE, NULL, FALSE ), // Boolean FALSE and NULL + array( TRUE, TRUE, TRUE ), // Both TRUE Booleans + array( TRUE, FALSE, FALSE ), // Mixed Booleans + array( FALSE, TRUE, FALSE ), // Mixed Booleans + array( FALSE, FALSE, FALSE ), // Both FALSE Booleans + array( TRUE, TRUE, FALSE, FALSE ), // Multiple Mixed Booleans + array( TRUE, TRUE, TRUE, TRUE ), // Multiple TRUE Booleans + array( FALSE, FALSE, FALSE, FALSE, FALSE ), // Multiple FALSE Booleans + array( -1, -2, TRUE ), + array( 0, 0, FALSE ), + array( 0, 1, FALSE ), + array( 1, 1, TRUE ), + array( '1',1, '#VALUE!' ), + array( 'TRUE',1, TRUE ), // 'TRUE' String + array( 'FALSE',TRUE, FALSE ), // 'FALSE' String + array( 'ABCD',1, '#VALUE!' ), // Non-numeric String + array( -2, 1, TRUE ), + array( -2, 0, FALSE ), + ); + +// return new testDataFileIterator('rawTestData/Calculation/Logical/AND.data'); + } + + /** + * @dataProvider providerOR + */ + public function testOR() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Calculation_Logical','LOGICAL_OR'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerOR() + { + return array( + array( '#VALUE!' ), // No arguments + array( NULL, FALSE ), // NULL + array( TRUE, NULL, TRUE ), // Boolean TRUE and NULL + array( FALSE, NULL, FALSE ), // Boolean FALSE and NULL + array( TRUE, TRUE, TRUE ), // Both TRUE Booleans + array( TRUE, FALSE, TRUE ), // Mixed Booleans + array( FALSE, TRUE, TRUE ), // Mixed Booleans + array( FALSE, FALSE, FALSE ), // Both FALSE Booleans + array( TRUE, TRUE, FALSE, TRUE ), // Multiple Mixed Booleans + array( TRUE, TRUE, TRUE, TRUE ), // Multiple TRUE Booleans + array( FALSE, FALSE, FALSE, FALSE, FALSE ), // Multiple FALSE Booleans + array( -1, -2, TRUE ), + array( 0, 0, FALSE ), + array( 0, 1, TRUE ), + array( 1, 1, TRUE ), + array( 'TRUE',1, TRUE ), // 'TRUE' String + array( 'FALSE',TRUE, TRUE ), // 'FALSE' String + array( 'ABCD',1, '#VALUE!' ), // Non-numeric String + array( -2, 1, TRUE ), + array( -2, 0, TRUE ), + ); + +// return new testDataFileIterator('rawTestData/Calculation/Logical/OR.data'); + } + + /** + * @dataProvider providerNOT + */ + public function testNOT() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Calculation_Logical','NOT'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerNOT() + { + return new testDataFileIterator('rawTestData/Calculation/Logical/NOT.data'); + } + + /** + * @dataProvider providerIF + */ + public function testIF() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Calculation_Logical','STATEMENT_IF'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerIF() + { + return array( + array( 0 ), + array( TRUE, 0 ), + array( FALSE, FALSE ), + array( TRUE, 'ABC', 'ABC' ), + array( FALSE, 'ABC', FALSE ), + array( TRUE, 'ABC', 'XYZ', 'ABC' ), + array( FALSE, 'ABC', 'XYZ', 'XYZ' ), + ); + +// return new testDataFileIterator('rawTestData/Calculation/Logical/IF.data'); + } + + /** + * @dataProvider providerIFERROR + */ + public function testIFERROR() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Calculation_Logical','IFERROR'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerIFERROR() + { + return array( + array( TRUE, 'Not an Error', 'Not an Error'), + array( '#VALUE!', 'Error', 'Error' ), + ); + +// return new testDataFileIterator('rawTestData/Calculation/Logical/IFERROR.data'); + } + +} diff --git a/unitTests/bootstrap.php b/unitTests/bootstrap.php new file mode 100644 index 00000000..61ae205c --- /dev/null +++ b/unitTests/bootstrap.php @@ -0,0 +1,44 @@ + /etc/php.d/xdebug.ini' . "\n"; +} diff --git a/unitTests/phpunit.xml b/unitTests/phpunit.xml new file mode 100644 index 00000000..4186581d --- /dev/null +++ b/unitTests/phpunit.xml @@ -0,0 +1,38 @@ + + + + + + + ./PHPExcel + + + + ../Classes + + ../Classes/PHPExcel/Shared/PDF + ../Classes/PHPExcel/Shared/PCLZip + ../Classes/PHPExcel/Shared/JAMA + + + + + + + + diff --git a/unitTests/rawTestData/Calculation/Logical/NOT.data b/unitTests/rawTestData/Calculation/Logical/NOT.data new file mode 100644 index 00000000..d65a28d2 --- /dev/null +++ b/unitTests/rawTestData/Calculation/Logical/NOT.data @@ -0,0 +1,20 @@ +,TRUE +NULL,TRUE +-1,FALSE +0,TRUE +1,FALSE +2,FALSE +-1.5,FALSE +1.5,FALSE +"-1","#VALUE!" +"0","#VALUE!" +"1","#VALUE!" +"2","#VALUE!" +"-1.5","#VALUE!" +"1.5","#VALUE!" +"","#VALUE!" +"ABC","#VALUE!" +"FALSE",TRUE +"TRUE",FALSE +TRUE,FALSE +FALSE,TRUE \ No newline at end of file diff --git a/unitTests/testDataFileIterator.php b/unitTests/testDataFileIterator.php new file mode 100644 index 00000000..f183887f --- /dev/null +++ b/unitTests/testDataFileIterator.php @@ -0,0 +1,79 @@ +file = fopen($file, 'r'); + } + + public function __destruct() + { + fclose($this->file); + } + + public function rewind() + { + rewind($this->file); + $this->current = $this->_parseNextDataset(); + $this->key = 0; + } + + public function valid() + { + return !feof($this->file); + } + + public function key() + { + return $this->key; + } + + public function current() + { + return $this->current; + } + + public function next() + { + $this->current = $this->_parseNextDataset(); + $this->key++; + } + + private function _parseNextDataset() + { + $dataSet = explode(',',trim(fgets($this->file))); + foreach($dataSet as &$dataValue) { + if (!is_numeric($dataValue)) { + if($dataValue == '') { + $dataValue = NULL; + } elseif($dataValue == '""') { + $dataValue = ''; + } elseif(($dataValue[0] == '"') && ($dataValue[strlen($dataValue)-1] == '"')) { + $dataValue = substr($dataValue,1,-1); + } else { + switch (strtoupper($dataValue)) { + case 'NULL' : $dataValue = NULL; break; + case 'TRUE' : $dataValue = TRUE; break; + case 'FALSE' : $dataValue = FALSE; break; + } + } + } else { + if (is_float($dataValue)) { + $dataValue = (float) $dataValue; + } else { + $dataValue = (int) $dataValue; + } + } + } + unset($dataValue); + + return $dataSet; + } + +}