mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-18 14:06:34 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
91 lines
2.5 KiB
PHP
91 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Cell;
|
|
|
|
use DateTime;
|
|
use DateTimeImmutable;
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DefaultValueBinderTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider binderProvider
|
|
*
|
|
* @param mixed $value
|
|
*/
|
|
public function testBindValue($value): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$cell = $sheet->getCell('A1');
|
|
$binder = new DefaultValueBinder();
|
|
$result = $binder->bindValue($cell, $value);
|
|
self::assertTrue($result);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function binderProvider(): array
|
|
{
|
|
return [
|
|
[null],
|
|
[''],
|
|
['ABC'],
|
|
['=SUM(A1:B2)'],
|
|
[true],
|
|
[false],
|
|
[123],
|
|
[-123.456],
|
|
['123'],
|
|
['-123.456'],
|
|
['#REF!'],
|
|
[new DateTime()],
|
|
[new DateTimeImmutable()],
|
|
['123456\n'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDataTypeForValue
|
|
*
|
|
* @param mixed $expectedResult
|
|
* @param mixed $value
|
|
*/
|
|
public function testDataTypeForValue($expectedResult, $value): void
|
|
{
|
|
$result = DefaultValueBinder::dataTypeForValue($value);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerDataTypeForValue(): array
|
|
{
|
|
return require 'tests/data/Cell/DefaultValueBinder.php';
|
|
}
|
|
|
|
public function testDataTypeForRichTextObject(): void
|
|
{
|
|
$objRichText = new RichText();
|
|
$objRichText->createText('Hello World');
|
|
|
|
$expectedResult = DataType::TYPE_INLINE;
|
|
$result = DefaultValueBinder::dataTypeForValue($objRichText);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function testCanOverrideStaticMethodWithoutOverridingBindValue(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$cell = $sheet->getCell('A1');
|
|
$binder = new ValueBinderWithOverriddenDataTypeForValue();
|
|
|
|
self::assertFalse($binder::$called);
|
|
$binder->bindValue($cell, 123);
|
|
self::assertTrue($binder::$called);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|