Unit tests for Cell classes, modification to use json-format in raw data provider

This commit is contained in:
MarkBaker 2015-05-28 08:29:44 +01:00
parent fe51b5e4fa
commit 53584ec9a3
7 changed files with 129 additions and 47 deletions

View File

@ -43,7 +43,7 @@ class DefaultValueBinder implements IValueBinder
$value = \PHPExcel\Shared\String::SanitizeUTF8($value); $value = \PHPExcel\Shared\String::SanitizeUTF8($value);
} elseif (is_object($value)) { } elseif (is_object($value)) {
// Handle any objects that might be injected // Handle any objects that might be injected
if ($value instanceof DateTime) { if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d H:i:s'); $value = $value->format('Y-m-d H:i:s');
} elseif (!($value instanceof \PHPExcel\RichText)) { } elseif (!($value instanceof \PHPExcel\RichText)) {
$value = (string) $value; $value = (string) $value;

View File

@ -27,22 +27,22 @@ namespace PHPExcel;
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE## * @version ##VERSION##, ##DATE##
*/ */
class RichText implements PHPExcel_IComparable class RichText implements IComparable
{ {
/** /**
* Rich text elements * Rich text elements
* *
* @var PHPExcel_RichText_ITextElement[] * @var RichText\ITextElement[]
*/ */
private $richTextElements; private $richTextElements;
/** /**
* Create a new PHPExcel_RichText instance * Create a new RichText instance
* *
* @param PHPExcel_Cell $pCell * @param Cell $pCell
* @throws PHPExcel_Exception * @throws Exception
*/ */
public function __construct(PHPExcel_Cell $pCell = null) public function __construct(Cell $pCell = null)
{ {
// Initialise variables // Initialise variables
$this->richTextElements = array(); $this->richTextElements = array();
@ -51,24 +51,24 @@ class RichText implements PHPExcel_IComparable
if ($pCell !== null) { if ($pCell !== null) {
// Add cell text and style // Add cell text and style
if ($pCell->getValue() != "") { 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()); $objRun->setFont(clone $pCell->getParent()->getStyle($pCell->getCoordinate())->getFont());
$this->addText($objRun); $this->addText($objRun);
} }
// Set parent value // Set parent value
$pCell->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING); $pCell->setValueExplicit($this, Cell\DataType::TYPE_STRING);
} }
} }
/** /**
* Add text * Add text
* *
* @param PHPExcel_RichText_ITextElement $pText Rich text element * @param RichText\ITextElement $pText Rich text element
* @throws PHPExcel_Exception * @throws Exception
* @return PHPExcel_RichText * @return RichText
*/ */
public function addText(PHPExcel_RichText_ITextElement $pText = null) public function addText(RichText\ITextElement $pText = null)
{ {
$this->richTextElements[] = $pText; $this->richTextElements[] = $pText;
return $this; return $this;
@ -78,12 +78,12 @@ class RichText implements PHPExcel_IComparable
* Create text * Create text
* *
* @param string $pText Text * @param string $pText Text
* @return PHPExcel_RichText_TextElement * @return RichText\TextElement
* @throws PHPExcel_Exception * @throws Exception
*/ */
public function createText($pText = '') public function createText($pText = '')
{ {
$objText = new PHPExcel_RichText_TextElement($pText); $objText = new RichText\TextElement($pText);
$this->addText($objText); $this->addText($objText);
return $objText; return $objText;
} }
@ -92,12 +92,12 @@ class RichText implements PHPExcel_IComparable
* Create text run * Create text run
* *
* @param string $pText Text * @param string $pText Text
* @return PHPExcel_RichText_Run * @return RichText\Run
* @throws PHPExcel_Exception * @throws Exception
*/ */
public function createTextRun($pText = '') public function createTextRun($pText = '')
{ {
$objText = new PHPExcel_RichText_Run($pText); $objText = new RichText\Run($pText);
$this->addText($objText); $this->addText($objText);
return $objText; return $objText;
} }
@ -112,12 +112,11 @@ class RichText implements PHPExcel_IComparable
// Return value // Return value
$returnValue = ''; $returnValue = '';
// Loop through all PHPExcel_RichText_ITextElement // Loop through all RichText\ITextElements
foreach ($this->richTextElements as $text) { foreach ($this->richTextElements as $text) {
$returnValue .= $text->getText(); $returnValue .= $text->getText();
} }
// Return
return $returnValue; return $returnValue;
} }
@ -134,7 +133,7 @@ class RichText implements PHPExcel_IComparable
/** /**
* Get Rich Text elements * Get Rich Text elements
* *
* @return PHPExcel_RichText_ITextElement[] * @return RichText\ITextElement[]
*/ */
public function getRichTextElements() public function getRichTextElements()
{ {
@ -144,16 +143,16 @@ class RichText implements PHPExcel_IComparable
/** /**
* Set Rich Text elements * Set Rich Text elements
* *
* @param PHPExcel_RichText_ITextElement[] $pElements Array of elements * @param RichText\ITextElement[] $pElements Array of elements
* @throws PHPExcel_Exception * @throws Exception
* @return PHPExcel_RichText * @return RichText
*/ */
public function setRichTextElements($pElements = null) public function setRichTextElements($pElements = null)
{ {
if (is_array($pElements)) { if (is_array($pElements)) {
$this->richTextElements = $pElements; $this->richTextElements = $pElements;
} else { } else {
throw new PHPExcel_Exception("Invalid PHPExcel_RichText_ITextElement[] array passed."); throw new Exception("Invalid \PHPExcel\RichText\ITextElement[] array passed.");
} }
return $this; return $this;
} }

View File

@ -9,12 +9,12 @@ class DataTypeTest extends PHPUnit_Framework_TestCase
if (!defined('PHPEXCEL_ROOT')) { if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/'); define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
} }
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); require_once(PHPEXCEL_ROOT . '/Bootstrap.php');
} }
public function testGetErrorCodes() 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->assertInternalType('array', $result);
$this->assertGreaterThan(0, count($result)); $this->assertGreaterThan(0, count($result));
$this->assertArrayHasKey('#NULL!', $result); $this->assertArrayHasKey('#NULL!', $result);

