TrimTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class TrimTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerTRIM
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $character
  11. */
  12. public function testTRIM($expectedResult, $character = 'omitted'): void
  13. {
  14. $this->mightHaveException($expectedResult);
  15. $sheet = $this->getSheet();
  16. if ($character === 'omitted') {
  17. $sheet->getCell('B1')->setValue('=TRIM()');
  18. } else {
  19. $this->setCell('A1', $character);
  20. $sheet->getCell('B1')->setValue('=TRIM(A1)');
  21. }
  22. $result = $sheet->getCell('B1')->getCalculatedValue();
  23. self::assertEquals($expectedResult, $result);
  24. }
  25. public function providerTRIM(): array
  26. {
  27. return require 'tests/data/Calculation/TextData/TRIM.php';
  28. }
  29. /**
  30. * @dataProvider providerTrimArray
  31. */
  32. public function testTrimArray(array $expectedResult, string $array): void
  33. {
  34. $calculation = Calculation::getInstance();
  35. $formula = "=TRIM({$array})";
  36. $result = $calculation->_calculateFormulaValue($formula);
  37. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  38. }
  39. public function providerTrimArray(): array
  40. {
  41. return [
  42. 'row vector' => [[['PHP', 'MS Excel', 'Open/Libre Office']], '{" PHP ", " MS Excel ", " Open/Libre Office "}'],
  43. 'column vector' => [[['PHP'], ['MS Excel'], ['Open/Libre Office']], '{" PHP "; " MS Excel "; " Open/Libre Office "}'],
  44. 'matrix' => [[['PHP', 'MS Excel'], ['PhpSpreadsheet', 'Open/Libre Office']], '{" PHP ", " MS Excel "; " PhpSpreadsheet ", " Open/Libre Office "}'],
  45. ];
  46. }
  47. }