2013-11-20 09:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-29 08:39:42 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
2018-10-21 06:40:21 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-05-29 20:35:35 +00:00
|
|
|
|
2017-11-08 15:48:01 +00:00
|
|
|
class CalculationTest extends TestCase
|
2013-11-20 09:55:05 +00:00
|
|
|
{
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
|
2013-11-20 09:55:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 08:39:42 +00:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$calculation->setLocale('en_us');
|
|
|
|
}
|
|
|
|
|
2013-11-20 09:55:05 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerBinaryComparisonOperation
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
|
|
|
* @param mixed $formula
|
|
|
|
* @param mixed $expectedResultExcel
|
|
|
|
* @param mixed $expectedResultOpenOffice
|
2013-11-20 09:55:05 +00:00
|
|
|
*/
|
|
|
|
public function testBinaryComparisonOperation($formula, $expectedResultExcel, $expectedResultOpenOffice)
|
|
|
|
{
|
2016-08-14 04:08:43 +00:00
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
|
2015-05-29 20:35:35 +00:00
|
|
|
$resultExcel = Calculation::getInstance()->_calculateFormulaValue($formula);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResultExcel, $resultExcel, 'should be Excel compatible');
|
2013-11-20 09:55:05 +00:00
|
|
|
|
2016-08-14 04:08:43 +00:00
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
|
2015-05-29 20:35:35 +00:00
|
|
|
$resultOpenOffice = Calculation::getInstance()->_calculateFormulaValue($formula);
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals($expectedResultOpenOffice, $resultOpenOffice, 'should be OpenOffice compatible');
|
2013-11-20 09:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providerBinaryComparisonOperation()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CalculationBinaryComparisonOperation.php';
|
2013-11-20 09:55:05 +00:00
|
|
|
}
|
2016-12-03 05:51:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerGetFunctions
|
2016-12-22 14:43:37 +00:00
|
|
|
*
|
2017-01-23 06:34:49 +00:00
|
|
|
* @param string $category
|
|
|
|
* @param array|string $functionCall
|
|
|
|
* @param string $argumentCount
|
2016-12-03 05:51:04 +00:00
|
|
|
*/
|
|
|
|
public function testGetFunctions($category, $functionCall, $argumentCount)
|
|
|
|
{
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertInternalType('callable', $functionCall);
|
2016-12-03 05:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providerGetFunctions()
|
|
|
|
{
|
|
|
|
return Calculation::getInstance()->getFunctions();
|
|
|
|
}
|
Use `::class` notation as much as possible
```
* ,
_/^\_
< >
* /.-.\ *
* `/&\` *
,@.*;@,
/_o.I %_\ *
* (`'--:o(_@;
/`;--.,__ `') *
;@`o % O,*`'`&\
* (`'--)_@ ;o %'()\ *
/`;--._`''--._O'@;
/&*,()~o`;-.,_ `""`)
* /`,@ ;+& () o*`;-';\
(`""--.,_0 +% @' &()\
/-.,_ ``''--....-'`) *
* /@%;o`:;'--,.__ __.'\
;*,&(); @ % &^;~`"`o;@(); *
/(); o^~; & ().o@*&`;&%O\
jgs `"="==""==,,,.,="=="==="`
__.----.(\-''#####---...___...-----._
'` \)_`"""""`
.--' ')
o( )_-\
`"""` `
```
2016-12-26 04:20:45 +00:00
|
|
|
|
|
|
|
public function testIsImplemented()
|
|
|
|
{
|
|
|
|
$calculation = Calculation::getInstance();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertFalse($calculation->isImplemented('non-existing-function'));
|
|
|
|
self::assertFalse($calculation->isImplemented('AREAS'));
|
|
|
|
self::assertTrue($calculation->isImplemented('coUNt'));
|
|
|
|
self::assertTrue($calculation->isImplemented('abs'));
|
Use `::class` notation as much as possible
```
* ,
_/^\_
< >
* /.-.\ *
* `/&\` *
,@.*;@,
/_o.I %_\ *
* (`'--:o(_@;
/`;--.,__ `') *
;@`o % O,*`'`&\
* (`'--)_@ ;o %'()\ *
/`;--._`''--._O'@;
/&*,()~o`;-.,_ `""`)
* /`,@ ;+& () o*`;-';\
(`""--.,_0 +% @' &()\
/-.,_ ``''--....-'`) *
* /@%;o`:;'--,.__ __.'\
;*,&(); @ % &^;~`"`o;@(); *
/(); o^~; & ().o@*&`;&%O\
jgs `"="==""==,,,.,="=="==="`
__.----.(\-''#####---...___...-----._
'` \)_`"""""`
.--' ')
o( )_-\
`"""` `
```
2016-12-26 04:20:45 +00:00
|
|
|
}
|
2017-01-23 06:34:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerCanLoadAllSupportedLocales
|
|
|
|
*
|
|
|
|
* @param string $locale
|
|
|
|
*/
|
|
|
|
public function testCanLoadAllSupportedLocales($locale)
|
|
|
|
{
|
|
|
|
$calculation = Calculation::getInstance();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertTrue($calculation->setLocale($locale));
|
2017-01-23 06:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providerCanLoadAllSupportedLocales()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['bg'],
|
|
|
|
['cs'],
|
|
|
|
['da'],
|
|
|
|
['de'],
|
|
|
|
['en_us'],
|
|
|
|
['es'],
|
|
|
|
['fi'],
|
|
|
|
['fr'],
|
|
|
|
['hu'],
|
|
|
|
['it'],
|
|
|
|
['nl'],
|
|
|
|
['no'],
|
|
|
|
['pl'],
|
|
|
|
['pt'],
|
|
|
|
['pt_br'],
|
|
|
|
['ru'],
|
|
|
|
['sv'],
|
|
|
|
['tr'],
|
|
|
|
];
|
|
|
|
}
|
2018-02-24 12:14:48 +00:00
|
|
|
|
|
|
|
public function testDoesHandleXlfnFunctions()
|
|
|
|
{
|
|
|
|
$calculation = Calculation::getInstance();
|
|
|
|
|
|
|
|
$tree = $calculation->parseFormula('=_xlfn.ISFORMULA(A1)');
|
|
|
|
self::assertCount(3, $tree);
|
|
|
|
$function = $tree[2];
|
|
|
|
self::assertEquals('Function', $function['type']);
|
|
|
|
|
|
|
|
$tree = $calculation->parseFormula('=_xlfn.STDEV.S(A1:B2)');
|
|
|
|
self::assertCount(5, $tree);
|
|
|
|
$function = $tree[4];
|
|
|
|
self::assertEquals('Function', $function['type']);
|
|
|
|
}
|
2018-10-21 06:40:21 +00:00
|
|
|
|
|
|
|
public function testFormulaWithOptionalArgumentsAndRequiredCellReferenceShouldPassNullForMissingArguments()
|
|
|
|
{
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
|
|
|
|
$sheet->fromArray(
|
|
|
|
[
|
|
|
|
[1, 2, 3],
|
|
|
|
[4, 5, 6],
|
|
|
|
[7, 8, 9],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$cell = $sheet->getCell('E5');
|
|
|
|
$cell->setValue('=OFFSET(D3, -1, -2, 1, 1)');
|
|
|
|
self::assertEquals(5, $cell->getCalculatedValue(), 'with all arguments');
|
|
|
|
|
|
|
|
$cell = $sheet->getCell('F6');
|
|
|
|
$cell->setValue('=OFFSET(D3, -1, -2)');
|
|
|
|
self::assertEquals(5, $cell->getCalculatedValue(), 'missing arguments should be filled with null');
|
|
|
|
}
|
2019-02-18 21:56:32 +00:00
|
|
|
|
|
|
|
public function testCellSetAsQuotedText()
|
|
|
|
{
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$workSheet = $spreadsheet->getActiveSheet();
|
|
|
|
$cell = $workSheet->getCell('A1');
|
|
|
|
|
|
|
|
$cell->setValue("=cmd|'/C calc'!A0");
|
|
|
|
$cell->getStyle()->setQuotePrefix(true);
|
|
|
|
|
|
|
|
self::assertEquals("=cmd|'/C calc'!A0", $cell->getCalculatedValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCellWithDdeExpresion()
|
|
|
|
{
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$workSheet = $spreadsheet->getActiveSheet();
|
|
|
|
$cell = $workSheet->getCell('A1');
|
|
|
|
|
|
|
|
$cell->setValue("=cmd|'/C calc'!A0");
|
|
|
|
|
|
|
|
self::assertEquals("=cmd|'/C calc'!A0", $cell->getCalculatedValue());
|
|
|
|
}
|
2013-11-20 09:55:05 +00:00
|
|
|
}
|