diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 1699b5a0..1df103ca 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -228,7 +228,7 @@ class Calculation private static $phpSpreadsheetFunctions = [ 'ABS' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'builtinABS'], + 'functionCall' => [MathTrig\Absolute::class, 'evaluate'], 'argumentCount' => '1', ], 'ACCRINT' => [ @@ -835,7 +835,7 @@ class Calculation ], 'DEGREES' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'builtinDEGREES'], + 'functionCall' => [MathTrig\Degrees::class, 'evaluate'], 'argumentCount' => '1', ], 'DELTA' => [ @@ -975,7 +975,7 @@ class Calculation ], 'EXP' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'builtinEXP'], + 'functionCall' => [MathTrig\Exp::class, 'evaluate'], 'argumentCount' => '1', ], 'EXPONDIST' => [ @@ -2038,7 +2038,7 @@ class Calculation ], 'RADIANS' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'builtinRADIANS'], + 'functionCall' => [MathTrig\Radians::class, 'evaluate'], 'argumentCount' => '1', ], 'RAND' => [ @@ -2185,7 +2185,7 @@ class Calculation ], 'SERIESSUM' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'SERIESSUM'], + 'functionCall' => [MathTrig\SeriesSum::class, 'funcSeriesSum'], 'argumentCount' => '4', ], 'SHEET' => [ @@ -2205,7 +2205,7 @@ class Calculation ], 'SIN' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'builtinSIN'], + 'functionCall' => [MathTrig\Sin::class, 'funcSin'], 'argumentCount' => '1', ], 'SINH' => [ @@ -2250,12 +2250,12 @@ class Calculation ], 'SQRT' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'builtinSQRT'], + 'functionCall' => [MathTrig\Sqrt::class, 'evaluate'], 'argumentCount' => '1', ], 'SQRTPI' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'SQRTPI'], + 'functionCall' => [MathTrig\SqrtPi::class, 'evaluate'], 'argumentCount' => '1', ], 'STANDARDIZE' => [ @@ -2331,22 +2331,22 @@ class Calculation ], 'SUMSQ' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'SUMSQ'], + 'functionCall' => [MathTrig\SumSquares::class, 'sumSquare'], 'argumentCount' => '1+', ], 'SUMX2MY2' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'SUMX2MY2'], + 'functionCall' => [MathTrig\SumSquares::class, 'sumXSquaredMinusYSquared'], 'argumentCount' => '2', ], 'SUMX2PY2' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'SUMX2PY2'], + 'functionCall' => [MathTrig\SumSquares::class, 'sumXSquaredPlusYSquared'], 'argumentCount' => '2', ], 'SUMXMY2' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig::class, 'SUMXMY2'], + 'functionCall' => [MathTrig\SumSquares::class, 'sumXMinusYSquared'], 'argumentCount' => '2', ], 'SWITCH' => [ diff --git a/src/PhpSpreadsheet/Calculation/DateTime.php b/src/PhpSpreadsheet/Calculation/DateTime.php index 3b79a6d6..7643ed0b 100644 --- a/src/PhpSpreadsheet/Calculation/DateTime.php +++ b/src/PhpSpreadsheet/Calculation/DateTime.php @@ -23,7 +23,7 @@ class DateTime /** * getDateValue. * - * @Deprecated 2.0.0 Use the method getDateValueNoThrow in the DateTimeExcel\Helpers class instead + * @Deprecated 2.0.0 Use the method getDateValue in the DateTimeExcel\Helpers class instead * * @param mixed $dateValue * @@ -31,7 +31,11 @@ class DateTime */ public static function getDateValue($dateValue) { - return DateTimeExcel\Helpers::getDateValueNoThrow($dateValue); + try { + return DateTimeExcel\Helpers::getDateValue($dateValue); + } catch (Exception $e) { + return $e->getMessage(); + } } /** diff --git a/src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php b/src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php index 48300642..636f0c87 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php @@ -56,22 +56,6 @@ class Helpers return (float) $dateValue; } - /** - * getDateValueNoThrow. - * - * @param mixed $dateValue - * - * @return mixed Excel date/time serial value, or string if error - */ - public static function getDateValueNoThrow($dateValue) - { - try { - return self::getDateValue($dateValue); - } catch (Exception $e) { - return $e->getMessage(); - } - } - /** * getTimeValue. * diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index e960d7ef..7f30edeb 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -640,23 +640,15 @@ class MathTrig * * Returns the square root of (number * pi). * + * @Deprecated 2.0.0 Use the evaluate method in the MathTrig\SqrtPi class instead + * * @param float $number Number * * @return float|string Square Root of Number * Pi, or a string containing an error */ public static function SQRTPI($number) { - $number = Functions::flattenSingleValue($number); - - if (is_numeric($number)) { - if ($number < 0) { - return Functions::NAN(); - } - - return sqrt($number * M_PI); - } - - return Functions::VALUE(); + return MathTrig\SqrtPi::evaluate($number); } /** @@ -769,107 +761,63 @@ class MathTrig * * SUMSQ returns the sum of the squares of the arguments * + * @Deprecated 2.0.0 Use the sumSquare method in the MathTrig\SumSquares class instead + * * Excel Function: * SUMSQ(value1[,value2[, ...]]) * * @param mixed ...$args Data values * - * @return float + * @return float|string */ public static function SUMSQ(...$args) { - $returnValue = 0; - - // Loop through arguments - foreach (Functions::flattenArray($args) as $arg) { - // Is it a numeric value? - if ((is_numeric($arg)) && (!is_string($arg))) { - $returnValue += ($arg * $arg); - } - } - - return $returnValue; + return MathTrig\SumSquares::sumSquare(...$args); } /** * SUMX2MY2. * + * @Deprecated 2.0.0 Use the sumXSquaredMinusYSquared method in the MathTrig\SumSquares class instead + * * @param mixed[] $matrixData1 Matrix #1 * @param mixed[] $matrixData2 Matrix #2 * - * @return float + * @return float|string */ public static function SUMX2MY2($matrixData1, $matrixData2) { - $array1 = Functions::flattenArray($matrixData1); - $array2 = Functions::flattenArray($matrixData2); - $count = min(count($array1), count($array2)); - - $result = 0; - for ($i = 0; $i < $count; ++$i) { - if ( - ((is_numeric($array1[$i])) && (!is_string($array1[$i]))) && - ((is_numeric($array2[$i])) && (!is_string($array2[$i]))) - ) { - $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]); - } - } - - return $result; + return MathTrig\SumSquares::sumXSquaredMinusYSquared($matrixData1, $matrixData2); } /** * SUMX2PY2. * + * @Deprecated 2.0.0 Use the sumXSquaredPlusYSquared method in the MathTrig\SumSquares class instead + * * @param mixed[] $matrixData1 Matrix #1 * @param mixed[] $matrixData2 Matrix #2 * - * @return float + * @return float|string */ public static function SUMX2PY2($matrixData1, $matrixData2) { - $array1 = Functions::flattenArray($matrixData1); - $array2 = Functions::flattenArray($matrixData2); - $count = min(count($array1), count($array2)); - - $result = 0; - for ($i = 0; $i < $count; ++$i) { - if ( - ((is_numeric($array1[$i])) && (!is_string($array1[$i]))) && - ((is_numeric($array2[$i])) && (!is_string($array2[$i]))) - ) { - $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]); - } - } - - return $result; + return MathTrig\SumSquares::sumXSquaredPlusYSquared($matrixData1, $matrixData2); } /** * SUMXMY2. * + * @Deprecated 2.0.0 Use the sumXMinusYSquared method in the MathTrig\SumSquares class instead + * * @param mixed[] $matrixData1 Matrix #1 * @param mixed[] $matrixData2 Matrix #2 * - * @return float + * @return float|string */ public static function SUMXMY2($matrixData1, $matrixData2) { - $array1 = Functions::flattenArray($matrixData1); - $array2 = Functions::flattenArray($matrixData2); - $count = min(count($array1), count($array2)); - - $result = 0; - for ($i = 0; $i < $count; ++$i) { - if ( - ((is_numeric($array1[$i])) && (!is_string($array1[$i]))) && - ((is_numeric($array2[$i])) && (!is_string($array2[$i]))) - ) { - $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]); - } - } - - return $result; + return MathTrig\SumSquares::sumXMinusYSquared($matrixData1, $matrixData2); } /** @@ -1057,19 +1005,15 @@ class MathTrig * * Returns the result of builtin function abs after validating args. * + * @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Absolute class instead + * * @param mixed $number Should be numeric * * @return float|int|string Rounded number */ public static function builtinABS($number) { - $number = Functions::flattenSingleValue($number); - - if (!is_numeric($number)) { - return Functions::VALUE(); - } - - return abs($number); + return MathTrig\Absolute::evaluate($number); } /** @@ -1205,19 +1149,15 @@ class MathTrig * * Returns the result of builtin function rad2deg after validating args. * + * @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Degrees class instead + * * @param mixed $number Should be numeric * * @return float|string Rounded number */ public static function builtinDEGREES($number) { - $number = Functions::flattenSingleValue($number); - - if (!is_numeric($number)) { - return Functions::VALUE(); - } - - return rad2deg($number); + return MathTrig\Degrees::evaluate($number); } /** @@ -1225,19 +1165,15 @@ class MathTrig * * Returns the result of builtin function exp after validating args. * + * @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Exp class instead + * * @param mixed $number Should be numeric * * @return float|string Rounded number */ public static function builtinEXP($number) { - $number = Functions::flattenSingleValue($number); - - if (!is_numeric($number)) { - return Functions::VALUE(); - } - - return exp($number); + return MathTrig\Exp::evaluate($number); } /** @@ -1277,19 +1213,15 @@ class MathTrig * * Returns the result of builtin function deg2rad after validating args. * + * @Deprecated 2.0.0 Use the funcSin method in the MathTrig\Sin class instead + * * @param mixed $number Should be numeric * * @return float|string Rounded number */ public static function builtinRADIANS($number) { - $number = Functions::flattenSingleValue($number); - - if (!is_numeric($number)) { - return Functions::VALUE(); - } - - return deg2rad($number); + return MathTrig\Radians::evaluate($number); } /** @@ -1329,19 +1261,15 @@ class MathTrig * * Returns the result of builtin function sqrt after validating args. * + * @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Sqrt class instead + * * @param mixed $number Should be numeric * * @return float|string Rounded number */ public static function builtinSQRT($number) { - $number = Functions::flattenSingleValue($number); - - if (!is_numeric($number)) { - return Functions::VALUE(); - } - - return self::numberOrNan(sqrt($number)); + return MathTrig\Sqrt::evaluate($number); } /** diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Absolute.php b/src/PhpSpreadsheet/Calculation/MathTrig/Absolute.php new file mode 100644 index 00000000..c2dc579f --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Absolute.php @@ -0,0 +1,28 @@ +getMessage(); + } + + return abs($number); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Degrees.php b/src/PhpSpreadsheet/Calculation/MathTrig/Degrees.php new file mode 100644 index 00000000..501817be --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Degrees.php @@ -0,0 +1,28 @@ +getMessage(); + } + + return rad2deg($number); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Exp.php b/src/PhpSpreadsheet/Calculation/MathTrig/Exp.php new file mode 100644 index 00000000..f3f8af59 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Exp.php @@ -0,0 +1,28 @@ +getMessage(); + } + + return exp($number); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Radians.php b/src/PhpSpreadsheet/Calculation/MathTrig/Radians.php new file mode 100644 index 00000000..15e97011 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Radians.php @@ -0,0 +1,28 @@ +getMessage(); + } + + return deg2rad($number); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php b/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php new file mode 100644 index 00000000..aeb38234 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php @@ -0,0 +1,28 @@ +getMessage(); + } + + return Helpers::numberOrNan(sqrt($number)); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/SqrtPi.php b/src/PhpSpreadsheet/Calculation/MathTrig/SqrtPi.php new file mode 100644 index 00000000..6ff79203 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/SqrtPi.php @@ -0,0 +1,29 @@ +getMessage(); + } + + return sqrt($number * M_PI); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/SumSquares.php b/src/PhpSpreadsheet/Calculation/MathTrig/SumSquares.php new file mode 100644 index 00000000..a750b149 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/SumSquares.php @@ -0,0 +1,142 @@ +getMessage(); + } + + return $returnValue; + } + + private static function getCount(array $array1, array $array2): int + { + $count = count($array1); + if ($count !== count($array2)) { + throw new Exception(Functions::NA()); + } + + return $count; + } + + /** + * These functions accept only numeric arguments, not even strings which are numeric. + * + * @param mixed $item + */ + private static function numericNotString($item): bool + { + return is_numeric($item) && !is_string($item); + } + + /** + * SUMX2MY2. + * + * @param mixed[] $matrixData1 Matrix #1 + * @param mixed[] $matrixData2 Matrix #2 + * + * @return float|string + */ + public static function sumXSquaredMinusYSquared($matrixData1, $matrixData2) + { + try { + $array1 = Functions::flattenArray($matrixData1); + $array2 = Functions::flattenArray($matrixData2); + $count = self::getCount($array1, $array2); + + $result = 0; + for ($i = 0; $i < $count; ++$i) { + if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) { + $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]); + } + } + } catch (Exception $e) { + return $e->getMessage(); + } + + return $result; + } + + /** + * SUMX2PY2. + * + * @param mixed[] $matrixData1 Matrix #1 + * @param mixed[] $matrixData2 Matrix #2 + * + * @return float|string + */ + public static function sumXSquaredPlusYSquared($matrixData1, $matrixData2) + { + try { + $array1 = Functions::flattenArray($matrixData1); + $array2 = Functions::flattenArray($matrixData2); + $count = self::getCount($array1, $array2); + + $result = 0; + for ($i = 0; $i < $count; ++$i) { + if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) { + $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]); + } + } + } catch (Exception $e) { + return $e->getMessage(); + } + + return $result; + } + + /** + * SUMXMY2. + * + * @param mixed[] $matrixData1 Matrix #1 + * @param mixed[] $matrixData2 Matrix #2 + * + * @return float|string + */ + public static function sumXMinusYSquared($matrixData1, $matrixData2) + { + try { + $array1 = Functions::flattenArray($matrixData1); + $array2 = Functions::flattenArray($matrixData2); + $count = self::getCount($array1, $array2); + + $result = 0; + for ($i = 0; $i < $count; ++$i) { + if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) { + $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]); + } + } + } catch (Exception $e) { + return $e->getMessage(); + } + + return $result; + } +} diff --git a/src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php b/src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php index 2b1c7911..48d927f2 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php @@ -17,7 +17,7 @@ class FractionFormatter extends BaseFormatter $decimalLength = strlen($decimalPart); $decimalDivisor = 10 ** $decimalLength; - $GCD = MathTrig::GCD($decimalPart, $decimalDivisor); + $GCD = MathTrig\Gcd::evaluate($decimalPart, $decimalDivisor); $adjustedDecimalPart = $decimalPart / $GCD; $adjustedDecimalDivisor = $decimalDivisor / $GCD; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AbsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AbsTest.php index 49816024..7e74474a 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AbsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AbsTest.php @@ -2,31 +2,26 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PHPUnit\Framework\TestCase; - -class AbsTest extends TestCase +class AbsTest extends AllSetupTeardown { /** * @dataProvider providerAbs * * @param mixed $expectedResult - * @param mixed $val + * @param mixed $number */ - public function testRound($expectedResult, $val = null): void + public function testRound($expectedResult, $number = 'omitted'): void { - if ($val === null) { - $this->expectException(CalcExp::class); - $formula = '=ABS()'; + $sheet = $this->sheet; + $this->mightHaveException($expectedResult); + $this->setCell('A1', $number); + if ($number === 'omitted') { + $sheet->getCell('B1')->setValue('=ABS()'); } else { - $formula = "=ABS($val)"; + $sheet->getCell('B1')->setValue('=ABS(A1)'); } - $spreadsheet = new Spreadsheet(); - $sheet = $spreadsheet->getActiveSheet(); - $sheet->getCell('A1')->setValue($formula); - $result = $sheet->getCell('A1')->getCalculatedValue(); - self::assertEqualsWithDelta($expectedResult, $result, 1E-12); + $result = $sheet->getCell('B1')->getCalculatedValue(); + self::assertSame($expectedResult, $result); } public function providerAbs() diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AllSetupTeardown.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AllSetupTeardown.php index 86c30c22..eef757f4 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AllSetupTeardown.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/AllSetupTeardown.php @@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException; use PhpOffice\PhpSpreadsheet\Calculation\Functions; +use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; @@ -49,4 +50,18 @@ class AllSetupTeardown extends TestCase $this->expectException(CalcException::class); } } + + /** + * @param mixed $value + */ + protected function setCell(string $cell, $value): void + { + if ($value !== null) { + if (is_string($value) && is_numeric($value)) { + $this->sheet->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING); + } else { + $this->sheet->getCell($cell)->setValue($value); + } + } + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/DegreesTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/DegreesTest.php index 3f92703b..d441a943 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/DegreesTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/DegreesTest.php @@ -2,31 +2,26 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PHPUnit\Framework\TestCase; - -class DegreesTest extends TestCase +class DegreesTest extends AllSetupTeardown { /** * @dataProvider providerDEGREES * * @param mixed $expectedResult - * @param mixed $val + * @param mixed $number */ - public function testDEGREES($expectedResult, $val = null): void + public function testDegrees($expectedResult, $number = 'omitted'): void { - if ($val === null) { - $this->expectException(CalcExp::class); - $formula = '=DEGREES()'; + $sheet = $this->sheet; + $this->mightHaveException($expectedResult); + $this->setCell('A1', $number); + if ($number === 'omitted') { + $sheet->getCell('B1')->setValue('=DEGREES()'); } else { - $formula = "=DEGREES($val)"; + $sheet->getCell('B1')->setValue('=DEGREES(A1)'); } - $spreadsheet = new Spreadsheet(); - $sheet = $spreadsheet->getActiveSheet(); - $sheet->getCell('A1')->setValue($formula); - $result = $sheet->getCell('A1')->getCalculatedValue(); - self::assertEqualsWithDelta($expectedResult, $result, 1E-6); + $result = $sheet->getCell('B1')->getCalculatedValue(); + self::assertEqualsWithDelta($expectedResult, $result, 1E-8); } public function providerDegrees() diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ExpTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ExpTest.php index 89bc0097..7e4510fa 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ExpTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ExpTest.php @@ -2,31 +2,28 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PHPUnit\Framework\TestCase; - -class ExpTest extends TestCase +class ExpTest extends AllSetupTeardown { /** * @dataProvider providerEXP * * @param mixed $expectedResult - * @param mixed $val + * @param mixed $number */ - public function testEXP($expectedResult, $val = null): void + public function testEXP($expectedResult, $number = 'omitted'): void { - if ($val === null) { - $this->expectException(CalcExp::class); - $formula = '=EXP()'; - } else { - $formula = "=EXP($val)"; + $this->mightHaveException($expectedResult); + $sheet = $this->sheet; + if ($number !== null) { + $sheet->getCell('A1')->setValue($number); } - $spreadsheet = new Spreadsheet(); - $sheet = $spreadsheet->getActiveSheet(); - $sheet->getCell('A1')->setValue($formula); - $result = $sheet->getCell('A1')->getCalculatedValue(); - self::assertEqualsWithDelta($expectedResult, $result, 1E-6); + if ($number === 'omitted') { + $sheet->getCell('B1')->setValue('=EXP()'); + } else { + $sheet->getCell('B1')->setValue('=EXP(A1)'); + } + $result = $sheet->getCell('B1')->getCalculatedValue(); + self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } public function providerEXP() diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MInverseTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MInverseTest.php index 8831fe83..1912fe07 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MInverseTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MInverseTest.php @@ -11,9 +11,9 @@ class MInverseTest extends AllSetupTeardown * * @param mixed $expectedResult */ - public function testMINVERSE($expectedResult, ...$args): void + public function testMINVERSE($expectedResult, array $args): void { - $result = MathTrig::MINVERSE(...$args); + $result = MathTrig\MatrixFunctions::funcMInverse($args); self::assertEqualsWithDelta($expectedResult, $result, 1E-8); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MMultTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MMultTest.php index 6c40103c..5e0d45f5 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MMultTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MMultTest.php @@ -13,7 +13,7 @@ class MMultTest extends AllSetupTeardown */ public function testMMULT($expectedResult, ...$args): void { - $result = MathTrig::MMULT(...$args); + $result = MathTrig\MatrixFunctions::funcMMult(...$args); self::assertEqualsWithDelta($expectedResult, $result, 1E-8); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MovedFunctionsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MovedFunctionsTest.php index 580092cd..2cca8f36 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MovedFunctionsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/MovedFunctionsTest.php @@ -16,6 +16,7 @@ class MovedFunctionsTest extends TestCase { public function testMovedFunctions(): void { + self::assertSame(1, MathTrig::builtinABS(1)); self::assertEqualsWithDelta(0, MathTrig::builtinACOS(1), 1E-9); self::assertEqualsWithDelta(0, MathTrig::builtinACOSH(1), 1E-9); self::assertEqualsWithDelta(3.04192400109863, MathTrig::ACOT(-10), 1E-9); @@ -35,12 +36,15 @@ class MovedFunctionsTest extends TestCase self::assertEquals('#DIV/0!', MathTrig::COTH(0)); self::assertEquals('#DIV/0!', MathTrig::CSC(0)); self::assertEquals('#DIV/0!', MathTrig::CSCH(0)); + self::assertEquals(0, MathTrig::builtinDEGREES(0)); self::assertEquals(6, MathTrig::EVEN(4.5)); + self::assertEquals(1, MathTrig::builtinEXP(0)); self::assertEquals(6, MathTrig::FACT(3)); self::assertEquals(105, MathTrig::FACTDOUBLE(7)); self::assertEquals(-6, MathTrig::FLOOR(-4.5, 2)); self::assertEquals(0.23, MathTrig::FLOORMATH(0.234, 0.01)); self::assertEquals(-4, MathTrig::FLOORPRECISE(-2.5, 2)); + self::assertEquals(2, MathTrig::GCD(4, 6)); self::assertEquals(-9, MathTrig::INT(-8.3)); self::assertEquals(12, MathTrig::LCM(4, 6)); self::assertEqualswithDelta(2.302585, MathTrig::builtinLN(10), 1E-6); @@ -58,10 +62,12 @@ class MovedFunctionsTest extends TestCase self::assertEquals(1, MathTrig::MOD(5, 2)); self::assertEquals(6, MathTrig::MROUND(7.3, 3)); self::assertEquals(1, MathTrig::MULTINOMIAL(1)); + self::assertEquals(0, MathTrig::numberOrNan(0)); self::assertEquals(5, MathTrig::ODD(4.5)); self::assertEquals(8, MathTrig::POWER(2, 3)); self::assertEquals(8, MathTrig::PRODUCT(1, 2, 4)); self::assertEquals(8, MathTrig::QUOTIENT(17, 2)); + self::assertEquals(0, MathTrig::builtinRADIANS(0)); self::assertGreaterThanOrEqual(0, MATHTRIG::RAND()); self::assertEquals('I', MathTrig::ROMAN(1)); self::assertEquals(3.3, MathTrig::builtinROUND(3.27, 1)); @@ -73,10 +79,23 @@ class MovedFunctionsTest extends TestCase self::assertEquals(1, MathTrig::SIGN(79.2)); self::assertEquals(0, MathTrig::builtinSIN(0)); self::assertEquals(0, MathTrig::builtinSINH(0)); + self::assertEquals(0, MathTrig::builtinSQRT(0)); + self::assertEqualswithDelta(3.54490770181103, MathTrig::SQRTPI(4), 1E-6); self::assertEquals(0, MathTrig::SUBTOTAL(2, [0, 0])); self::assertEquals(7, MathTrig::SUM(1, 2, 4)); self::assertEquals(4, MathTrig::SUMIF([[2], [4]], '>2')); + self::assertEquals(2, MathTrig::SUMIFS( + [[1], [1], [1]], + [['Y'], ['Y'], ['N']], + '=Y', + [['H'], ['H'], ['H']], + '=H' + )); self::assertEquals(17, MathTrig::SUMPRODUCT([1, 2, 3], [5, 0, 4])); + self::assertEquals(21, MathTrig::SUMSQ(1, 2, 4)); + self::assertEquals(-20, MathTrig::SUMX2MY2([1, 2], [3, 4])); + self::assertEquals(30, MathTrig::SUMX2PY2([1, 2], [3, 4])); + self::assertEquals(8, MathTrig::SUMXMY2([1, 2], [3, 4])); self::assertEquals(0, MathTrig::builtinTAN(0)); self::assertEquals(0, MathTrig::builtinTANH(0)); self::assertEquals(70, MathTrig::TRUNC(79.2, -1)); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RadiansTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RadiansTest.php index b5849540..00af620e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RadiansTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RadiansTest.php @@ -2,31 +2,26 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PHPUnit\Framework\TestCase; - -class RadiansTest extends TestCase +class RadiansTest extends AllSetupTeardown { /** * @dataProvider providerRADIANS * * @param mixed $expectedResult - * @param mixed $val + * @param mixed $number */ - public function testRADIANS($expectedResult, $val = null): void + public function testRADIANS($expectedResult, $number = 'omitted'): void { - if ($val === null) { - $this->expectException(CalcExp::class); - $formula = '=RADIANS()'; + $sheet = $this->sheet; + $this->mightHaveException($expectedResult); + $this->setCell('A1', $number); + if ($number === 'omitted') { + $sheet->getCell('B1')->setValue('=RADIANS()'); } else { - $formula = "=RADIANS($val)"; + $sheet->getCell('B1')->setValue('=RADIANS(A1)'); } - $spreadsheet = new Spreadsheet(); - $sheet = $spreadsheet->getActiveSheet(); - $sheet->getCell('A1')->setValue($formula); - $result = $sheet->getCell('A1')->getCalculatedValue(); - self::assertEqualsWithDelta($expectedResult, $result, 1E-6); + $result = $sheet->getCell('B1')->getCalculatedValue(); + self::assertEqualsWithDelta($expectedResult, $result, 1E-9); } public function providerRADIANS() diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtPiTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtPiTest.php index c49934ea..fe130d8c 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtPiTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtPiTest.php @@ -2,26 +2,27 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PHPUnit\Framework\TestCase; - -class SqrtPiTest extends TestCase +class SqrtPiTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSQRTPI * * @param mixed $expectedResult - * @param mixed $value + * @param mixed $number */ - public function testSQRTPI($expectedResult, $value): void + public function testSQRTPI($expectedResult, $number): void { - $result = MathTrig::SQRTPI($value); + $this->mightHaveException($expectedResult); + $sheet = $this->sheet; + if ($number !== null) { + $sheet->getCell('A1')->setValue($number); + } + if ($number === 'omitted') { + $sheet->getCell('B1')->setValue('=SQRTPI()'); + } else { + $sheet->getCell('B1')->setValue('=SQRTPI(A1)'); + } + $result = $sheet->getCell('B1')->getCalculatedValue(); self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtTest.php index 972035e7..9e82fe70 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SqrtTest.php @@ -2,30 +2,25 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PHPUnit\Framework\TestCase; - -class SqrtTest extends TestCase +class SqrtTest extends AllSetupTeardown { /** * @dataProvider providerSQRT * * @param mixed $expectedResult - * @param mixed $val + * @param mixed $number */ - public function testSQRT($expectedResult, $val = null): void + public function testSQRT($expectedResult, $number = 'omitted'): void { - if ($val === null) { - $this->expectException(CalcExp::class); - $formula = '=SQRT()'; + $sheet = $this->sheet; + $this->mightHaveException($expectedResult); + $this->setCell('A1', $number); + if ($number === 'omitted') { + $sheet->getCell('B1')->setValue('=SQRT()'); } else { - $formula = "=SQRT($val)"; + $sheet->getCell('B1')->setValue('=SQRT(A1)'); } - $spreadsheet = new Spreadsheet(); - $sheet = $spreadsheet->getActiveSheet(); - $sheet->getCell('A1')->setValue($formula); - $result = $sheet->getCell('A1')->getCalculatedValue(); + $result = $sheet->getCell('B1')->getCalculatedValue(); self::assertEqualsWithDelta($expectedResult, $result, 1E-6); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfsTest.php index b7be17c9..a4a99888 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfsTest.php @@ -2,17 +2,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PHPUnit\Framework\TestCase; +use PhpOffice\PhpSpreadsheet\Calculation\Statistical; -class SumIfsTest extends TestCase +class SumIfsTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSUMIFS * @@ -20,7 +13,7 @@ class SumIfsTest extends TestCase */ public function testSUMIFS($expectedResult, ...$args): void { - $result = MathTrig::SUMIFS(...$args); + $result = Statistical\Conditional::SUMIFS(...$args); self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumSqTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumSqTest.php index f1165e7b..e811dd75 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumSqTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumSqTest.php @@ -2,17 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PHPUnit\Framework\TestCase; - -class SumSqTest extends TestCase +class SumSqTest extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSUMSQ * @@ -20,7 +11,19 @@ class SumSqTest extends TestCase */ public function testSUMSQ($expectedResult, ...$args): void { - $result = MathTrig::SUMSQ(...$args); + $this->mightHaveException($expectedResult); + $maxRow = 0; + $funcArg = ''; + $sheet = $this->sheet; + foreach ($args as $arg) { + ++$maxRow; + $funcArg = "A1:A$maxRow"; + if ($arg !== null) { + $sheet->getCell("A$maxRow")->setValue($arg); + } + } + $sheet->getCell('B1')->setValue("=SUMSQ($funcArg)"); + $result = $sheet->getCell('B1')->getCalculatedValue(); self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2MY2Test.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2MY2Test.php index 3bf2785b..a6813bb2 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2MY2Test.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2MY2Test.php @@ -3,24 +3,34 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PHPUnit\Framework\TestCase; -class SumX2MY2Test extends TestCase +class SumX2MY2Test extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSUMX2MY2 * * @param mixed $expectedResult */ - public function testSUMX2MY2($expectedResult, ...$args): void + public function testSUMX2MY2($expectedResult, array $matrixData1, array $matrixData2): void { - $result = MathTrig::SUMX2MY2(...$args); + $this->mightHaveException($expectedResult); + $sheet = $this->sheet; + $maxRow = 0; + $funcArg1 = ''; + foreach (Functions::flattenArray($matrixData1) as $arg) { + ++$maxRow; + $funcArg1 = "A1:A$maxRow"; + $this->setCell("A$maxRow", $arg); + } + $maxRow = 0; + $funcArg2 = ''; + foreach (Functions::flattenArray($matrixData2) as $arg) { + ++$maxRow; + $funcArg2 = "C1:C$maxRow"; + $this->setCell("C$maxRow", $arg); + } + $sheet->getCell('B1')->setValue("=SUMX2MY2($funcArg1, $funcArg2)"); + $result = $sheet->getCell('B1')->getCalculatedValue(); self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2PY2Test.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2PY2Test.php index a370d79b..2db78440 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2PY2Test.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumX2PY2Test.php @@ -3,24 +3,34 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PHPUnit\Framework\TestCase; -class SumX2PY2Test extends TestCase +class SumX2PY2Test extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSUMX2PY2 * * @param mixed $expectedResult */ - public function testSUMX2PY2($expectedResult, ...$args): void + public function testSUMX2PY2($expectedResult, array $matrixData1, array $matrixData2): void { - $result = MathTrig::SUMX2PY2(...$args); + $this->mightHaveException($expectedResult); + $sheet = $this->sheet; + $maxRow = 0; + $funcArg1 = ''; + foreach (Functions::flattenArray($matrixData1) as $arg) { + ++$maxRow; + $funcArg1 = "A1:A$maxRow"; + $this->setCell("A$maxRow", $arg); + } + $maxRow = 0; + $funcArg2 = ''; + foreach (Functions::flattenArray($matrixData2) as $arg) { + ++$maxRow; + $funcArg2 = "C1:C$maxRow"; + $this->setCell("C$maxRow", $arg); + } + $sheet->getCell('B1')->setValue("=SUMX2PY2($funcArg1, $funcArg2)"); + $result = $sheet->getCell('B1')->getCalculatedValue(); self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumXMY2Test.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumXMY2Test.php index 1f64523b..eaa1ec7a 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumXMY2Test.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumXMY2Test.php @@ -3,24 +3,34 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; use PhpOffice\PhpSpreadsheet\Calculation\Functions; -use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PHPUnit\Framework\TestCase; -class SumXMY2Test extends TestCase +class SumXMY2Test extends AllSetupTeardown { - protected function setUp(): void - { - Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); - } - /** * @dataProvider providerSUMXMY2 * * @param mixed $expectedResult */ - public function testSUMXMY2($expectedResult, ...$args): void + public function testSUMXMY2($expectedResult, array $matrixData1, array $matrixData2): void { - $result = MathTrig::SUMXMY2(...$args); + $this->mightHaveException($expectedResult); + $sheet = $this->sheet; + $maxRow = 0; + $funcArg1 = ''; + foreach (Functions::flattenArray($matrixData1) as $arg) { + ++$maxRow; + $funcArg1 = "A1:A$maxRow"; + $this->setCell("A$maxRow", $arg); + } + $maxRow = 0; + $funcArg2 = ''; + foreach (Functions::flattenArray($matrixData2) as $arg) { + ++$maxRow; + $funcArg2 = "C1:C$maxRow"; + $this->setCell("C$maxRow", $arg); + } + $sheet->getCell('B1')->setValue("=SUMXMY2($funcArg1, $funcArg2)"); + $result = $sheet->getCell('B1')->getCalculatedValue(); self::assertEqualsWithDelta($expectedResult, $result, 1E-12); } diff --git a/tests/data/Calculation/MathTrig/ABS.php b/tests/data/Calculation/MathTrig/ABS.php index 2fc9631b..4082ceeb 100644 --- a/tests/data/Calculation/MathTrig/ABS.php +++ b/tests/data/Calculation/MathTrig/ABS.php @@ -1,12 +1,18 @@