TextJoinTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class TextJoinTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerTEXTJOIN
  8. *
  9. * @param mixed $expectedResult
  10. */
  11. public function testTEXTJOIN($expectedResult, array $args): void
  12. {
  13. $this->mightHaveException($expectedResult);
  14. $sheet = $this->getSheet();
  15. $b1Formula = '=TEXTJOIN(';
  16. $comma = '';
  17. $row = 0;
  18. foreach ($args as $arg) {
  19. ++$row;
  20. $this->setCell("A$row", $arg);
  21. $b1Formula .= $comma . "A$row";
  22. $comma = ',';
  23. }
  24. $b1Formula .= ')';
  25. $this->setCell('B1', $b1Formula);
  26. $result = $sheet->getCell('B1')->getCalculatedValue();
  27. self::assertEquals($expectedResult, $result);
  28. }
  29. public function providerTEXTJOIN(): array
  30. {
  31. return require 'tests/data/Calculation/TextData/TEXTJOIN.php';
  32. }
  33. /**
  34. * @dataProvider providerTextjoinArray
  35. */
  36. public function testTextjoinArray(array $expectedResult, string $delimiter, string $blanks, string $texts): void
  37. {
  38. $calculation = Calculation::getInstance();
  39. $formula = "=TEXTJOIN({$delimiter}, {$blanks}, {$texts})";
  40. $result = $calculation->_calculateFormulaValue($formula);
  41. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  42. }
  43. public function providerTextjoinArray(): array
  44. {
  45. return [
  46. 'row vector #1' => [[['AB,CD,EF', 'AB;CD;EF']], '{",", ";"}', 'FALSE', '"AB", "CD", "EF"'],
  47. 'column vector #1' => [[['AB--CD--EF'], ['AB|CD|EF']], '{"--"; "|"}', 'FALSE', '"AB", "CD", "EF"'],
  48. 'matrix #1' => [[['AB,CD,EF', 'AB;CD;EF'], ['AB-CD-EF', 'AB|CD|EF']], '{",", ";"; "-", "|"}', 'FALSE', '"AB", "CD", "EF"'],
  49. ];
  50. }
  51. }