SeriesSumTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. class SeriesSumTest extends AllSetupTeardown
  6. {
  7. /**
  8. * @dataProvider providerSERIESSUM
  9. *
  10. * @param mixed $expectedResult
  11. * @param mixed $arg1
  12. * @param mixed $arg2
  13. * @param mixed $arg3
  14. */
  15. public function testSERIESSUM($expectedResult, $arg1, $arg2, $arg3, ...$args): void
  16. {
  17. $sheet = $this->getSheet();
  18. if ($arg1 !== null) {
  19. $sheet->getCell('C1')->setValue($arg1);
  20. }
  21. if ($arg2 !== null) {
  22. $sheet->getCell('C2')->setValue($arg2);
  23. }
  24. if ($arg3 !== null) {
  25. $sheet->getCell('C3')->setValue($arg3);
  26. }
  27. $row = 0;
  28. $aArgs = Functions::flattenArray($args);
  29. foreach ($aArgs as $arg) {
  30. ++$row;
  31. if ($arg !== null) {
  32. $sheet->getCell("A$row")->setValue($arg);
  33. }
  34. }
  35. $sheet->getCell('B1')->setValue("=SERIESSUM(C1, C2, C3, A1:A$row)");
  36. $result = $sheet->getCell('B1')->getCalculatedValue();
  37. self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
  38. }
  39. public function providerSERIESSUM(): array
  40. {
  41. return require 'tests/data/Calculation/MathTrig/SERIESSUM.php';
  42. }
  43. /**
  44. * @dataProvider providerSeriesSumArray
  45. */
  46. public function testSeriesSumArray(array $expectedResult, string $x, string $n, string $m, string $values): void
  47. {
  48. $calculation = Calculation::getInstance();
  49. $formula = "=SERIESSUM({$x}, {$n}, {$m}, {$values})";
  50. $result = $calculation->_calculateFormulaValue($formula);
  51. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  52. }
  53. public function providerSeriesSumArray(): array
  54. {
  55. return [
  56. 'row vector #1' => [[[3780, 756]], '5', '{1, 0}', '1', '{1, 1, 0, 1, 1}'],
  57. 'column vector #1' => [[[54], [3780]], '{2; 5}', '1', '1', '{1, 1, 0, 1, 1}'],
  58. 'matrix #1' => [[[54, 27], [3780, 756]], '{2; 5}', '{1, 0}', '1', '{1, 1, 0, 1, 1}'],
  59. ];
  60. }
  61. }