View File

@ -1,6 +1,7 @@
<?php <?php
require_once 'testDataFileIterator.php'; require_once 'testDataFileIterator.php';
require_once 'testDataFileIteratorJson.php';
class DefaultValueBinderTest extends PHPUnit_Framework_TestCase class DefaultValueBinderTest extends PHPUnit_Framework_TestCase
{ {
@ -11,13 +12,13 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase
if (!defined('PHPEXCEL_ROOT')) { if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/'); define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
} }
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); require_once(PHPEXCEL_ROOT . '/Bootstrap.php');
} }
protected function createCellStub() protected function createCellStub()
{ {
// Create a stub for the Cell class. // Create a stub for the Cell class.
$this->cellStub = $this->getMockBuilder('PHPExcel_Cell') $this->cellStub = $this->getMockBuilder('\\PHPExcel\\Cell')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
// Configure the stub. // Configure the stub.
@ -33,7 +34,7 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase
public function testBindValue($value) public function testBindValue($value)
{ {
$this->createCellStub(); $this->createCellStub();
$binder = new PHPExcel_Cell_DefaultValueBinder(); $binder = new \PHPExcel\Cell\DefaultValueBinder();
$result = $binder->bindValue($this->cellStub, $value); $result = $binder->bindValue($this->cellStub, $value);
$this->assertTrue($result); $this->assertTrue($result);
} }
@ -52,7 +53,7 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase
array('123'), array('123'),
array('-123.456'), array('-123.456'),
array('#REF!'), array('#REF!'),
array(new DateTime()), array(new \DateTime()),
); );
} }
@ -63,22 +64,22 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase
{ {
$args = func_get_args(); $args = func_get_args();
$expectedResult = array_pop($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); $this->assertEquals($expectedResult, $result);
} }
public function providerDataTypeForValue() public function providerDataTypeForValue()
{ {
return new testDataFileIterator('rawTestData/Cell/DefaultValueBinder.data'); return new testDataFileIteratorJson('rawTestData/Cell/DefaultValueBinder.json');
} }
public function testDataTypeForRichTextObject() public function testDataTypeForRichTextObject()
{ {
$objRichText = new PHPExcel_RichText(); $objRichText = new \PHPExcel\RichText();
$objRichText->createText('Hello World'); $objRichText->createText('Hello World');
$expectedResult = PHPExcel_Cell_DataType::TYPE_INLINE; $expectedResult = \PHPExcel\Cell\DataType::TYPE_INLINE;
$result = call_user_func(array('PHPExcel_Cell_DefaultValueBinder','dataTypeForValue'), $objRichText); $result = call_user_func(array('\\PHPExcel\\Cell\\DefaultValueBinder','dataTypeForValue'), $objRichText);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
} }

