ReplaceTest.php 2.5 KB

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