NumberValueTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class NumberValueTest extends AllSetupTeardown
  5. {
  6. const NV_PRECISION = 1.0E-8;
  7. /**
  8. * @dataProvider providerNUMBERVALUE
  9. *
  10. * @param mixed $expectedResult
  11. * @param mixed $number
  12. * @param mixed $decimal
  13. * @param mixed $group
  14. */
  15. public function testNUMBERVALUE($expectedResult, $number = 'omitted', $decimal = 'omitted', $group = 'omitted'): void
  16. {
  17. $this->mightHaveException($expectedResult);
  18. $sheet = $this->getSheet();
  19. if ($number === 'omitted') {
  20. $sheet->getCell('B1')->setValue('=NUMBERVALUE()');
  21. } elseif ($decimal === 'omitted') {
  22. $this->setCell('A1', $number);
  23. $sheet->getCell('B1')->setValue('=NUMBERVALUE(A1)');
  24. } elseif ($group === 'omitted') {
  25. $this->setCell('A1', $number);
  26. $this->setCell('A2', $decimal);
  27. $sheet->getCell('B1')->setValue('=NUMBERVALUE(A1, A2)');
  28. } else {
  29. $this->setCell('A1', $number);
  30. $this->setCell('A2', $decimal);
  31. $this->setCell('A3', $group);
  32. $sheet->getCell('B1')->setValue('=NUMBERVALUE(A1, A2, A3)');
  33. }
  34. $result = $sheet->getCell('B1')->getCalculatedValue();
  35. self::assertEqualsWithDelta($expectedResult, $result, self::NV_PRECISION);
  36. }
  37. public function providerNUMBERVALUE(): array
  38. {
  39. return require 'tests/data/Calculation/TextData/NUMBERVALUE.php';
  40. }
  41. /**
  42. * @dataProvider providerNumberValueArray
  43. */
  44. public function testNumberValueArray(array $expectedResult, string $argument1, string $argument2, string $argument3): void
  45. {
  46. $calculation = Calculation::getInstance();
  47. $formula = "=NumberValue({$argument1}, {$argument2}, {$argument3})";
  48. $result = $calculation->_calculateFormulaValue($formula);
  49. self::assertEqualsWithDelta($expectedResult, $result, self::NV_PRECISION);
  50. }
  51. public function providerNumberValueArray(): array
  52. {
  53. return [
  54. 'row vector #1' => [[[-123.321, 123.456, 12345.6789]], '{"-123,321", "123,456", "12 345,6789"}', '","', '" "'],
  55. 'column vector #1' => [[[-123.321], [123.456], [12345.6789]], '{"-123,321"; "123,456"; "12 345,6789"}', '","', '" "'],
  56. ];
  57. }
  58. }