From cdf9dfdcbea2d82f03a18c4082aa544cf80b8a6f Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 3 Aug 2012 21:21:32 +0100 Subject: [PATCH] Unit tests, plus a couple of minor validation bugfixes --- Classes/PHPExcel/Shared/Font.php | 14 ++- Classes/PHPExcel/Style/Color.php | 41 ++++---- .../Classes/PHPExcel/Cell/HyperlinkTest.php | 88 +++++++++++++++++ .../Classes/PHPExcel/Shared/FontTest.php | 94 +++++++++++++++++++ .../PHPExcel/Shared/PasswordHasherTest.php | 33 +++++++ .../Classes/PHPExcel/Style/ColorTest.php | 81 ++++++++++++++++ .../Shared/CentimeterSizeToPixels.data | 5 + .../rawTestData/Shared/FontSizeToPixels.data | 16 ++++ .../rawTestData/Shared/InchSizeToPixels.data | 5 + .../rawTestData/Shared/PasswordHashes.data | 9 ++ .../Style/ColorChangeBrightness.data | 14 +++ unitTests/rawTestData/Style/ColorGetBlue.data | 6 ++ .../rawTestData/Style/ColorGetGreen.data | 6 ++ unitTests/rawTestData/Style/ColorGetRed.data | 6 ++ 14 files changed, 398 insertions(+), 20 deletions(-) create mode 100644 unitTests/Classes/PHPExcel/Cell/HyperlinkTest.php create mode 100644 unitTests/Classes/PHPExcel/Shared/FontTest.php create mode 100644 unitTests/Classes/PHPExcel/Shared/PasswordHasherTest.php create mode 100644 unitTests/Classes/PHPExcel/Style/ColorTest.php create mode 100644 unitTests/rawTestData/Shared/CentimeterSizeToPixels.data create mode 100644 unitTests/rawTestData/Shared/FontSizeToPixels.data create mode 100644 unitTests/rawTestData/Shared/InchSizeToPixels.data create mode 100644 unitTests/rawTestData/Shared/PasswordHashes.data create mode 100644 unitTests/rawTestData/Style/ColorChangeBrightness.data create mode 100644 unitTests/rawTestData/Style/ColorGetBlue.data create mode 100644 unitTests/rawTestData/Style/ColorGetGreen.data create mode 100644 unitTests/rawTestData/Style/ColorGetRed.data diff --git a/Classes/PHPExcel/Shared/Font.php b/Classes/PHPExcel/Shared/Font.php index 47f6b72..62260d8 100644 --- a/Classes/PHPExcel/Shared/Font.php +++ b/Classes/PHPExcel/Shared/Font.php @@ -39,6 +39,11 @@ class PHPExcel_Shared_Font const AUTOSIZE_METHOD_APPROX = 'approx'; const AUTOSIZE_METHOD_EXACT = 'exact'; + private static $_autoSizeMethods = array( + self::AUTOSIZE_METHOD_APPROX, + self::AUTOSIZE_METHOD_EXACT, + ); + /** Character set codes used by BIFF5-8 in Font records */ const CHARSET_ANSI_LATIN = 0x00; const CHARSET_SYSTEM_DEFAULT = 0x01; @@ -187,10 +192,17 @@ class PHPExcel_Shared_Font * Set autoSize method * * @param string $pValue + * @return boolean Success or failure */ - public static function setAutoSizeMethod($pValue = 'approx') + public static function setAutoSizeMethod($pValue = self::AUTOSIZE_METHOD_APPROX) { + if (!in_array($pValue,self::$_autoSizeMethods)) { + return FALSE; + } + self::$autoSizeMethod = $pValue; + + return TRUE; } /** diff --git a/Classes/PHPExcel/Style/Color.php b/Classes/PHPExcel/Style/Color.php index a94a0ed..89c919b 100644 --- a/Classes/PHPExcel/Style/Color.php +++ b/Classes/PHPExcel/Style/Color.php @@ -213,7 +213,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable * @throws Exception * @return PHPExcel_Style_Color */ - public function applyFromArray($pStyles = null) { + public function applyFromArray($pStyles = NULL) { if (is_array($pStyles)) { if ($this->_isSupervisor) { $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); @@ -303,8 +303,8 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable * decimal value * @return string The extracted colour component */ - private static function _getColourComponent($RGB,$offset,$hex=true) { - $colour = substr($RGB,$offset,2); + private static function _getColourComponent($RGB,$offset,$hex=TRUE) { + $colour = substr($RGB, $offset, 2); if (!$hex) $colour = hexdec($colour); return $colour; @@ -318,11 +318,11 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable * decimal value * @return string The red colour component */ - public static function getRed($RGB,$hex=true) { + public static function getRed($RGB,$hex=TRUE) { if (strlen($RGB) == 8) { - return self::_getColourComponent($RGB,2,$hex); + return self::_getColourComponent($RGB, 2, $hex); } elseif (strlen($RGB) == 6) { - return self::_getColourComponent($RGB,0,$hex); + return self::_getColourComponent($RGB, 0, $hex); } } @@ -334,11 +334,11 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable * decimal value * @return string The green colour component */ - public static function getGreen($RGB,$hex=true) { + public static function getGreen($RGB,$hex=TRUE) { if (strlen($RGB) == 8) { - return self::_getColourComponent($RGB,4,$hex); + return self::_getColourComponent($RGB, 4, $hex); } elseif (strlen($RGB) == 6) { - return self::_getColourComponent($RGB,2,$hex); + return self::_getColourComponent($RGB, 2, $hex); } } @@ -350,25 +350,27 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable * decimal value * @return string The blue colour component */ - public static function getBlue($RGB,$hex=true) { + public static function getBlue($RGB,$hex=TRUE) { if (strlen($RGB) == 8) { - return self::_getColourComponent($RGB,6,$hex); + return self::_getColourComponent($RGB, 6, $hex); } elseif (strlen($RGB) == 6) { - return self::_getColourComponent($RGB,4,$hex); + return self::_getColourComponent($RGB, 4, $hex); } } /** * Adjust the brightness of a color * - * @param string $hex The colour as an RGB value (e.g. FF00CCCC or CCDDEE + * @param string $hex The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE) * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1 - * @return string The adjusted colour as an RGB value (e.g. FF00CCCC or CCDDEE + * @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE) */ public static function changeBrightness($hex, $adjustPercentage) { - $red = self::getRed($hex,false); - $green = self::getGreen($hex,false); - $blue = self::getBlue($hex,false); + $rgba = (strlen($hex) == 8); + + $red = self::getRed($hex, FALSE); + $green = self::getGreen($hex, FALSE); + $blue = self::getBlue($hex, FALSE); if ($adjustPercentage > 0) { $red += (255 - $red) * $adjustPercentage; $green += (255 - $green) * $adjustPercentage; @@ -386,10 +388,11 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable if ($blue < 0) $blue = 0; elseif ($blue > 255) $blue = 255; - return strtoupper( str_pad(dechex($red), 2, '0', 0) . + $rgb = strtoupper( str_pad(dechex($red), 2, '0', 0) . str_pad(dechex($green), 2, '0', 0) . str_pad(dechex($blue), 2, '0', 0) ); + return (($rgba) ? 'FF' : '') . $rgb; } /** @@ -400,7 +403,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable * should be returned if the indexed colour doesn't exist * @return PHPExcel_Style_Color */ - public static function indexedColor($pIndex, $background=false) { + public static function indexedColor($pIndex, $background=FALSE) { // Clean parameter $pIndex = intval($pIndex); diff --git a/unitTests/Classes/PHPExcel/Cell/HyperlinkTest.php b/unitTests/Classes/PHPExcel/Cell/HyperlinkTest.php new file mode 100644 index 0000000..54910cf --- /dev/null +++ b/unitTests/Classes/PHPExcel/Cell/HyperlinkTest.php @@ -0,0 +1,88 @@ +getUrl(); + $this->assertEquals($urlValue,$result); + } + + public function testSetUrl() + { + $initialUrlValue = 'http://www.phpexcel.net'; + $newUrlValue = 'http://github.com/PHPOffice/PHPExcel'; + + $testInstance = new PHPExcel_Cell_Hyperlink($initialUrlValue); + $result = $testInstance->setUrl($newUrlValue); + $this->assertTrue($result instanceof PHPExcel_Cell_Hyperlink); + + $result = $testInstance->getUrl(); + $this->assertEquals($newUrlValue,$result); + } + + public function testGetTooltip() + { + $tooltipValue = 'PHPExcel Web Site'; + + $testInstance = new PHPExcel_Cell_Hyperlink(NULL, $tooltipValue); + + $result = $testInstance->getTooltip(); + $this->assertEquals($tooltipValue,$result); + } + + public function testSetTooltip() + { + $initialTooltipValue = 'PHPExcel Web Site'; + $newTooltipValue = 'PHPExcel Repository on Github'; + + $testInstance = new PHPExcel_Cell_Hyperlink(NULL, $initialTooltipValue); + $result = $testInstance->setTooltip($newTooltipValue); + $this->assertTrue($result instanceof PHPExcel_Cell_Hyperlink); + + $result = $testInstance->getTooltip(); + $this->assertEquals($newTooltipValue,$result); + } + + public function testIsInternal() + { + $initialUrlValue = 'http://www.phpexcel.net'; + $newUrlValue = 'sheet://Worksheet1!A1'; + + $testInstance = new PHPExcel_Cell_Hyperlink($initialUrlValue); + $result = $testInstance->isInternal(); + $this->assertFalse($result); + + $testInstance->setUrl($newUrlValue); + $result = $testInstance->isInternal(); + $this->assertTrue($result); + } + + public function testGetHashCode() + { + $urlValue = 'http://www.phpexcel.net'; + $tooltipValue = 'PHPExcel Web Site'; + $initialExpectedHash = 'd84d713aed1dbbc8a7c5af183d6c7dbb'; + + $testInstance = new PHPExcel_Cell_Hyperlink($urlValue, $tooltipValue); + + $result = $testInstance->getHashCode(); + $this->assertEquals($initialExpectedHash,$result); + } + +} diff --git a/unitTests/Classes/PHPExcel/Shared/FontTest.php b/unitTests/Classes/PHPExcel/Shared/FontTest.php new file mode 100644 index 0000000..6ce5a02 --- /dev/null +++ b/unitTests/Classes/PHPExcel/Shared/FontTest.php @@ -0,0 +1,94 @@ +assertEquals($expectedResult, $result); + } + + public function testSetAutoSizeMethod() + { + $autosizeMethodValues = array( + PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT, + PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX, + ); + + foreach($autosizeMethodValues as $autosizeMethodValue) { + $result = call_user_func(array('PHPExcel_Shared_Font','setAutoSizeMethod'),$autosizeMethodValue); + $this->assertTrue($result); + } + } + + public function testSetAutoSizeMethodWithInvalidValue() + { + $unsupportedAutosizeMethod = 'guess'; + + $result = call_user_func(array('PHPExcel_Shared_Font','setAutoSizeMethod'),$unsupportedAutosizeMethod); + $this->assertFalse($result); + } + + /** + * @dataProvider providerFontSizeToPixels + */ + public function testFontSizeToPixels() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Shared_Font','fontSizeToPixels'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerFontSizeToPixels() + { + return new testDataFileIterator('rawTestData/Shared/FontSizeToPixels.data'); + } + + /** + * @dataProvider providerInchSizeToPixels + */ + public function testInchSizeToPixels() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Shared_Font','inchSizeToPixels'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerInchSizeToPixels() + { + return new testDataFileIterator('rawTestData/Shared/InchSizeToPixels.data'); + } + + /** + * @dataProvider providerCentimeterSizeToPixels + */ + public function testCentimeterSizeToPixels() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Shared_Font','centimeterSizeToPixels'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerCentimeterSizeToPixels() + { + return new testDataFileIterator('rawTestData/Shared/CentimeterSizeToPixels.data'); + } + +} diff --git a/unitTests/Classes/PHPExcel/Shared/PasswordHasherTest.php b/unitTests/Classes/PHPExcel/Shared/PasswordHasherTest.php new file mode 100644 index 0000000..24a2617 --- /dev/null +++ b/unitTests/Classes/PHPExcel/Shared/PasswordHasherTest.php @@ -0,0 +1,33 @@ +assertEquals($expectedResult, $result); + } + + public function providerHashPassword() + { + return new testDataFileIterator('rawTestData/Shared/PasswordHashes.data'); + } + +} diff --git a/unitTests/Classes/PHPExcel/Style/ColorTest.php b/unitTests/Classes/PHPExcel/Style/ColorTest.php new file mode 100644 index 0000000..f157b1a --- /dev/null +++ b/unitTests/Classes/PHPExcel/Style/ColorTest.php @@ -0,0 +1,81 @@ +assertEquals($expectedResult, $result); + } + + public function providerColorGetRed() + { + return new testDataFileIterator('rawTestData/Style/ColorGetRed.data'); + } + + /** + * @dataProvider providerColorGetGreen + */ + public function testGetGreen() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Style_Color','getGreen'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerColorGetGreen() + { + return new testDataFileIterator('rawTestData/Style/ColorGetGreen.data'); + } + + /** + * @dataProvider providerColorGetBlue + */ + public function testGetBlue() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Style_Color','getBlue'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerColorGetBlue() + { + return new testDataFileIterator('rawTestData/Style/ColorGetBlue.data'); + } + + /** + * @dataProvider providerColorChangeBrightness + */ + public function testChangeBrightness() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Style_Color','changeBrightness'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerColorChangeBrightness() + { + return new testDataFileIterator('rawTestData/Style/ColorChangeBrightness.data'); + } + +} diff --git a/unitTests/rawTestData/Shared/CentimeterSizeToPixels.data b/unitTests/rawTestData/Shared/CentimeterSizeToPixels.data new file mode 100644 index 0000000..fc48f21 --- /dev/null +++ b/unitTests/rawTestData/Shared/CentimeterSizeToPixels.data @@ -0,0 +1,5 @@ +0.1, 3.7795275591 +0.2, 7.5590551182 +0.5, 18.8976377955 +1.0, 37.795275591 +2.0, 75.590551182 diff --git a/unitTests/rawTestData/Shared/FontSizeToPixels.data b/unitTests/rawTestData/Shared/FontSizeToPixels.data new file mode 100644 index 0000000..af89196 --- /dev/null +++ b/unitTests/rawTestData/Shared/FontSizeToPixels.data @@ -0,0 +1,16 @@ +6, 8 +7, 9 +8, 10 +9, 12 +10, 13 +11, 14 +12, 16 +14, 18 +16, 21 +18, 24 +20, 26 +22, 29 +24, 32 +36, 48 +48, 64 +60, 80 diff --git a/unitTests/rawTestData/Shared/InchSizeToPixels.data b/unitTests/rawTestData/Shared/InchSizeToPixels.data new file mode 100644 index 0000000..147eaa9 --- /dev/null +++ b/unitTests/rawTestData/Shared/InchSizeToPixels.data @@ -0,0 +1,5 @@ +0.1, 9.6 +0.2, 19.2 +0.5, 48.0 +1.0, 96.0 +2.0, 192.0 diff --git a/unitTests/rawTestData/Shared/PasswordHashes.data b/unitTests/rawTestData/Shared/PasswordHashes.data new file mode 100644 index 0000000..8ceffa4 --- /dev/null +++ b/unitTests/rawTestData/Shared/PasswordHashes.data @@ -0,0 +1,9 @@ +"PHPExcel", "8053" +"Mark Baker", "877D" +"I<3Am3l1a/*", "E3C8" +"μυστικό κωδικό πρόσβασης", "FFFF26DD" +"গোপন পাসওয়ার্ড", "E858" +"Секретный пароль", "EA5F" +"秘密口令", "C07E" +"leyndarmál lykilorð", "99E8" +"", "CE4B" diff --git a/unitTests/rawTestData/Style/ColorChangeBrightness.data b/unitTests/rawTestData/Style/ColorChangeBrightness.data new file mode 100644 index 0000000..de5eeaf --- /dev/null +++ b/unitTests/rawTestData/Style/ColorChangeBrightness.data @@ -0,0 +1,14 @@ +"FFAABBCC", 0.1, "FFB2C1D1" // RGBA +"FFAABBCC", -0.1, "FF99A8B7" // RGBA +"AABBCC", 0.1, "B2C1D1" // RGB +"AABBCC", -0.1, "99A8B7" // RGB +"FF0000", 0.1, "FF1919" +"FF0000", -0.1, "E50000" +"FF8080", 0.1, "FF8C8C" +"FF8080", -0.1, "E57373" +"FF0000", 0.15, "FF2626" +"FF0000", -0.15, "D80000" +"FF8080", 0.15, "FF9393" +"FF8080", -0.15, "D86C6C" +"FFF008", 0.5, "FFF783" +"FFF008", -0.5, "7F7804" diff --git a/unitTests/rawTestData/Style/ColorGetBlue.data b/unitTests/rawTestData/Style/ColorGetBlue.data new file mode 100644 index 0000000..4e01d1c --- /dev/null +++ b/unitTests/rawTestData/Style/ColorGetBlue.data @@ -0,0 +1,6 @@ +"FFAABBCC", "CC" // RGBA (hex) +"FFAABBCC", FALSE, 204 // RGBA (decimal) +"AABBCC", "CC" // RGB (hex) +"AABBCC", FALSE, 204 // RGB (decimal) +"FFFF00", "00" +"FFFF00", FALSE, 0 diff --git a/unitTests/rawTestData/Style/ColorGetGreen.data b/unitTests/rawTestData/Style/ColorGetGreen.data new file mode 100644 index 0000000..0ace610 --- /dev/null +++ b/unitTests/rawTestData/Style/ColorGetGreen.data @@ -0,0 +1,6 @@ +"FFAABBCC", "BB" // RGBA (hex) +"FFAABBCC", FALSE, 187 // RGBA (decimal) +"AABBCC", "BB" // RGB (hex) +"AABBCC", FALSE, 187 // RGB (decimal) +"FF00FF", "00" +"FF00FF", FALSE, 0 diff --git a/unitTests/rawTestData/Style/ColorGetRed.data b/unitTests/rawTestData/Style/ColorGetRed.data new file mode 100644 index 0000000..1692a6a --- /dev/null +++ b/unitTests/rawTestData/Style/ColorGetRed.data @@ -0,0 +1,6 @@ +"FFAABBCC", "AA" // RGBA (hex) +"FFAABBCC", FALSE, 170 // RGBA (decimal) +"AABBCC", "AA" // RGB (hex) +"AABBCC", FALSE, 170 // RGB (decimal) +"00FFFF", "00" +"00FFFF", FALSE, 0