FindTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class FindTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerFIND
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $string1
  11. * @param mixed $string2
  12. * @param mixed $start
  13. */
  14. public function testFIND($expectedResult, $string1 = 'omitted', $string2 = 'omitted', $start = 'omitted'): void
  15. {
  16. $this->mightHaveException($expectedResult);
  17. $sheet = $this->getSheet();
  18. if ($string1 === 'omitted') {
  19. $sheet->getCell('B1')->setValue('=FIND()');
  20. } elseif ($string2 === 'omitted') {
  21. $this->setCell('A1', $string1);
  22. $sheet->getCell('B1')->setValue('=FIND(A1)');
  23. } elseif ($start === 'omitted') {
  24. $this->setCell('A1', $string1);
  25. $this->setCell('A2', $string2);
  26. $sheet->getCell('B1')->setValue('=FIND(A1, A2)');
  27. } else {
  28. $this->setCell('A1', $string1);
  29. $this->setCell('A2', $string2);
  30. $this->setCell('A3', $start);
  31. $sheet->getCell('B1')->setValue('=FIND(A1, A2, A3)');
  32. }
  33. $result = $sheet->getCell('B1')->getCalculatedValue();
  34. self::assertEquals($expectedResult, $result);
  35. }
  36. public function providerFIND(): array
  37. {
  38. return require 'tests/data/Calculation/TextData/FIND.php';
  39. }
  40. /**
  41. * @dataProvider providerFindArray
  42. */
  43. public function testFindArray(array $expectedResult, string $argument1, string $argument2): void
  44. {
  45. $calculation = Calculation::getInstance();
  46. $formula = "=FIND({$argument1}, {$argument2})";
  47. $result = $calculation->_calculateFormulaValue($formula);
  48. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  49. }
  50. public function providerFindArray(): array
  51. {
  52. return [
  53. 'row vector #1' => [[[3, 4, '#VALUE!']], '"l"', '{"Hello", "World", "PhpSpreadsheet"}'],
  54. 'column vector #1' => [[[3], [4], ['#VALUE!']], '"l"', '{"Hello"; "World"; "PhpSpreadsheet"}'],
  55. 'matrix #1' => [[[3, 4], ['#VALUE!', 5]], '"l"', '{"Hello", "World"; "PhpSpreadsheet", "Excel"}'],
  56. ];
  57. }
  58. }