Files
oleibman 70b4ecd4d3 Use calculateFormula Rather Than _calculateFormulaValue in Tests
They aren't quite interchangeable. Both are used in the test suite, with no indication of why one or the other. I think we'd be best off being consistent. Based on the names, I think `_calculateFormulaValue` was intended as a private, or at least internal, method, so favor `calculateFormula`. I do not intend to rename or re-categorize `_calculateFormulaValue`, just remove its usage when it isn't clearly warranted.
2025-11-13 20:48:46 -08:00

65 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PHPUnit\Framework\Attributes\DataProvider;
class SubstituteTest extends AllSetupTeardown
{
#[DataProvider('providerSUBSTITUTE')]
public function testSUBSTITUTE(mixed $expectedResult, mixed $text = 'omitted', mixed $oldText = 'omitted', mixed $newText = 'omitted', mixed $instance = 'omitted'): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
if ($text === 'omitted') {
$sheet->getCell('B1')->setValue('=SUBSTITUTE()');
} elseif ($oldText === 'omitted') {
$this->setCell('A1', $text);
$sheet->getCell('B1')->setValue('=SUBSTITUTE(A1)');
} elseif ($newText === 'omitted') {
$this->setCell('A1', $text);
$this->setCell('A2', $oldText);
$sheet->getCell('B1')->setValue('=SUBSTITUTE(A1, A2)');
} elseif ($instance === 'omitted') {
$this->setCell('A1', $text);
$this->setCell('A2', $oldText);
$this->setCell('A3', $newText);
$sheet->getCell('B1')->setValue('=SUBSTITUTE(A1, A2, A3)');
} else {
$this->setCell('A1', $text);
$this->setCell('A2', $oldText);
$this->setCell('A3', $newText);
$this->setCell('A4', $instance);
$sheet->getCell('B1')->setValue('=SUBSTITUTE(A1, A2, A3, A4)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public static function providerSUBSTITUTE(): array
{
return require 'tests/data/Calculation/TextData/SUBSTITUTE.php';
}
/** @param mixed[] $expectedResult */
#[DataProvider('providerSubstituteArray')]
public function testSubstituteArray(array $expectedResult, string $oldText, string $fromText, string $toText): void
{
$calculation = Calculation::getInstance();
$formula = "=SUBSTITUTE({$oldText}, {$fromText}, {$toText})";
$result = $calculation->calculateFormula($formula);
self::assertSame($expectedResult, $result);
}
public static function providerSubstituteArray(): array
{
return [
'row vector' => [[['ElePHPant', 'EleFFant']], '"Elephant"', '"ph"', '{"PHP", "FF"}'],
];
}
}