SubstituteTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class SubstituteTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerSUBSTITUTE
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $text
  11. * @param mixed $oldText
  12. * @param mixed $newText
  13. * @param mixed $instance
  14. */
  15. public function testSUBSTITUTE($expectedResult, $text = 'omitted', $oldText = 'omitted', $newText = 'omitted', $instance = 'omitted'): void
  16. {
  17. $this->mightHaveException($expectedResult);
  18. $sheet = $this->getSheet();
  19. if ($text === 'omitted') {
  20. $sheet->getCell('B1')->setValue('=SUBSTITUTE()');
  21. } elseif ($oldText === 'omitted') {
  22. $this->setCell('A1', $text);
  23. $sheet->getCell('B1')->setValue('=SUBSTITUTE(A1)');
  24. } elseif ($newText === 'omitted') {
  25. $this->setCell('A1', $text);
  26. $this->setCell('A2', $oldText);
  27. $sheet->getCell('B1')->setValue('=SUBSTITUTE(A1, A2)');
  28. } elseif ($instance === 'omitted') {
  29. $this->setCell('A1', $text);
  30. $this->setCell('A2', $oldText);
  31. $this->setCell('A3', $newText);
  32. $sheet->getCell('B1')->setValue('=SUBSTITUTE(A1, A2, A3)');
  33. } else {
  34. $this->setCell('A1', $text);
  35. $this->setCell('A2', $oldText);
  36. $this->setCell('A3', $newText);
  37. $this->setCell('A4', $instance);
  38. $sheet->getCell('B1')->setValue('=SUBSTITUTE(A1, A2, A3, A4)');
  39. }
  40. $result = $sheet->getCell('B1')->getCalculatedValue();
  41. self::assertEquals($expectedResult, $result);
  42. }
  43. public function providerSUBSTITUTE(): array
  44. {
  45. return require 'tests/data/Calculation/TextData/SUBSTITUTE.php';
  46. }
  47. /**
  48. * @dataProvider providerSubstituteArray
  49. */
  50. public function testSubstituteArray(array $expectedResult, string $oldText, string $fromText, string $toText): void
  51. {
  52. $calculation = Calculation::getInstance();
  53. $formula = "=SUBSTITUTE({$oldText}, {$fromText}, {$toText})";
  54. $result = $calculation->_calculateFormulaValue($formula);
  55. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  56. }
  57. public function providerSubstituteArray(): array
  58. {
  59. return [
  60. 'row vector' => [[['ElePHPant', 'EleFFant']], '"Elephant"', '"ph"', '{"PHP", "FF"}'],
  61. ];
  62. }
  63. }