mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-13 11:36:24 +00:00
Adding tests for conversion of string percentage to numeric
Adding two test cases: 1) Test the function in `StringHelper.php`. Modeled this test on the test for the `convertToNumberIfFraction` function. 2) Test a spreadsheet with a string percentage in a formula to see if it calculates the formula correctly.
This commit is contained in:
@@ -180,6 +180,18 @@ class CalculationTest extends TestCase
|
||||
self::assertEquals('9', $cell3->getCalculatedValue());
|
||||
}
|
||||
|
||||
public function testCellWithStringPercentage(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$workSheet = $spreadsheet->getActiveSheet();
|
||||
$cell1 = $workSheet->getCell('A1');
|
||||
$cell1->setValue('2%');
|
||||
$cell2 = $workSheet->getCell('B1');
|
||||
$cell2->setValue('=100*A1');
|
||||
|
||||
self::assertEquals('2', $cell2->getCalculatedValue());
|
||||
}
|
||||
|
||||
public function testBranchPruningFormulaParsingSimpleCase(): void
|
||||
{
|
||||
$calculation = Calculation::getInstance();
|
||||
|
||||
@@ -148,4 +148,33 @@ class StringHelperTest extends TestCase
|
||||
'improper fraction' => ['1.75', '7/4'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerPercentages
|
||||
*/
|
||||
public function testPercentage(string $expected, string $value): void
|
||||
{
|
||||
$originalValue = $value;
|
||||
$result = StringHelper::convertToNumberIfPercent($value);
|
||||
if ($result === false) {
|
||||
self::assertSame($expected, $originalValue);
|
||||
self::assertSame($expected, $value);
|
||||
} else {
|
||||
self::assertSame($expected, (string) $value);
|
||||
self::assertNotEquals($value, $originalValue);
|
||||
}
|
||||
}
|
||||
|
||||
public function providerPercentages(): array
|
||||
{
|
||||
return [
|
||||
'non-percentage' => ['10', '10'],
|
||||
'single digit percentage' => ['0.02', '2%'],
|
||||
'two digit percentage' => ['0.13', '13%'],
|
||||
'negative single digit percentage' => ['-0.07', '-7%'],
|
||||
'negative two digit percentage' => ['-0.75', '-75%'],
|
||||
'large percentage' => ['98.45', '9845%'],
|
||||
'small percentage' => ['0.0005', '0.05%'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user