Skip to content

Code Coverage >90% #2973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Calculation/FormulaParser.php

-
message: "#^Strict comparison using \\=\\=\\= between string and null will always evaluate to false\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/FormulaParser.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Functions\\:\\:ifCondition\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/FormulaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FormulaParser
/**
* Create a new FormulaParser.
*
* @param string $formula Formula to parse
* @param ?string $formula Formula to parse
*/
public function __construct($formula = '')
{
Expand Down
151 changes: 151 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/FormulaParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?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');
$result = 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 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'],
],
],
];
}
}