From 53584ec9a3a972f9a161d3382cb6a4bf921d5459 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 28 May 2015 08:29:44 +0100 Subject: [PATCH] Unit tests for Cell classes, modification to use json-format in raw data provider --- .../Cell/DefaultValueBinder.php | 2 +- src/PhpSpreadsheet/RichText.php | 49 +++++++-------- unitTests/Classes/src/Cell/DataTypeTest.php | 4 +- .../src/Cell/DefaultValueBinderTest.php | 19 +++--- unitTests/Classes/src/Cell/HyperlinkTest.php | 20 +++--- .../rawTestData/Cell/DefaultValueBinder.json | 19 ++++++ unitTests/testDataFileIteratorJson.php | 63 +++++++++++++++++++ 7 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 unitTests/rawTestData/Cell/DefaultValueBinder.json create mode 100644 unitTests/testDataFileIteratorJson.php diff --git a/src/PhpSpreadsheet/Cell/DefaultValueBinder.php b/src/PhpSpreadsheet/Cell/DefaultValueBinder.php index 3ad0541f..d4cbaa00 100644 --- a/src/PhpSpreadsheet/Cell/DefaultValueBinder.php +++ b/src/PhpSpreadsheet/Cell/DefaultValueBinder.php @@ -43,7 +43,7 @@ class DefaultValueBinder implements IValueBinder $value = \PHPExcel\Shared\String::SanitizeUTF8($value); } elseif (is_object($value)) { // Handle any objects that might be injected - if ($value instanceof DateTime) { + if ($value instanceof \DateTime) { $value = $value->format('Y-m-d H:i:s'); } elseif (!($value instanceof \PHPExcel\RichText)) { $value = (string) $value; diff --git a/src/PhpSpreadsheet/RichText.php b/src/PhpSpreadsheet/RichText.php index bdccdd27..88e55a73 100644 --- a/src/PhpSpreadsheet/RichText.php +++ b/src/PhpSpreadsheet/RichText.php @@ -27,22 +27,22 @@ namespace PHPExcel; * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @version ##VERSION##, ##DATE## */ -class RichText implements PHPExcel_IComparable +class RichText implements IComparable { /** * Rich text elements * - * @var PHPExcel_RichText_ITextElement[] + * @var RichText\ITextElement[] */ private $richTextElements; /** - * Create a new PHPExcel_RichText instance + * Create a new RichText instance * - * @param PHPExcel_Cell $pCell - * @throws PHPExcel_Exception + * @param Cell $pCell + * @throws Exception */ - public function __construct(PHPExcel_Cell $pCell = null) + public function __construct(Cell $pCell = null) { // Initialise variables $this->richTextElements = array(); @@ -51,24 +51,24 @@ class RichText implements PHPExcel_IComparable if ($pCell !== null) { // Add cell text and style if ($pCell->getValue() != "") { - $objRun = new PHPExcel_RichText_Run($pCell->getValue()); + $objRun = new RichText\Run($pCell->getValue()); $objRun->setFont(clone $pCell->getParent()->getStyle($pCell->getCoordinate())->getFont()); $this->addText($objRun); } // Set parent value - $pCell->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING); + $pCell->setValueExplicit($this, Cell\DataType::TYPE_STRING); } } /** * Add text * - * @param PHPExcel_RichText_ITextElement $pText Rich text element - * @throws PHPExcel_Exception - * @return PHPExcel_RichText + * @param RichText\ITextElement $pText Rich text element + * @throws Exception + * @return RichText */ - public function addText(PHPExcel_RichText_ITextElement $pText = null) + public function addText(RichText\ITextElement $pText = null) { $this->richTextElements[] = $pText; return $this; @@ -78,12 +78,12 @@ class RichText implements PHPExcel_IComparable * Create text * * @param string $pText Text - * @return PHPExcel_RichText_TextElement - * @throws PHPExcel_Exception + * @return RichText\TextElement + * @throws Exception */ public function createText($pText = '') { - $objText = new PHPExcel_RichText_TextElement($pText); + $objText = new RichText\TextElement($pText); $this->addText($objText); return $objText; } @@ -92,12 +92,12 @@ class RichText implements PHPExcel_IComparable * Create text run * * @param string $pText Text - * @return PHPExcel_RichText_Run - * @throws PHPExcel_Exception + * @return RichText\Run + * @throws Exception */ public function createTextRun($pText = '') { - $objText = new PHPExcel_RichText_Run($pText); + $objText = new RichText\Run($pText); $this->addText($objText); return $objText; } @@ -112,12 +112,11 @@ class RichText implements PHPExcel_IComparable // Return value $returnValue = ''; - // Loop through all PHPExcel_RichText_ITextElement + // Loop through all RichText\ITextElements foreach ($this->richTextElements as $text) { $returnValue .= $text->getText(); } - // Return return $returnValue; } @@ -134,7 +133,7 @@ class RichText implements PHPExcel_IComparable /** * Get Rich Text elements * - * @return PHPExcel_RichText_ITextElement[] + * @return RichText\ITextElement[] */ public function getRichTextElements() { @@ -144,16 +143,16 @@ class RichText implements PHPExcel_IComparable /** * Set Rich Text elements * - * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements - * @throws PHPExcel_Exception - * @return PHPExcel_RichText + * @param RichText\ITextElement[] $pElements Array of elements + * @throws Exception + * @return RichText */ public function setRichTextElements($pElements = null) { if (is_array($pElements)) { $this->richTextElements = $pElements; } else { - throw new PHPExcel_Exception("Invalid PHPExcel_RichText_ITextElement[] array passed."); + throw new Exception("Invalid \PHPExcel\RichText\ITextElement[] array passed."); } return $this; } diff --git a/unitTests/Classes/src/Cell/DataTypeTest.php b/unitTests/Classes/src/Cell/DataTypeTest.php index 1180c9d6..019a8d49 100644 --- a/unitTests/Classes/src/Cell/DataTypeTest.php +++ b/unitTests/Classes/src/Cell/DataTypeTest.php @@ -9,12 +9,12 @@ class DataTypeTest extends PHPUnit_Framework_TestCase if (!defined('PHPEXCEL_ROOT')) { define('PHPEXCEL_ROOT', APPLICATION_PATH . '/'); } - require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); + require_once(PHPEXCEL_ROOT . '/Bootstrap.php'); } public function testGetErrorCodes() { - $result = call_user_func(array('PHPExcel_Cell_DataType','getErrorCodes')); + $result = call_user_func(array('\\PHPExcel\\Cell\\DataType','getErrorCodes')); $this->assertInternalType('array', $result); $this->assertGreaterThan(0, count($result)); $this->assertArrayHasKey('#NULL!', $result); diff --git a/unitTests/Classes/src/Cell/DefaultValueBinderTest.php b/unitTests/Classes/src/Cell/DefaultValueBinderTest.php index 37287b1f..4405f335 100644 --- a/unitTests/Classes/src/Cell/DefaultValueBinderTest.php +++ b/unitTests/Classes/src/Cell/DefaultValueBinderTest.php @@ -1,6 +1,7 @@ cellStub = $this->getMockBuilder('PHPExcel_Cell') + $this->cellStub = $this->getMockBuilder('\\PHPExcel\\Cell') ->disableOriginalConstructor() ->getMock(); // Configure the stub. @@ -33,7 +34,7 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase public function testBindValue($value) { $this->createCellStub(); - $binder = new PHPExcel_Cell_DefaultValueBinder(); + $binder = new \PHPExcel\Cell\DefaultValueBinder(); $result = $binder->bindValue($this->cellStub, $value); $this->assertTrue($result); } @@ -52,7 +53,7 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase array('123'), array('-123.456'), array('#REF!'), - array(new DateTime()), + array(new \DateTime()), ); } @@ -63,22 +64,22 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase { $args = func_get_args(); $expectedResult = array_pop($args); - $result = call_user_func_array(array('PHPExcel_Cell_DefaultValueBinder','dataTypeForValue'), $args); + $result = call_user_func_array(array('\\PHPExcel\\Cell\\DefaultValueBinder','dataTypeForValue'), $args); $this->assertEquals($expectedResult, $result); } public function providerDataTypeForValue() { - return new testDataFileIterator('rawTestData/Cell/DefaultValueBinder.data'); + return new testDataFileIteratorJson('rawTestData/Cell/DefaultValueBinder.json'); } public function testDataTypeForRichTextObject() { - $objRichText = new PHPExcel_RichText(); + $objRichText = new \PHPExcel\RichText(); $objRichText->createText('Hello World'); - $expectedResult = PHPExcel_Cell_DataType::TYPE_INLINE; - $result = call_user_func(array('PHPExcel_Cell_DefaultValueBinder','dataTypeForValue'), $objRichText); + $expectedResult = \PHPExcel\Cell\DataType::TYPE_INLINE; + $result = call_user_func(array('\\PHPExcel\\Cell\\DefaultValueBinder','dataTypeForValue'), $objRichText); $this->assertEquals($expectedResult, $result); } } diff --git a/unitTests/Classes/src/Cell/HyperlinkTest.php b/unitTests/Classes/src/Cell/HyperlinkTest.php index 42dff6a5..99825860 100644 --- a/unitTests/Classes/src/Cell/HyperlinkTest.php +++ b/unitTests/Classes/src/Cell/HyperlinkTest.php @@ -9,14 +9,14 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase if (!defined('PHPEXCEL_ROOT')) { define('PHPEXCEL_ROOT', APPLICATION_PATH . '/'); } - require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); + require_once(PHPEXCEL_ROOT . '/Bootstrap.php'); } public function testGetUrl() { $urlValue = 'http://www.phpexcel.net'; - $testInstance = new PHPExcel_Cell_Hyperlink($urlValue); + $testInstance = new \PHPExcel\Cell\Hyperlink($urlValue); $result = $testInstance->getUrl(); $this->assertEquals($urlValue, $result); @@ -27,9 +27,9 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase $initialUrlValue = 'http://www.phpexcel.net'; $newUrlValue = 'http://github.com/PHPOffice/PHPExcel'; - $testInstance = new PHPExcel_Cell_Hyperlink($initialUrlValue); + $testInstance = new \PHPExcel\Cell\Hyperlink($initialUrlValue); $result = $testInstance->setUrl($newUrlValue); - $this->assertTrue($result instanceof PHPExcel_Cell_Hyperlink); + $this->assertTrue($result instanceof \PHPExcel\Cell\Hyperlink); $result = $testInstance->getUrl(); $this->assertEquals($newUrlValue, $result); @@ -39,7 +39,7 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase { $tooltipValue = 'PHPExcel Web Site'; - $testInstance = new PHPExcel_Cell_Hyperlink(null, $tooltipValue); + $testInstance = new \PHPExcel\Cell\Hyperlink(null, $tooltipValue); $result = $testInstance->getTooltip(); $this->assertEquals($tooltipValue, $result); @@ -50,9 +50,9 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase $initialTooltipValue = 'PHPExcel Web Site'; $newTooltipValue = 'PHPExcel Repository on Github'; - $testInstance = new PHPExcel_Cell_Hyperlink(null, $initialTooltipValue); + $testInstance = new \PHPExcel\Cell\Hyperlink(null, $initialTooltipValue); $result = $testInstance->setTooltip($newTooltipValue); - $this->assertTrue($result instanceof PHPExcel_Cell_Hyperlink); + $this->assertTrue($result instanceof \PHPExcel\Cell\Hyperlink); $result = $testInstance->getTooltip(); $this->assertEquals($newTooltipValue, $result); @@ -63,7 +63,7 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase $initialUrlValue = 'http://www.phpexcel.net'; $newUrlValue = 'sheet://Worksheet1!A1'; - $testInstance = new PHPExcel_Cell_Hyperlink($initialUrlValue); + $testInstance = new \PHPExcel\Cell\Hyperlink($initialUrlValue); $result = $testInstance->isInternal(); $this->assertFalse($result); @@ -76,9 +76,9 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase { $urlValue = 'http://www.phpexcel.net'; $tooltipValue = 'PHPExcel Web Site'; - $initialExpectedHash = 'd84d713aed1dbbc8a7c5af183d6c7dbb'; + $initialExpectedHash = '176f1ec64e84084db814481bd710b6b3'; - $testInstance = new PHPExcel_Cell_Hyperlink($urlValue, $tooltipValue); + $testInstance = new \PHPExcel\Cell\Hyperlink($urlValue, $tooltipValue); $result = $testInstance->getHashCode(); $this->assertEquals($initialExpectedHash, $result); diff --git a/unitTests/rawTestData/Cell/DefaultValueBinder.json b/unitTests/rawTestData/Cell/DefaultValueBinder.json new file mode 100644 index 00000000..9bb33a7f --- /dev/null +++ b/unitTests/rawTestData/Cell/DefaultValueBinder.json @@ -0,0 +1,19 @@ +[null,"null"] +[null,"null"] +["#NULL!","e"] +[false,"b"] +[true,"b"] +["FALSE","s"] +["TRUE","s"] +["","s"] +["ABC","s"] +["123","n"] +[123,"n"] +[0.123,"n"] +["-123","n"] +["1.23E4","n"] +["-1.23E4","n"] +["1.23E-4","n"] +["000123","s"] +["=123","f"] +["#DIV\/0!","e"] diff --git a/unitTests/testDataFileIteratorJson.php b/unitTests/testDataFileIteratorJson.php new file mode 100644 index 00000000..c1c89d09 --- /dev/null +++ b/unitTests/testDataFileIteratorJson.php @@ -0,0 +1,63 @@ +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() + { + // Read a line of test data from the file + do { + // Only take lines that contain test data and that aren't commented out + $testDataRow = trim(fgets($this->file)); + } while (($testDataRow > '') && ($testDataRow{0} === '#')); + + // Discard any comments at the end of the line + list($testData) = explode('//', $testDataRow); + + // Split data into an array of individual values and a result + $dataSet = json_decode(trim($testData)); + + return $dataSet; + } +}