View File

@ -9,14 +9,14 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase
if (!defined('PHPEXCEL_ROOT')) { if (!defined('PHPEXCEL_ROOT')) {
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/'); define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
} }
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); require_once(PHPEXCEL_ROOT . '/Bootstrap.php');
} }
public function testGetUrl() public function testGetUrl()
{ {
$urlValue = 'http://www.phpexcel.net'; $urlValue = 'http://www.phpexcel.net';
$testInstance = new PHPExcel_Cell_Hyperlink($urlValue); $testInstance = new \PHPExcel\Cell\Hyperlink($urlValue);
$result = $testInstance->getUrl(); $result = $testInstance->getUrl();
$this->assertEquals($urlValue, $result); $this->assertEquals($urlValue, $result);
@ -27,9 +27,9 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase
$initialUrlValue = 'http://www.phpexcel.net'; $initialUrlValue = 'http://www.phpexcel.net';
$newUrlValue = 'http://github.com/PHPOffice/PHPExcel'; $newUrlValue = 'http://github.com/PHPOffice/PHPExcel';
$testInstance = new PHPExcel_Cell_Hyperlink($initialUrlValue); $testInstance = new \PHPExcel\Cell\Hyperlink($initialUrlValue);
$result = $testInstance->setUrl($newUrlValue); $result = $testInstance->setUrl($newUrlValue);
$this->assertTrue($result instanceof PHPExcel_Cell_Hyperlink); $this->assertTrue($result instanceof \PHPExcel\Cell\Hyperlink);
$result = $testInstance->getUrl(); $result = $testInstance->getUrl();
$this->assertEquals($newUrlValue, $result); $this->assertEquals($newUrlValue, $result);
@ -39,7 +39,7 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase
{ {
$tooltipValue = 'PHPExcel Web Site'; $tooltipValue = 'PHPExcel Web Site';
$testInstance = new PHPExcel_Cell_Hyperlink(null, $tooltipValue); $testInstance = new \PHPExcel\Cell\Hyperlink(null, $tooltipValue);
$result = $testInstance->getTooltip(); $result = $testInstance->getTooltip();
$this->assertEquals($tooltipValue, $result); $this->assertEquals($tooltipValue, $result);
@ -50,9 +50,9 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase
$initialTooltipValue = 'PHPExcel Web Site'; $initialTooltipValue = 'PHPExcel Web Site';
$newTooltipValue = 'PHPExcel Repository on Github'; $newTooltipValue = 'PHPExcel Repository on Github';
$testInstance = new PHPExcel_Cell_Hyperlink(null, $initialTooltipValue); $testInstance = new \PHPExcel\Cell\Hyperlink(null, $initialTooltipValue);
$result = $testInstance->setTooltip($newTooltipValue); $result = $testInstance->setTooltip($newTooltipValue);
$this->assertTrue($result instanceof PHPExcel_Cell_Hyperlink); $this->assertTrue($result instanceof \PHPExcel\Cell\Hyperlink);
$result = $testInstance->getTooltip(); $result = $testInstance->getTooltip();
$this->assertEquals($newTooltipValue, $result); $this->assertEquals($newTooltipValue, $result);
@ -63,7 +63,7 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase
$initialUrlValue = 'http://www.phpexcel.net'; $initialUrlValue = 'http://www.phpexcel.net';
$newUrlValue = 'sheet://Worksheet1!A1'; $newUrlValue = 'sheet://Worksheet1!A1';
$testInstance = new PHPExcel_Cell_Hyperlink($initialUrlValue); $testInstance = new \PHPExcel\Cell\Hyperlink($initialUrlValue);
$result = $testInstance->isInternal(); $result = $testInstance->isInternal();
$this->assertFalse($result); $this->assertFalse($result);
@ -76,9 +76,9 @@ class HyperlinkTest extends PHPUnit_Framework_TestCase
{ {
$urlValue = 'http://www.phpexcel.net'; $urlValue = 'http://www.phpexcel.net';
$tooltipValue = 'PHPExcel Web Site'; $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(); $result = $testInstance->getHashCode();
$this->assertEquals($initialExpectedHash, $result); $this->assertEquals($initialExpectedHash, $result);

View File

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

View File

@ -0,0 +1,63 @@
<?php
class testDataFileIteratorJson implements Iterator
{
protected $file;
protected $key = 0;
protected $current;
public function __construct($file)
{
$this->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;
}
}