SearchTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class SearchTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerSEARCH
  8. *
  9. * @param mixed $expectedResult
  10. * @param mixed $findText
  11. * @param mixed $withinText
  12. * @param mixed $start
  13. */
  14. public function testSEARCH($expectedResult, $findText = 'omitted', $withinText = 'omitted', $start = 'omitted'): void
  15. {
  16. $this->mightHaveException($expectedResult);
  17. $sheet = $this->getSheet();
  18. if ($findText === 'omitted') {
  19. $sheet->getCell('B1')->setValue('=SEARCH()');
  20. } elseif ($withinText === 'omitted') {
  21. $this->setCell('A1', $findText);
  22. $sheet->getCell('B1')->setValue('=SEARCH(A1)');
  23. } elseif ($start === 'omitted') {
  24. $this->setCell('A1', $findText);
  25. $this->setCell('A2', $withinText);
  26. $sheet->getCell('B1')->setValue('=SEARCH(A1, A2)');
  27. } else {
  28. $this->setCell('A1', $findText);
  29. $this->setCell('A2', $withinText);
  30. $this->setCell('A3', $start);
  31. $sheet->getCell('B1')->setValue('=SEARCH(A1, A2, A3)');
  32. }
  33. $result = $sheet->getCell('B1')->getCalculatedValue();
  34. self::assertEquals($expectedResult, $result);
  35. }
  36. public function providerSEARCH(): array
  37. {
  38. return require 'tests/data/Calculation/TextData/SEARCH.php';
  39. }
  40. /**
  41. * @dataProvider providerSearchArray
  42. */
  43. public function testSearchArray(array $expectedResult, string $argument1, string $argument2): void
  44. {
  45. $calculation = Calculation::getInstance();
  46. $formula = "=SEARCH({$argument1}, {$argument2})";
  47. $result = $calculation->_calculateFormulaValue($formula);
  48. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  49. }
  50. public function providerSearchArray(): 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. }