mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-02 13:41:21 +00:00
78190ffdb6
In PR #4716, @RobinvanderVliet points out that Cell::setValueExplicit and Worksheet::setCellValueExplicit are strangely inconsistent, where the latter requires you to specify a DataType but the former does not. The fix in that PR is to make the latter offer a default DataType rather than requiring the parameter. While that does eliminate inconsistency, I think it does so in the wrong direction - the solution should be to eliminate the optionality in the former - an implicit value for setValueExplicit just doesn't make sense. We can't do that without a breaking change, which this is not. However, we can update the doc-block and change log to indicate our intention to make that change in the next breaking release.
61 lines
1.9 KiB
PHP
61 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use ArgumentCountError;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use ReflectionMethod;
|
|
|
|
class SetValueExplicitWorksheetTest extends TestCase
|
|
{
|
|
protected string $method = 'setCellValueExplicit';
|
|
|
|
protected int $requiredParameters = 3;
|
|
|
|
public function testRequired(): void
|
|
{
|
|
$reflectionMethod = new ReflectionMethod(Worksheet::class, $this->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();
|
|
}
|
|
}
|