From e19228ecb0b97e7c95f57b1b9be2d246e224a3eb Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 3 Jan 2020 23:29:53 +0100 Subject: [PATCH] Additional cell datatype unit tests (#1301) --- src/PhpSpreadsheet/Cell/Cell.php | 3 ++ .../Shared/JAMA/QRDecomposition.php | 2 +- tests/PhpSpreadsheetTests/Cell/CellTest.php | 23 +++++++++++++ tests/data/Cell/SetValueExplicit.php | 33 ++++++++++++++++--- tests/data/Cell/SetValueExplicitException.php | 10 ++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 tests/data/Cell/SetValueExplicitException.php diff --git a/src/PhpSpreadsheet/Cell/Cell.php b/src/PhpSpreadsheet/Cell/Cell.php index d20b8a56..cdef9018 100644 --- a/src/PhpSpreadsheet/Cell/Cell.php +++ b/src/PhpSpreadsheet/Cell/Cell.php @@ -217,6 +217,9 @@ class Cell break; case DataType::TYPE_NUMERIC: + if (is_string($pValue) && !is_numeric($pValue)) { + throw new Exception('Invalid numeric value for datatype Numeric'); + } $this->value = 0 + $pValue; break; diff --git a/src/PhpSpreadsheet/Shared/JAMA/QRDecomposition.php b/src/PhpSpreadsheet/Shared/JAMA/QRDecomposition.php index e666d74b..3bb8a10e 100644 --- a/src/PhpSpreadsheet/Shared/JAMA/QRDecomposition.php +++ b/src/PhpSpreadsheet/Shared/JAMA/QRDecomposition.php @@ -60,7 +60,7 @@ class QRDecomposition { if ($A instanceof Matrix) { // Initialize. - $this->QR = $A->getArrayCopy(); + $this->QR = $A->getArray(); $this->m = $A->getRowDimension(); $this->n = $A->getColumnDimension(); // Main loop. diff --git a/tests/PhpSpreadsheetTests/Cell/CellTest.php b/tests/PhpSpreadsheetTests/Cell/CellTest.php index 884ed239..d8c38585 100644 --- a/tests/PhpSpreadsheetTests/Cell/CellTest.php +++ b/tests/PhpSpreadsheetTests/Cell/CellTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Cell; +use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; @@ -27,4 +28,26 @@ class CellTest extends TestCase { return require 'data/Cell/SetValueExplicit.php'; } + + /** + * @dataProvider providerSetValueExplicitException + * + * @param mixed $expected + * @param mixed $value + * @param string $dataType + */ + public function testSetValueExplicitException($value, string $dataType) + { + $this->expectException(Exception::class); + + $spreadsheet = new Spreadsheet(); + $cell = $spreadsheet->getActiveSheet()->getCell('A1'); + $cell->setValueExplicit($value, $dataType); + } + + public function providerSetValueExplicitException() + { + return require 'data/Cell/SetValueExplicitException.php'; + } + } diff --git a/tests/data/Cell/SetValueExplicit.php b/tests/data/Cell/SetValueExplicit.php index 3ac039fe..50b81019 100644 --- a/tests/data/Cell/SetValueExplicit.php +++ b/tests/data/Cell/SetValueExplicit.php @@ -8,19 +8,44 @@ return [ '01234567890123456789', DataType::TYPE_NUMERIC, ], + [ + 1234567890123456789, + 1234567890123456789, + DataType::TYPE_NUMERIC, + ], [ 123.456, '123.456', DataType::TYPE_NUMERIC, ], [ - 1234567890123456789, - 1234567890123456789, + 123.456, + 123.456, DataType::TYPE_NUMERIC, ], [ - 123.456, - 123.456, + 0, + null, DataType::TYPE_NUMERIC, ], + [ + 0, + false, + DataType::TYPE_NUMERIC, + ], + [ + 1, + true, + DataType::TYPE_NUMERIC, + ], + [ + '#DIV/0!', + '#DIV/0!', + DataType::TYPE_ERROR, + ], + [ + '#NULL!', + 'NOT A VALID ERROR TYPE VALUE', + DataType::TYPE_ERROR, + ], ]; diff --git a/tests/data/Cell/SetValueExplicitException.php b/tests/data/Cell/SetValueExplicitException.php new file mode 100644 index 00000000..a05704f0 --- /dev/null +++ b/tests/data/Cell/SetValueExplicitException.php @@ -0,0 +1,10 @@ +