PhpSpreadsheet/tests/PhpSpreadsheetTests/Cell/DefaultValueBinderTest.php

99 lines
2.6 KiB
PHP
Raw Normal View History

2015-03-27 21:28:26 +00:00
<?php
namespace PhpOffice\PhpSpreadsheetTests\Cell;
2015-03-27 21:28:26 +00:00
use DateTime;
use DateTimeImmutable;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PHPUnit\Framework\TestCase;
class DefaultValueBinderTest extends TestCase
2015-03-27 21:28:26 +00:00
{
private function createCellStub()
{
// Create a stub for the Cell class.
/** @var Cell $cellStub */
$cellStub = $this->getMockBuilder(Cell::class)
->disableOriginalConstructor()
->getMock();
// Configure the stub.
2020-05-18 04:49:57 +00:00
$cellStub->expects(self::any())
->method('setValueExplicit')
2020-05-18 04:49:57 +00:00
->willReturn(true);
return $cellStub;
}
/**
* @dataProvider binderProvider
2016-12-22 14:43:37 +00:00
*
* @param mixed $value
*/
2020-05-18 04:49:57 +00:00
public function testBindValue($value): void
2015-05-17 13:00:02 +00:00
{
$cellStub = $this->createCellStub();
$binder = new DefaultValueBinder();
$result = $binder->bindValue($cellStub, $value);
self::assertTrue($result);
2015-05-17 13:00:02 +00:00
}
public function binderProvider()
{
return [
[null],
[''],
['ABC'],
['=SUM(A1:B2)'],
[true],
[false],
[123],
[-123.456],
['123'],
['-123.456'],
['#REF!'],
[new DateTime()],
[new DateTimeImmutable()],
];
}
2015-03-27 21:28:26 +00:00
/**
* @dataProvider providerDataTypeForValue
*
* @param mixed $expectedResult
2015-03-27 21:28:26 +00:00
*/
2020-05-18 04:49:57 +00:00
public function testDataTypeForValue($expectedResult, ...$args): void
2015-05-17 13:00:02 +00:00
{
$result = DefaultValueBinder::dataTypeForValue(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
2015-03-27 21:28:26 +00:00
public function providerDataTypeForValue()
{
return require 'tests/data/Cell/DefaultValueBinder.php';
2015-05-17 13:00:02 +00:00
}
2015-03-27 21:28:26 +00:00
2020-05-18 04:49:57 +00:00
public function testDataTypeForRichTextObject(): void
2015-05-17 13:00:02 +00:00
{
$objRichText = new RichText();
$objRichText->createText('Hello World');
$expectedResult = DataType::TYPE_INLINE;
$result = DefaultValueBinder::dataTypeForValue($objRichText);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testCanOverrideStaticMethodWithoutOverridingBindValue(): void
{
$cellStub = $this->createCellStub();
$binder = new ValueBinderWithOverriddenDataTypeForValue();
self::assertFalse($binder::$called);
$binder->bindValue($cellStub, 123);
self::assertTrue($binder::$called);
}
2015-03-27 21:28:26 +00:00
}