diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e36a8d4b..bea22d6ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a ### Deprecated -- Nothing yet. +- $dataType, the second parameter of Cell::setValueExplicit, is currently optional. Omitting it is deprecated, and it will be required in a future release. ### Fixed diff --git a/src/PhpSpreadsheet/Cell/Cell.php b/src/PhpSpreadsheet/Cell/Cell.php index e9fbabb41..e2a84bb27 100644 --- a/src/PhpSpreadsheet/Cell/Cell.php +++ b/src/PhpSpreadsheet/Cell/Cell.php @@ -247,6 +247,8 @@ class Cell implements Stringable * * @param mixed $value Value * @param string $dataType Explicit data type, see DataType::TYPE_* + * This parameter is currently optional (default = string). + * Omitting it is ***DEPRECATED***, and the default will be removed in a future release. * Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this * method, then it is your responsibility as an end-user developer to validate that the value and * the datatype match. diff --git a/tests/PhpSpreadsheetTests/Cell/SetValueExplicitCellTest.php b/tests/PhpSpreadsheetTests/Cell/SetValueExplicitCellTest.php new file mode 100644 index 000000000..ec166aa9e --- /dev/null +++ b/tests/PhpSpreadsheetTests/Cell/SetValueExplicitCellTest.php @@ -0,0 +1,60 @@ +method); + $requiredParameters = $reflectionMethod->getNumberOfRequiredParameters(); + self::assertSame($this->requiredParameters, $requiredParameters); + } + + public static function setValueExplicitTypeArgumentProvider(): array + { + return require 'tests/data/Cell/SetValueExplicitTypeArguments.php'; + } + + #[DataProvider('setValueExplicitTypeArgumentProvider')] + public function testSetValueExplicitTypeArgumentHandling( + mixed $value, + ?string $dataType, + mixed $expectedValue, + string $expectedDataType + ): void { + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + $coordinate = 'A1'; + $cell = $worksheet->getCell($coordinate); + + try { + if ($dataType !== null) { + $cell->{$this->method}($value, $dataType); + } else { + $cell->{$this->method}($value); + self::assertSame(1, $this->requiredParameters); + } + self::assertSame($expectedValue, $cell->getValue()); + self::assertSame($expectedDataType, $cell->getDataType()); + } catch (ArgumentCountError) { + self::assertSame(2, $this->requiredParameters); + self::assertNull($dataType); + } + + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/PhpSpreadsheetTests/Worksheet/SetValueExplicitWorksheetTest.php b/tests/PhpSpreadsheetTests/Worksheet/SetValueExplicitWorksheetTest.php new file mode 100644 index 000000000..02e0f8662 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Worksheet/SetValueExplicitWorksheetTest.php @@ -0,0 +1,60 @@ +method); + $requiredParameters = $reflectionMethod->getNumberOfRequiredParameters(); + self::assertSame($this->requiredParameters, $requiredParameters); + } + + public static function setCellValueExplicitTypeArgumentProvider(): array + { + return require 'tests/data/Cell/SetValueExplicitTypeArguments.php'; + } + + #[DataProvider('setCellValueExplicitTypeArgumentProvider')] + public function testSetCellValueExplicitTypeArgumentHandling( + mixed $value, + ?string $dataType, + mixed $expectedValue, + string $expectedDataType + ): void { + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + $coordinate = 'A1'; + $cell = $worksheet->getCell($coordinate); + + try { + if ($dataType) { + $worksheet->{$this->method}($coordinate, $value, $dataType); + } else { + $worksheet->{$this->method}($coordinate, $value); + self::assertSame(2, $this->requiredParameters); + } + self::assertSame($expectedValue, $cell->getValue()); + self::assertSame($expectedDataType, $cell->getDataType()); + } catch (ArgumentCountError) { + self::assertSame(3, $this->requiredParameters); + self::assertNull($dataType); + } + + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/data/Cell/SetValueExplicitTypeArguments.php b/tests/data/Cell/SetValueExplicitTypeArguments.php new file mode 100644 index 000000000..387c3b610 --- /dev/null +++ b/tests/data/Cell/SetValueExplicitTypeArguments.php @@ -0,0 +1,21 @@ + ['default string', null, 'default string', DataType::TYPE_STRING], + 'integer with default type' => [42, null, '42', DataType::TYPE_STRING], + 'string with string' => ['explicit string', DataType::TYPE_STRING, 'explicit string', DataType::TYPE_STRING], + 'integer with string type' => [123, DataType::TYPE_STRING, '123', DataType::TYPE_STRING], + 'numeric string with string type' => ['496', DataType::TYPE_STRING, '496', DataType::TYPE_STRING], + 'integer with numeric type' => [591, DataType::TYPE_NUMERIC, 591, DataType::TYPE_NUMERIC], + 'numeric string with numeric type' => ['1887', DataType::TYPE_NUMERIC, 1887, DataType::TYPE_NUMERIC], + 'true with bool type' => [true, DataType::TYPE_BOOL, true, DataType::TYPE_BOOL], + 'false with bool type' => [false, DataType::TYPE_BOOL, false, DataType::TYPE_BOOL], +];