mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 03:59:11 +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+.
152 lines
5.1 KiB
PHP
152 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\FormulaParser;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class FormulaParserTest extends TestCase
|
|
{
|
|
public function testNullFormula(): void
|
|
{
|
|
$this->expectException(CalcException::class);
|
|
$this->expectExceptionMessage('Invalid parameter passed: formula');
|
|
new FormulaParser(null);
|
|
}
|
|
|
|
public function testInvalidTokenId(): void
|
|
{
|
|
$this->expectException(CalcException::class);
|
|
$this->expectExceptionMessage('Token with id 1 does not exist.');
|
|
$result = new FormulaParser('=2');
|
|
$result->getToken(1);
|
|
}
|
|
|
|
public function testNoFormula(): void
|
|
{
|
|
$result = new FormulaParser('');
|
|
self::assertSame(0, $result->getTokenCount());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerFormulaParser
|
|
*/
|
|
public function testFormulaParser(string $formula, array $expectedResult): void
|
|
{
|
|
$formula = "=$formula";
|
|
$result = new FormulaParser($formula);
|
|
self::assertSame($formula, $result->getFormula());
|
|
self::assertSame(count($expectedResult), $result->getTokenCount());
|
|
$tokens = $result->getTokens();
|
|
$token0 = $result->getToken(0);
|
|
self::assertSame($tokens[0], $token0);
|
|
$idx = -1;
|
|
foreach ($expectedResult as $resultArray) {
|
|
++$idx;
|
|
self::assertSame($resultArray[0], $tokens[$idx]->getValue());
|
|
self::assertSame($resultArray[1], $tokens[$idx]->getTokenType());
|
|
self::assertSame($resultArray[2], $tokens[$idx]->getTokenSubType());
|
|
}
|
|
}
|
|
|
|
public static function providerFormulaParser(): array
|
|
{
|
|
return [
|
|
['5%*(2+(-3))+A3',
|
|
[
|
|
['5', 'Operand', 'Number'],
|
|
['%', 'OperatorPostfix', 'Nothing'],
|
|
['*', 'OperatorInfix', 'Math'],
|
|
['', 'Subexpression', 'Start'],
|
|
['2', 'Operand', 'Number'],
|
|
['+', 'OperatorInfix', 'Math'],
|
|
['', 'Subexpression', 'Start'],
|
|
['-', 'OperatorPrefix', 'Nothing'],
|
|
['3', 'Operand', 'Number'],
|
|
['', 'Subexpression', 'Stop'],
|
|
['', 'Subexpression', 'Stop'],
|
|
['+', 'OperatorInfix', 'Math'],
|
|
['A3', 'Operand', 'Range'],
|
|
],
|
|
],
|
|
['"hello" & "goodbye"',
|
|
[
|
|
['hello', 'Operand', 'Text'],
|
|
['&', 'OperatorInfix', 'Concatenation'],
|
|
['goodbye', 'Operand', 'Text'],
|
|
],
|
|
],
|
|
['+1.23E5',
|
|
[
|
|
['1.23E5', 'Operand', 'Number'],
|
|
],
|
|
],
|
|
['#DIV/0!',
|
|
[
|
|
['#DIV/0!', 'Operand', 'Error'],
|
|
],
|
|
],
|
|
['"HE""LLO"',
|
|
[
|
|
['HE"LLO', 'Operand', 'Text'],
|
|
],
|
|
],
|
|
['MINVERSE({3,1;4,2})',
|
|
[
|
|
['MINVERSE', 'Function', 'Start'],
|
|
['ARRAY', 'Function', 'Start'],
|
|
['ARRAYROW', 'Function', 'Start'],
|
|
['3', 'Operand', 'Number'],
|
|
[',', 'OperatorInfix', 'Union'],
|
|
['1', 'Operand', 'Number'],
|
|
['', 'Function', 'Stop'],
|
|
[',', 'Argument', 'Nothing'],
|
|
['ARRAYROW', 'Function', 'Start'],
|
|
['4', 'Operand', 'Number'],
|
|
[',', 'OperatorInfix', 'Union'],
|
|
['2', 'Operand', 'Number'],
|
|
['', 'Function', 'Stop'],
|
|
['', 'Function', 'Stop'],
|
|
['', 'Function', 'Stop'],
|
|
],
|
|
],
|
|
['[1,1]*5',
|
|
[
|
|
['[1,1]', 'Operand', 'Range'],
|
|
['*', 'OperatorInfix', 'Math'],
|
|
['5', 'Operand', 'Number'],
|
|
],
|
|
],
|
|
['IF(A1>=0,2,3)',
|
|
[
|
|
['IF', 'Function', 'Start'],
|
|
['A1', 'Operand', 'Range'],
|
|
['>=', 'OperatorInfix', 'Logical'],
|
|
['0', 'Operand', 'Number'],
|
|
[',', 'OperatorInfix', 'Union'],
|
|
['2', 'Operand', 'Number'],
|
|
[',', 'OperatorInfix', 'Union'],
|
|
['3', 'Operand', 'Number'],
|
|
['', 'Function', 'Stop'],
|
|
],
|
|
],
|
|
["'Worksheet'!A1:A3",
|
|
[
|
|
['Worksheet!A1:A3', 'Operand', 'Range'],
|
|
],
|
|
],
|
|
["'Worksh''eet'!A1:A3",
|
|
[
|
|
['Worksh\'eet!A1:A3', 'Operand', 'Range'],
|
|
],
|
|
],
|
|
['true',
|
|
[
|
|
['true', 'Operand', 'Logical'],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
}
|