2015-03-27 21:28:26 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Cell;
|
2015-03-27 21:28:26 +00:00
|
|
|
|
2017-05-17 22:02:17 +00:00
|
|
|
use DateTime;
|
2017-12-23 12:50:07 +00:00
|
|
|
use DateTimeImmutable;
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class DefaultValueBinderTest extends TestCase
|
2015-03-27 21:28:26 +00:00
|
|
|
{
|
2017-12-23 12:50:07 +00:00
|
|
|
private function createCellStub()
|
2015-04-16 00:09:29 +00:00
|
|
|
{
|
|
|
|
// Create a stub for the Cell class.
|
2018-10-23 03:40:41 +00:00
|
|
|
/** @var Cell $cellStub */
|
|
|
|
$cellStub = $this->getMockBuilder(Cell::class)
|
2015-04-16 00:09:29 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2018-10-23 03:40:41 +00:00
|
|
|
|
2015-04-16 00:09:29 +00:00
|
|
|
// Configure the stub.
|
2018-10-23 03:40:41 +00:00
|
|
|
$cellStub->expects($this->any())
|
2017-01-05 03:26:20 +00:00
|
|
|
->method('setValueExplicit')
|
|
|
|
->will($this->returnValue(true));
|
2018-10-23 03:40:41 +00:00
|
|
|
|
|
|
|
return $cellStub;
|
2015-04-16 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider binderProvider
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
2015-04-16 00:09:29 +00:00
|
|
|
*/
|
|
|
|
public function testBindValue($value)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2018-10-23 03:40:41 +00:00
|
|
|
$cellStub = $this->createCellStub();
|
2016-08-14 04:08:43 +00:00
|
|
|
$binder = new DefaultValueBinder();
|
2018-10-23 03:40:41 +00:00
|
|
|
$result = $binder->bindValue($cellStub, $value);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertTrue($result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-04-16 00:09:29 +00:00
|
|
|
|
|
|
|
public function binderProvider()
|
|
|
|
{
|
2016-08-16 15:33:57 +00:00
|
|
|
return [
|
|
|
|
[null],
|
|
|
|
[''],
|
|
|
|
['ABC'],
|
|
|
|
['=SUM(A1:B2)'],
|
|
|
|
[true],
|
|
|
|
[false],
|
|
|
|
[123],
|
|
|
|
[-123.456],
|
|
|
|
['123'],
|
|
|
|
['-123.456'],
|
|
|
|
['#REF!'],
|
2017-05-17 22:02:17 +00:00
|
|
|
[new DateTime()],
|
2017-12-23 12:50:07 +00:00
|
|
|
[new DateTimeImmutable()],
|
2016-08-16 15:33:57 +00:00
|
|
|
];
|
2015-04-16 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 21:28:26 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerDataTypeForValue
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2015-03-27 21:28:26 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testDataTypeForValue($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = DefaultValueBinder::dataTypeForValue(...$args);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-03-27 21:28:26 +00:00
|
|
|
|
|
|
|
public function providerDataTypeForValue()
|
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
return require 'tests/data/Cell/DefaultValueBinder.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2015-03-27 21:28:26 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
public function testDataTypeForRichTextObject()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
$objRichText = new RichText();
|
2015-04-16 00:09:29 +00:00
|
|
|
$objRichText->createText('Hello World');
|
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
$expectedResult = DataType::TYPE_INLINE;
|
2016-08-26 06:39:29 +00:00
|
|
|
$result = DefaultValueBinder::dataTypeForValue($objRichText);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResult, $result);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2018-10-23 03:40:41 +00:00
|
|
|
|
|
|
|
public function testCanOverrideStaticMethodWithoutOverridingBindValue()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|