Unit tests, plus a couple of minor validation bugfixes

This commit is contained in:
Mark Baker 2012-08-03 21:21:32 +01:00
parent 7fae37247b
commit cdf9dfdcbe
14 changed files with 398 additions and 20 deletions

View File

@ -39,6 +39,11 @@ class PHPExcel_Shared_Font
const AUTOSIZE_METHOD_APPROX = 'approx'; const AUTOSIZE_METHOD_APPROX = 'approx';
const AUTOSIZE_METHOD_EXACT = 'exact'; 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 */ /** Character set codes used by BIFF5-8 in Font records */
const CHARSET_ANSI_LATIN = 0x00; const CHARSET_ANSI_LATIN = 0x00;
const CHARSET_SYSTEM_DEFAULT = 0x01; const CHARSET_SYSTEM_DEFAULT = 0x01;
@ -187,10 +192,17 @@ class PHPExcel_Shared_Font
* Set autoSize method * Set autoSize method
* *
* @param string $pValue * @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; self::$autoSizeMethod = $pValue;
return TRUE;
} }
/** /**

View File

@ -213,7 +213,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
* @throws Exception * @throws Exception
* @return PHPExcel_Style_Color * @return PHPExcel_Style_Color
*/ */
public function applyFromArray($pStyles = null) { public function applyFromArray($pStyles = NULL) {
if (is_array($pStyles)) { if (is_array($pStyles)) {
if ($this->_isSupervisor) { if ($this->_isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
@ -303,7 +303,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
* decimal value * decimal value
* @return string The extracted colour component * @return string The extracted colour component
*/ */
private static function _getColourComponent($RGB,$offset,$hex=true) { private static function _getColourComponent($RGB,$offset,$hex=TRUE) {
$colour = substr($RGB, $offset, 2); $colour = substr($RGB, $offset, 2);
if (!$hex) if (!$hex)
$colour = hexdec($colour); $colour = hexdec($colour);
@ -318,7 +318,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
* decimal value * decimal value
* @return string The red colour component * @return string The red colour component
*/ */
public static function getRed($RGB,$hex=true) { public static function getRed($RGB,$hex=TRUE) {
if (strlen($RGB) == 8) { if (strlen($RGB) == 8) {
return self::_getColourComponent($RGB, 2, $hex); return self::_getColourComponent($RGB, 2, $hex);
} elseif (strlen($RGB) == 6) { } elseif (strlen($RGB) == 6) {
@ -334,7 +334,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
* decimal value * decimal value
* @return string The green colour component * @return string The green colour component
*/ */
public static function getGreen($RGB,$hex=true) { public static function getGreen($RGB,$hex=TRUE) {
if (strlen($RGB) == 8) { if (strlen($RGB) == 8) {
return self::_getColourComponent($RGB, 4, $hex); return self::_getColourComponent($RGB, 4, $hex);
} elseif (strlen($RGB) == 6) { } elseif (strlen($RGB) == 6) {
@ -350,7 +350,7 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
* decimal value * decimal value
* @return string The blue colour component * @return string The blue colour component
*/ */
public static function getBlue($RGB,$hex=true) { public static function getBlue($RGB,$hex=TRUE) {
if (strlen($RGB) == 8) { if (strlen($RGB) == 8) {
return self::_getColourComponent($RGB, 6, $hex); return self::_getColourComponent($RGB, 6, $hex);
} elseif (strlen($RGB) == 6) { } elseif (strlen($RGB) == 6) {
@ -361,14 +361,16 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
/** /**
* Adjust the brightness of a color * 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 * @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) { public static function changeBrightness($hex, $adjustPercentage) {
$red = self::getRed($hex,false); $rgba = (strlen($hex) == 8);
$green = self::getGreen($hex,false);
$blue = self::getBlue($hex,false); $red = self::getRed($hex, FALSE);
$green = self::getGreen($hex, FALSE);
$blue = self::getBlue($hex, FALSE);
if ($adjustPercentage > 0) { if ($adjustPercentage > 0) {
$red += (255 - $red) * $adjustPercentage; $red += (255 - $red) * $adjustPercentage;
$green += (255 - $green) * $adjustPercentage; $green += (255 - $green) * $adjustPercentage;
@ -386,10 +388,11 @@ class PHPExcel_Style_Color implements PHPExcel_IComparable
if ($blue < 0) $blue = 0; if ($blue < 0) $blue = 0;
elseif ($blue > 255) $blue = 255; 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($green), 2, '0', 0) .
str_pad(dechex($blue), 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 * should be returned if the indexed colour doesn't exist
* @return PHPExcel_Style_Color * @return PHPExcel_Style_Color
*/ */
public static function indexedColor($pIndex, $background=false) { public static function indexedColor($pIndex, $background=FALSE) {
// Clean parameter // Clean parameter
$pIndex = intval($pIndex); $pIndex = intval($pIndex);

View File

@ -0,0 +1,88 @@
<?php
class HyperlinkTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT'))
{
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
public function testGetUrl()
{
$urlValue = 'http://www.phpexcel.net';
$testInstance = new PHPExcel_Cell_Hyperlink($urlValue);
$result = $testInstance->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);
}
}

View File

@ -0,0 +1,94 @@
<?php
require_once 'testDataFileIterator.php';
class FontTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
public function testGetAutoSizeMethod()
{
$expectedResult = PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX;
$result = call_user_func(array('PHPExcel_Shared_Font','getAutoSizeMethod'));
$this->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');
}
}

View File

@ -0,0 +1,33 @@
<?php
require_once 'testDataFileIterator.php';
class PasswordHasherTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
/**
* @dataProvider providerHashPassword
*/
public function testHashPassword()
{
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array('PHPExcel_Shared_PasswordHasher','hashPassword'),$args);
$this->assertEquals($expectedResult, $result);
}
public function providerHashPassword()
{
return new testDataFileIterator('rawTestData/Shared/PasswordHashes.data');
}
}

View File

@ -0,0 +1,81 @@
<?php
require_once 'testDataFileIterator.php';
class ColorTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
/**
* @dataProvider providerColorGetRed
*/
public function testGetRed()
{
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array('PHPExcel_Style_Color','getRed'),$args);
$this->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');
}
}

View File

@ -0,0 +1,5 @@
0.1, 3.7795275591
0.2, 7.5590551182
0.5, 18.8976377955
1.0, 37.795275591
2.0, 75.590551182

View File

@ -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

View File

@ -0,0 +1,5 @@
0.1, 9.6
0.2, 19.2
0.5, 48.0
1.0, 96.0
2.0, 192.0

View File

@ -0,0 +1,9 @@
"PHPExcel", "8053"
"Mark Baker", "877D"
"I<3Am3l1a/*", "E3C8"
"μυστικό κωδικό πρόσβασης", "FFFF26DD"
"গোপন পাসওয়ার্ড", "E858"
"Секретный пароль", "EA5F"
"秘密口令", "C07E"
"leyndarmál lykilorð", "99E8"
"", "CE4B"

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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