From a266ade7c676cceb6d632c0f834125452055e23a Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 21 Feb 2021 20:13:18 +0100 Subject: [PATCH 1/5] Refactor the Excel Database functions; and rewrite the query building to fix a bug --- .../Calculation/Calculation.php | 24 +- src/PhpSpreadsheet/Calculation/Database.php | 300 ++++-------------- .../Calculation/Database/DAverage.php | 46 +++ .../Calculation/Database/DCount.php | 53 ++++ .../Calculation/Database/DCountA.php | 49 +++ .../Calculation/Database/DGet.php | 50 +++ .../Calculation/Database/DMax.php | 47 +++ .../Calculation/Database/DMin.php | 47 +++ .../Calculation/Database/DProduct.php | 46 +++ .../Calculation/Database/DStDev.php | 47 +++ .../Calculation/Database/DStDevP.php | 47 +++ .../Calculation/Database/DSum.php | 46 +++ .../Calculation/Database/DVar.php | 47 +++ .../Calculation/Database/DVarP.php | 47 +++ .../Calculation/Database/DatabaseAbstract.php | 142 +++++++++ .../Functions/Database/DAverageTest.php | 60 ++++ .../Functions/Database/DCountATest.php | 54 ++++ .../Functions/Database/DCountTest.php | 54 ++++ .../Functions/Database/DGetTest.php | 65 ++++ .../Functions/Database/DMaxTest.php | 55 ++++ .../Functions/Database/DMinTest.php | 55 ++++ .../Functions/Database/DProductTest.php | 55 ++++ .../Functions/Database/DStDevPTest.php | 55 ++++ .../Functions/Database/DStDevTest.php | 55 ++++ .../Functions/Database/DSumTest.php | 64 ++++ .../Functions/Database/DVarPTest.php | 55 ++++ .../Functions/Database/DVarTest.php | 55 ++++ 27 files changed, 1471 insertions(+), 249 deletions(-) create mode 100644 src/PhpSpreadsheet/Calculation/Database/DAverage.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DCount.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DCountA.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DGet.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DMax.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DMin.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DProduct.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DStDev.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DStDevP.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DSum.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DVar.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DVarP.php create mode 100644 src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 7868ec5ef7..2c98a1683d 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -769,7 +769,7 @@ class Calculation ], 'DAVERAGE' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DAVERAGE'], + 'functionCall' => [Database\DAverage::class, 'evaluate'], 'argumentCount' => '3', ], 'DAY' => [ @@ -799,12 +799,12 @@ class Calculation ], 'DCOUNT' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DCOUNT'], + 'functionCall' => [Database\DCount::class, 'evaluate'], 'argumentCount' => '3', ], 'DCOUNTA' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DCOUNTA'], + 'functionCall' => [Database\DCountA::class, 'evaluate'], 'argumentCount' => '3', ], 'DDB' => [ @@ -849,7 +849,7 @@ class Calculation ], 'DGET' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DGET'], + 'functionCall' => [Database\DGet::class, 'evaluate'], 'argumentCount' => '3', ], 'DISC' => [ @@ -859,12 +859,12 @@ class Calculation ], 'DMAX' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DMAX'], + 'functionCall' => [Database\DMax::class, 'evaluate'], 'argumentCount' => '3', ], 'DMIN' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DMIN'], + 'functionCall' => [Database\DMin::class, 'evaluate'], 'argumentCount' => '3', ], 'DOLLAR' => [ @@ -884,22 +884,22 @@ class Calculation ], 'DPRODUCT' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DPRODUCT'], + 'functionCall' => [Database\DProduct::class, 'evaluate'], 'argumentCount' => '3', ], 'DSTDEV' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DSTDEV'], + 'functionCall' => [Database\DStDev::class, 'evaluate'], 'argumentCount' => '3', ], 'DSTDEVP' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DSTDEVP'], + 'functionCall' => [Database\DStDevP::class, 'evaluate'], 'argumentCount' => '3', ], 'DSUM' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DSUM'], + 'functionCall' => [Database\DSum::class, 'evaluate'], 'argumentCount' => '3', ], 'DURATION' => [ @@ -909,12 +909,12 @@ class Calculation ], 'DVAR' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DVAR'], + 'functionCall' => [Database\DVar::class, 'evaluate'], 'argumentCount' => '3', ], 'DVARP' => [ 'category' => Category::CATEGORY_DATABASE, - 'functionCall' => [Database::class, 'DVARP'], + 'functionCall' => [Database\DVarP::class, 'evaluate'], 'argumentCount' => '3', ], 'EDATE' => [ diff --git a/src/PhpSpreadsheet/Calculation/Database.php b/src/PhpSpreadsheet/Calculation/Database.php index 5250e30693..967a7dd107 100644 --- a/src/PhpSpreadsheet/Calculation/Database.php +++ b/src/PhpSpreadsheet/Calculation/Database.php @@ -2,126 +2,11 @@ namespace PhpOffice\PhpSpreadsheet\Calculation; +/** + * @deprecated 1.17.0 + */ class Database { - /** - * fieldExtract. - * - * Extracts the column ID to use for the data field. - * - * @param mixed[] $database The range of cells that makes up the list or database. - * A database is a list of related data in which rows of related - * information are records, and columns of data are fields. The - * first row of the list contains labels for each column. - * @param mixed $field Indicates which column is used in the function. Enter the - * column label enclosed between double quotation marks, such as - * "Age" or "Yield," or a number (without quotation marks) that - * represents the position of the column within the list: 1 for - * the first column, 2 for the second column, and so on. - * - * @return null|string - */ - private static function fieldExtract($database, $field) - { - $field = strtoupper(Functions::flattenSingleValue($field)); - $fieldNames = array_map('strtoupper', array_shift($database)); - - if (is_numeric($field)) { - $keys = array_keys($fieldNames); - - return $keys[$field - 1]; - } - $key = array_search($field, $fieldNames); - - return $key ?: null; - } - - /** - * filter. - * - * Parses the selection criteria, extracts the database rows that match those criteria, and - * returns that subset of rows. - * - * @param mixed[] $database The range of cells that makes up the list or database. - * A database is a list of related data in which rows of related - * information are records, and columns of data are fields. The - * first row of the list contains labels for each column. - * @param mixed[] $criteria The range of cells that contains the conditions you specify. - * You can use any range for the criteria argument, as long as it - * includes at least one column label and at least one cell below - * the column label in which you specify a condition for the - * column. - * - * @return array of mixed - */ - private static function filter($database, $criteria) - { - $fieldNames = array_shift($database); - $criteriaNames = array_shift($criteria); - - // Convert the criteria into a set of AND/OR conditions with [:placeholders] - $testConditions = $testValues = []; - $testConditionsCount = 0; - foreach ($criteriaNames as $key => $criteriaName) { - $testCondition = []; - $testConditionCount = 0; - foreach ($criteria as $row => $criterion) { - if ($criterion[$key] > '') { - $testCondition[] = '[:' . $criteriaName . ']' . Functions::ifCondition($criterion[$key]); - ++$testConditionCount; - } - } - if ($testConditionCount > 1) { - $testConditions[] = 'OR(' . implode(',', $testCondition) . ')'; - ++$testConditionsCount; - } elseif ($testConditionCount == 1) { - $testConditions[] = $testCondition[0]; - ++$testConditionsCount; - } - } - - if ($testConditionsCount > 1) { - $testConditionSet = 'AND(' . implode(',', $testConditions) . ')'; - } elseif ($testConditionsCount == 1) { - $testConditionSet = $testConditions[0]; - } - - // Loop through each row of the database - foreach ($database as $dataRow => $dataValues) { - // Substitute actual values from the database row for our [:placeholders] - $testConditionList = $testConditionSet; - foreach ($criteriaNames as $key => $criteriaName) { - $k = array_search($criteriaName, $fieldNames); - if (isset($dataValues[$k])) { - $dataValue = $dataValues[$k]; - $dataValue = (is_string($dataValue)) ? Calculation::wrapResult(strtoupper($dataValue)) : $dataValue; - $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList); - } - } - // evaluate the criteria against the row data - $result = Calculation::getInstance()->_calculateFormulaValue('=' . $testConditionList); - // If the row failed to meet the criteria, remove it from the database - if (!$result) { - unset($database[$dataRow]); - } - } - - return $database; - } - - private static function getFilteredColumn($database, $field, $criteria) - { - // reduce the database to a set of rows that match all the criteria - $database = self::filter($database, $criteria); - // extract an array of values for the requested column - $colData = []; - foreach ($database as $row) { - $colData[] = $row[$field]; - } - - return $colData; - } - /** * DAVERAGE. * @@ -130,6 +15,10 @@ private static function getFilteredColumn($database, $field, $criteria) * Excel Function: * DAVERAGE(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DAverage class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -149,15 +38,7 @@ private static function getFilteredColumn($database, $field, $criteria) */ public static function DAVERAGE($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::AVERAGE( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DAverage::evaluate($database, $field, $criteria); } /** @@ -172,6 +53,10 @@ public static function DAVERAGE($database, $field, $criteria) * Excel Function: * DAVERAGE(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DCount class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -194,15 +79,7 @@ public static function DAVERAGE($database, $field, $criteria) */ public static function DCOUNT($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::COUNT( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DCount::evaluate($database, $field, $criteria); } /** @@ -213,6 +90,10 @@ public static function DCOUNT($database, $field, $criteria) * Excel Function: * DCOUNTA(database,[field],criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DCountA class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -235,23 +116,7 @@ public static function DCOUNT($database, $field, $criteria) */ public static function DCOUNTA($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // reduce the database to a set of rows that match all the criteria - $database = self::filter($database, $criteria); - // extract an array of values for the requested column - $colData = []; - foreach ($database as $row) { - $colData[] = $row[$field]; - } - - // Return - return Statistical::COUNTA( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DCountA::evaluate($database, $field, $criteria); } /** @@ -263,6 +128,10 @@ public static function DCOUNTA($database, $field, $criteria) * Excel Function: * DGET(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DGet class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -282,18 +151,7 @@ public static function DCOUNTA($database, $field, $criteria) */ public static function DGET($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - $colData = self::getFilteredColumn($database, $field, $criteria); - if (count($colData) > 1) { - return Functions::NAN(); - } - - return $colData[0]; + return Database\DGet::evaluate($database, $field, $criteria); } /** @@ -305,6 +163,10 @@ public static function DGET($database, $field, $criteria) * Excel Function: * DMAX(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DMax class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -324,15 +186,7 @@ public static function DGET($database, $field, $criteria) */ public static function DMAX($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::MAX( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DMax::evaluate($database, $field, $criteria); } /** @@ -344,6 +198,10 @@ public static function DMAX($database, $field, $criteria) * Excel Function: * DMIN(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DMin class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -363,15 +221,7 @@ public static function DMAX($database, $field, $criteria) */ public static function DMIN($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::MIN( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DMin::evaluate($database, $field, $criteria); } /** @@ -382,6 +232,10 @@ public static function DMIN($database, $field, $criteria) * Excel Function: * DPRODUCT(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DProduct class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -401,15 +255,7 @@ public static function DMIN($database, $field, $criteria) */ public static function DPRODUCT($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return MathTrig::PRODUCT( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DProduct::evaluate($database, $field, $criteria); } /** @@ -421,6 +267,10 @@ public static function DPRODUCT($database, $field, $criteria) * Excel Function: * DSTDEV(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DStDev class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -440,15 +290,7 @@ public static function DPRODUCT($database, $field, $criteria) */ public static function DSTDEV($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::STDEV( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DStDev::evaluate($database, $field, $criteria); } /** @@ -460,6 +302,10 @@ public static function DSTDEV($database, $field, $criteria) * Excel Function: * DSTDEVP(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DStDevP class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -479,15 +325,7 @@ public static function DSTDEV($database, $field, $criteria) */ public static function DSTDEVP($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::STDEVP( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DStDevP::evaluate($database, $field, $criteria); } /** @@ -498,6 +336,10 @@ public static function DSTDEVP($database, $field, $criteria) * Excel Function: * DSUM(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DSum class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -517,15 +359,7 @@ public static function DSTDEVP($database, $field, $criteria) */ public static function DSUM($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return MathTrig::SUM( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DSum::evaluate($database, $field, $criteria); } /** @@ -537,6 +371,10 @@ public static function DSUM($database, $field, $criteria) * Excel Function: * DVAR(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DVar class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -556,15 +394,7 @@ public static function DSUM($database, $field, $criteria) */ public static function DVAR($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::VARFunc( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DVar::evaluate($database, $field, $criteria); } /** @@ -576,6 +406,10 @@ public static function DVAR($database, $field, $criteria) * Excel Function: * DVARP(database,field,criteria) * + * @Deprecated 1.17.0 + * + * @see Use the evaluate() method in the Database\DVarP class instead + * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The @@ -595,14 +429,6 @@ public static function DVAR($database, $field, $criteria) */ public static function DVARP($database, $field, $criteria) { - $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - - // Return - return Statistical::VARP( - self::getFilteredColumn($database, $field, $criteria) - ); + return Database\DVarP::evaluate($database, $field, $criteria); } } diff --git a/src/PhpSpreadsheet/Calculation/Database/DAverage.php b/src/PhpSpreadsheet/Calculation/Database/DAverage.php new file mode 100644 index 0000000000..a8ec367453 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/Database/DAverage.php @@ -0,0 +1,46 @@ + 1) { + return Functions::NAN(); + } + + return $columnData[0]; + } +} diff --git a/src/PhpSpreadsheet/Calculation/Database/DMax.php b/src/PhpSpreadsheet/Calculation/Database/DMax.php new file mode 100644 index 0000000000..6e70be890a --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/Database/DMax.php @@ -0,0 +1,47 @@ + $criterion) { + foreach ($criterion as $field => $value) { + $criterionName = $criteriaNames[$field]; + if ($value !== null && $value !== '') { + $condition = '[:' . $criterionName . ']' . Functions::ifCondition($value); + $baseQuery[$key][] = $condition; + } + } + } + + $rowQuery = array_map( + function ($rowValue) { + return (count($rowValue) > 1) ? 'AND(' . implode(',', $rowValue) . ')': $rowValue[0]; + }, + $baseQuery + ); + + return (count($rowQuery) > 1) ? 'OR(' . implode(',', $rowQuery) . ')' : $rowQuery[0]; + } + + /** + * @param array $database + * @param string $query + * @param $criteriaNames + * @param $fieldNames + * @return array + */ + private static function executeQuery(array $database, string $query, $criteriaNames, $fieldNames): array + { + foreach ($database as $dataRow => $dataValues) { + // Substitute actual values from the database row for our [:placeholders] + $testConditionList = $query; + foreach ($criteriaNames as $key => $criteriaName) { + $k = array_search($criteriaName, $fieldNames); + if (isset($dataValues[$k])) { + $dataValue = $dataValues[$k]; + $dataValue = (is_string($dataValue)) ? Calculation::wrapResult(strtoupper($dataValue)) : $dataValue; + $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList); + } + } + + // evaluate the criteria against the row data + $result = Calculation::getInstance()->_calculateFormulaValue('=' . $testConditionList); + // If the row failed to meet the criteria, remove it from the database + + if ($result !== true) { + unset($database[$dataRow]); + } + } + + return $database; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php new file mode 100644 index 0000000000..aac39e977f --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php @@ -0,0 +1,60 @@ +database(), + 'Yield', + [ + ['Tree', 'Height'], + ['=Apple', '>10'], + ], + ], + [ + 13, + $this->database(), + 3, + $this->database(), + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php new file mode 100644 index 0000000000..46f2b8bccf --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php @@ -0,0 +1,54 @@ +database(), + 'Profit', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php new file mode 100644 index 0000000000..9a9dd3d942 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php @@ -0,0 +1,54 @@ +database(), + 'Age', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php new file mode 100644 index 0000000000..e5cedfe06e --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php @@ -0,0 +1,65 @@ +database(), + 'Yield', + [ + ['Tree'], + ['=Apple'], + ['=Pear'] + ], + ], + [ + 10, + $this->database(), + 'Yield', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ['=Pear', '>12', null] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php new file mode 100644 index 0000000000..c43bb73f74 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php @@ -0,0 +1,55 @@ +database(), + 'Profit', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ['=Pear', null, null] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php new file mode 100644 index 0000000000..b55043362c --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php @@ -0,0 +1,55 @@ +database(), + 'Profit', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ['=Pear', '>12', null] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php new file mode 100644 index 0000000000..df69bd3dec --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php @@ -0,0 +1,55 @@ +database(), + 'Yield', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ['=Pear', null, null] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php new file mode 100644 index 0000000000..b8e113ddd6 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php @@ -0,0 +1,55 @@ +database(), + 'Yield', + [ + ['Tree'], + ['=Apple'], + ['=Pear'] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php new file mode 100644 index 0000000000..8cc2acc4d1 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php @@ -0,0 +1,55 @@ +database(), + 'Yield', + [ + ['Tree'], + ['=Apple'], + ['=Pear'] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php new file mode 100644 index 0000000000..2e5570e67e --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php @@ -0,0 +1,64 @@ +database(), + 'Profit', + [ + ['Tree'], + ['=Apple'], + ], + ], + [ + 248, + $this->database(), + 'Profit', + [ + ['Tree', 'Height', 'Height'], + ['=Apple', '>10', '<16'], + ['=Pear', null, null] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php new file mode 100644 index 0000000000..2110485150 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php @@ -0,0 +1,55 @@ +database(), + 'Yield', + [ + ['Tree'], + ['=Apple'], + ['=Pear'] + ], + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php new file mode 100644 index 0000000000..8878bf3b7f --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php @@ -0,0 +1,55 @@ +database(), + 'Yield', + [ + ['Tree'], + ['=Apple'], + ['=Pear'] + ], + ], + ]; + } +} From 1c7e3aab34357085fca80a7f8d427ef8771d5c7b Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 21 Feb 2021 20:37:56 +0100 Subject: [PATCH 2/5] PHPCS Fixes --- .../Calculation/Database/DatabaseAbstract.php | 5 +---- .../Functions/Database/DAverageTest.php | 19 +++++++++++-------- .../Functions/Database/DCountATest.php | 9 ++++++--- .../Functions/Database/DCountTest.php | 9 ++++++--- .../Functions/Database/DGetTest.php | 13 ++++++++----- .../Functions/Database/DMaxTest.php | 11 +++++++---- .../Functions/Database/DMinTest.php | 11 +++++++---- .../Functions/Database/DProductTest.php | 11 +++++++---- .../Functions/Database/DStDevPTest.php | 11 +++++++---- .../Functions/Database/DStDevTest.php | 11 +++++++---- .../Functions/Database/DSumTest.php | 11 +++++++---- .../Functions/Database/DVarPTest.php | 11 +++++++---- .../Functions/Database/DVarTest.php | 11 +++++++---- 13 files changed, 88 insertions(+), 55 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php index 27b5cd57dd..176dd42ce7 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php +++ b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php @@ -99,7 +99,7 @@ private static function buildQuery(array $criteriaNames, array $criteria): strin $rowQuery = array_map( function ($rowValue) { - return (count($rowValue) > 1) ? 'AND(' . implode(',', $rowValue) . ')': $rowValue[0]; + return (count($rowValue) > 1) ? 'AND(' . implode(',', $rowValue) . ')' : $rowValue[0]; }, $baseQuery ); @@ -108,11 +108,8 @@ function ($rowValue) { } /** - * @param array $database - * @param string $query * @param $criteriaNames * @param $fieldNames - * @return array */ private static function executeQuery(array $database, string $query, $criteriaNames, $fieldNames): array { diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php index aac39e977f..303873ac35 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DAverage; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -13,14 +13,17 @@ protected function setUp(): void Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); } - /** - * @dataProvider providerDAverage - * - * @param mixed $expectedResult - */ - public function testDAverage($expectedResult, $database, $field, $criteria) + /** + * @dataProvider providerDAverage + * + * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria + */ + public function testDAverage($expectedResult, $database, $field, $criteria): void { - $result = DAverage::evaluate($database, $field, $criteria); + $result = Database::DAVERAGE($database, $field, $criteria); self::assertSame($expectedResult, $result); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php index 46f2b8bccf..4b1021f7bd 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DCountA; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDCountA * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDCountA($expectedResult, $database, $field, $criteria) + public function testDCountA($expectedResult, $database, $field, $criteria): void { - $result = DCountA::evaluate($database, $field, $criteria); + $result = Database::DCOUNTA($database, $field, $criteria); self::assertSame($expectedResult, $result); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php index 9a9dd3d942..0a678e3371 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DCount; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDCount * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDCount($expectedResult, $database, $field, $criteria) + public function testDCount($expectedResult, $database, $field, $criteria): void { - $result = DCount::evaluate($database, $field, $criteria); + $result = Database::DCOUNT($database, $field, $criteria); self::assertSame($expectedResult, $result); } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php index e5cedfe06e..26af44c226 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DGet; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDGet * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDGet($expectedResult, $database, $field, $criteria) + public function testDGet($expectedResult, $database, $field, $criteria): void { - $result = DGet::evaluate($database, $field, $criteria); + $result = Database::DGET($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDGet() [ ['Tree'], ['=Apple'], - ['=Pear'] + ['=Pear'], ], ], [ @@ -57,7 +60,7 @@ public function providerDGet() [ ['Tree', 'Height', 'Height'], ['=Apple', '>10', '<16'], - ['=Pear', '>12', null] + ['=Pear', '>12', null], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php index c43bb73f74..ca50cf9b3e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DMax; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDMax * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDMax($expectedResult, $database, $field, $criteria) + public function testDMax($expectedResult, $database, $field, $criteria): void { - $result = DMax::evaluate($database, $field, $criteria); + $result = Database::DMAX($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDMax() [ ['Tree', 'Height', 'Height'], ['=Apple', '>10', '<16'], - ['=Pear', null, null] + ['=Pear', null, null], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php index b55043362c..f40b45851a 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DMin; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDMin * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDMin($expectedResult, $database, $field, $criteria) + public function testDMin($expectedResult, $database, $field, $criteria): void { - $result = DMin::evaluate($database, $field, $criteria); + $result = Database::DMIN($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDMin() [ ['Tree', 'Height', 'Height'], ['=Apple', '>10', '<16'], - ['=Pear', '>12', null] + ['=Pear', '>12', null], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php index df69bd3dec..5075301f32 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DProduct; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDProduct * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDProduct($expectedResult, $database, $field, $criteria) + public function testDProduct($expectedResult, $database, $field, $criteria): void { - $result = DProduct::evaluate($database, $field, $criteria); + $result = Database::DPRODUCT($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDProduct() [ ['Tree', 'Height', 'Height'], ['=Apple', '>10', '<16'], - ['=Pear', null, null] + ['=Pear', null, null], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php index b8e113ddd6..0f9d22fb00 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DStDevP; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDStDevP * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDStDevP($expectedResult, $database, $field, $criteria) + public function testDStDevP($expectedResult, $database, $field, $criteria): void { - $result = DStDevP::evaluate($database, $field, $criteria); + $result = Database::DSTDEVP($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDStDevP() [ ['Tree'], ['=Apple'], - ['=Pear'] + ['=Pear'], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php index 8cc2acc4d1..f5aa3eaea6 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DStDev; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDStDev * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDStDev($expectedResult, $database, $field, $criteria) + public function testDStDev($expectedResult, $database, $field, $criteria): void { - $result = DStDev::evaluate($database, $field, $criteria); + $result = Database::DSTDEV($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDStDev() [ ['Tree'], ['=Apple'], - ['=Pear'] + ['=Pear'], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php index 2e5570e67e..8e6e5a6d79 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DSum; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDSum * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDSum($expectedResult, $database, $field, $criteria) + public function testDSum($expectedResult, $database, $field, $criteria): void { - $result = DSum::evaluate($database, $field, $criteria); + $result = Database::DSUM($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -56,7 +59,7 @@ public function providerDSum() [ ['Tree', 'Height', 'Height'], ['=Apple', '>10', '<16'], - ['=Pear', null, null] + ['=Pear', null, null], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php index 2110485150..6741bbc59d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DVarP; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDVarP * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDVarP($expectedResult, $database, $field, $criteria) + public function testDVarP($expectedResult, $database, $field, $criteria): void { - $result = DVarP::evaluate($database, $field, $criteria); + $result = Database::DVARP($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDVarP() [ ['Tree'], ['=Apple'], - ['=Pear'] + ['=Pear'], ], ], ]; diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php index 8878bf3b7f..57b951da88 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; -use PhpOffice\PhpSpreadsheet\Calculation\Database\DVar; +use PhpOffice\PhpSpreadsheet\Calculation\Database; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -17,10 +17,13 @@ protected function setUp(): void * @dataProvider providerDVar * * @param mixed $expectedResult + * @param mixed $database + * @param mixed $field + * @param mixed $criteria */ - public function testDVar($expectedResult, $database, $field, $criteria) + public function testDVar($expectedResult, $database, $field, $criteria): void { - $result = DVar::evaluate($database, $field, $criteria); + $result = Database::DVAR($database, $field, $criteria); self::assertSame($expectedResult, $result); } @@ -47,7 +50,7 @@ public function providerDVar() [ ['Tree'], ['=Apple'], - ['=Pear'] + ['=Pear'], ], ], ]; From 42c6f57bcc8299556275ceaeb8af5d2564180636 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 21 Feb 2021 20:55:39 +0100 Subject: [PATCH 3/5] Additional test cases for null field value --- .../Calculation/Functions/Database/DAverageTest.php | 6 ++++++ .../Calculation/Functions/Database/DCountATest.php | 6 ++++++ .../Calculation/Functions/Database/DCountTest.php | 6 ++++++ .../Calculation/Functions/Database/DGetTest.php | 6 ++++++ .../Calculation/Functions/Database/DMaxTest.php | 6 ++++++ .../Calculation/Functions/Database/DMinTest.php | 6 ++++++ .../Calculation/Functions/Database/DProductTest.php | 6 ++++++ .../Calculation/Functions/Database/DStDevPTest.php | 6 ++++++ .../Calculation/Functions/Database/DStDevTest.php | 6 ++++++ .../Calculation/Functions/Database/DSumTest.php | 6 ++++++ .../Calculation/Functions/Database/DVarPTest.php | 6 ++++++ .../Calculation/Functions/Database/DVarTest.php | 6 ++++++ 12 files changed, 72 insertions(+) diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php index 303873ac35..42651cc920 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php @@ -58,6 +58,12 @@ public function providerDAverage() 3, $this->database(), ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php index 4b1021f7bd..a16e232845 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php @@ -52,6 +52,12 @@ public function providerDCountA() ['=Apple', '>10', '<16'], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php index 0a678e3371..93b308b58f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php @@ -52,6 +52,12 @@ public function providerDCount() ['=Apple', '>10', '<16'], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php index 26af44c226..edfe343239 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php @@ -63,6 +63,12 @@ public function providerDGet() ['=Pear', '>12', null], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php index ca50cf9b3e..99dc84ed4a 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php @@ -53,6 +53,12 @@ public function providerDMax() ['=Pear', null, null], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php index f40b45851a..76447f2f3e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php @@ -53,6 +53,12 @@ public function providerDMin() ['=Pear', '>12', null], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php index 5075301f32..dd5377ec63 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php @@ -53,6 +53,12 @@ public function providerDProduct() ['=Pear', null, null], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php index 0f9d22fb00..1f2bc483f6 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php @@ -53,6 +53,12 @@ public function providerDStDevP() ['=Pear'], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php index f5aa3eaea6..bfdbccb972 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php @@ -53,6 +53,12 @@ public function providerDStDev() ['=Pear'], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php index 8e6e5a6d79..68f5e50752 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php @@ -62,6 +62,12 @@ public function providerDSum() ['=Pear', null, null], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php index 6741bbc59d..58e3950de5 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php @@ -53,6 +53,12 @@ public function providerDVarP() ['=Pear'], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php index 57b951da88..4bc869311d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php @@ -53,6 +53,12 @@ public function providerDVar() ['=Pear'], ], ], + [ + null, + $this->database(), + null, + $this->database(), + ], ]; } } From 0d9a0f11ac06531f45575b9f2c60a8410b311123 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 21 Feb 2021 21:31:58 +0100 Subject: [PATCH 4/5] A little bit of type hinting --- .../Calculation/Database/DatabaseAbstract.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php index 176dd42ce7..e40b3937bd 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php +++ b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php @@ -23,10 +23,8 @@ abstract public static function evaluate($database, $field, $criteria); * "Age" or "Yield," or a number (without quotation marks) that * represents the position of the column within the list: 1 for * the first column, 2 for the second column, and so on. - * - * @return null|string */ - protected static function fieldExtract($database, $field) + protected static function fieldExtract(array $database, $field): ?string { $field = strtoupper(Functions::flattenSingleValue($field)); $fieldNames = array_map('strtoupper', array_shift($database)); @@ -59,7 +57,7 @@ protected static function fieldExtract($database, $field) * * @return array of mixed */ - protected static function filter($database, $criteria) + protected static function filter(array $database, array $criteria): array { $fieldNames = array_shift($database); $criteriaNames = array_shift($criteria); @@ -71,7 +69,7 @@ protected static function filter($database, $criteria) return self::executeQuery($database, $query, $criteriaNames, $fieldNames); } - protected static function getFilteredColumn($database, $field, $criteria) + protected static function getFilteredColumn(array $database, $field, array $criteria): array { // reduce the database to a set of rows that match all the criteria $database = self::filter($database, $criteria); From 05de1a8493d83353e7d47c7d028c0f83c5613814 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Mon, 22 Feb 2021 12:27:09 +0100 Subject: [PATCH 5/5] Fix handling for empty cells and NULL values in searches Expand unit tests; and add TODOs for dates, percentages, and wildcard text comparisons --- .../Calculation/Calculation.php | 1 + src/PhpSpreadsheet/Calculation/Database.php | 10 +--- .../Calculation/Database/DAverage.php | 1 - .../Calculation/Database/DCount.php | 12 +--- .../Calculation/Database/DCountA.php | 7 --- .../Calculation/Database/DGet.php | 1 - .../Calculation/Database/DMax.php | 1 - .../Calculation/Database/DMin.php | 1 - .../Calculation/Database/DProduct.php | 1 - .../Calculation/Database/DStDev.php | 1 - .../Calculation/Database/DStDevP.php | 1 - .../Calculation/Database/DSum.php | 1 - .../Calculation/Database/DVar.php | 1 - .../Calculation/Database/DVarP.php | 1 - .../Calculation/Database/DatabaseAbstract.php | 17 ++++-- .../Functions/Database/DAverageTest.php | 53 +++++++++++++++-- .../Functions/Database/DCountATest.php | 57 +++++++++++++++++-- .../Functions/Database/DCountTest.php | 50 ++++++++++++++-- .../Functions/Database/DGetTest.php | 51 +++++++++++++++-- .../Functions/Database/DMaxTest.php | 49 ++++++++++++++-- .../Functions/Database/DMinTest.php | 45 +++++++++++++-- .../Functions/Database/DProductTest.php | 49 ++++++++++++++-- .../Functions/Database/DStDevPTest.php | 45 +++++++++++++-- .../Functions/Database/DStDevTest.php | 45 +++++++++++++-- .../Functions/Database/DSumTest.php | 54 ++++++++++++++++-- .../Functions/Database/DVarPTest.php | 45 +++++++++++++-- .../Functions/Database/DVarTest.php | 45 +++++++++++++-- 27 files changed, 549 insertions(+), 96 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 2c98a1683d..87bf44a516 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -3437,6 +3437,7 @@ public function _calculateFormulaValue($formula, $cellID = null, ?Cell $pCell = $this->debugLog->writeDebugLog('Formula for cell ', $wsCellReference, ' is ', $formula); // Parse the formula onto the token stack and calculate the value $this->cyclicReferenceStack->push($wsCellReference); + $cellValue = $this->processTokenStack($this->internalParseFormula($formula, $pCell), $cellID, $pCell); $this->cyclicReferenceStack->pop(); diff --git a/src/PhpSpreadsheet/Calculation/Database.php b/src/PhpSpreadsheet/Calculation/Database.php index 967a7dd107..db16e2df8f 100644 --- a/src/PhpSpreadsheet/Calculation/Database.php +++ b/src/PhpSpreadsheet/Calculation/Database.php @@ -50,9 +50,6 @@ public static function DAVERAGE($database, $field, $criteria) * Excel Function: * DCOUNT(database,[field],criteria) * - * Excel Function: - * DAVERAGE(database,field,criteria) - * * @Deprecated 1.17.0 * * @see Use the evaluate() method in the Database\DCount class instead @@ -61,7 +58,7 @@ public static function DAVERAGE($database, $field, $criteria) * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The * first row of the list contains labels for each column. - * @param int|string $field Indicates which column is used in the function. Enter the + * @param null|int|string $field Indicates which column is used in the function. Enter the * column label enclosed between double quotation marks, such as * "Age" or "Yield," or a number (without quotation marks) that * represents the position of the column within the list: 1 for @@ -98,7 +95,7 @@ public static function DCOUNT($database, $field, $criteria) * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The * first row of the list contains labels for each column. - * @param int|string $field Indicates which column is used in the function. Enter the + * @param null|int|string $field Indicates which column is used in the function. Enter the * column label enclosed between double quotation marks, such as * "Age" or "Yield," or a number (without quotation marks) that * represents the position of the column within the list: 1 for @@ -110,9 +107,6 @@ public static function DCOUNT($database, $field, $criteria) * column. * * @return int - * - * @TODO The field argument is optional. If field is omitted, DCOUNTA counts all records in the - * database that match the criteria. */ public static function DCOUNTA($database, $field, $criteria) { diff --git a/src/PhpSpreadsheet/Calculation/Database/DAverage.php b/src/PhpSpreadsheet/Calculation/Database/DAverage.php index a8ec367453..899b242669 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DAverage.php +++ b/src/PhpSpreadsheet/Calculation/Database/DAverage.php @@ -38,7 +38,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::AVERAGE( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DCount.php b/src/PhpSpreadsheet/Calculation/Database/DCount.php index 66ed5d670f..da173c8c74 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DCount.php +++ b/src/PhpSpreadsheet/Calculation/Database/DCount.php @@ -15,14 +15,11 @@ class DCount extends DatabaseAbstract * Excel Function: * DCOUNT(database,[field],criteria) * - * Excel Function: - * DAVERAGE(database,field,criteria) - * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related * information are records, and columns of data are fields. The * first row of the list contains labels for each column. - * @param int|string $field Indicates which column is used in the function. Enter the + * @param null|int|string $field Indicates which column is used in the function. Enter the * column label enclosed between double quotation marks, such as * "Age" or "Yield," or a number (without quotation marks) that * represents the position of the column within the list: 1 for @@ -34,18 +31,11 @@ class DCount extends DatabaseAbstract * column. * * @return int - * - * @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the - * database that match the criteria. */ public static function evaluate($database, $field, $criteria) { $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - // Return return Statistical::COUNT( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DCountA.php b/src/PhpSpreadsheet/Calculation/Database/DCountA.php index bfe6eb2a86..5e2ef110f0 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DCountA.php +++ b/src/PhpSpreadsheet/Calculation/Database/DCountA.php @@ -30,18 +30,11 @@ class DCountA extends DatabaseAbstract * column. * * @return int - * - * @TODO The field argument is optional. If field is omitted, DCOUNTA counts all records in the - * database that match the criteria. */ public static function evaluate($database, $field, $criteria) { $field = self::fieldExtract($database, $field); - if ($field === null) { - return null; - } - // Return return Statistical::COUNTA( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DGet.php b/src/PhpSpreadsheet/Calculation/Database/DGet.php index f57ee54158..64858d9d28 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DGet.php +++ b/src/PhpSpreadsheet/Calculation/Database/DGet.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return $columnData = self::getFilteredColumn($database, $field, $criteria); if (count($columnData) > 1) { return Functions::NAN(); diff --git a/src/PhpSpreadsheet/Calculation/Database/DMax.php b/src/PhpSpreadsheet/Calculation/Database/DMax.php index 6e70be890a..8918c54d2e 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DMax.php +++ b/src/PhpSpreadsheet/Calculation/Database/DMax.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::MAX( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DMin.php b/src/PhpSpreadsheet/Calculation/Database/DMin.php index 65934479b9..357c9a9130 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DMin.php +++ b/src/PhpSpreadsheet/Calculation/Database/DMin.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::MIN( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DProduct.php b/src/PhpSpreadsheet/Calculation/Database/DProduct.php index 5684e88572..a3b245f374 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DProduct.php +++ b/src/PhpSpreadsheet/Calculation/Database/DProduct.php @@ -38,7 +38,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return MathTrig::PRODUCT( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DStDev.php b/src/PhpSpreadsheet/Calculation/Database/DStDev.php index 1972be7126..58bb4f7dd2 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DStDev.php +++ b/src/PhpSpreadsheet/Calculation/Database/DStDev.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::STDEV( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DStDevP.php b/src/PhpSpreadsheet/Calculation/Database/DStDevP.php index 77038d1957..0d2e050d2a 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DStDevP.php +++ b/src/PhpSpreadsheet/Calculation/Database/DStDevP.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::STDEVP( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DSum.php b/src/PhpSpreadsheet/Calculation/Database/DSum.php index 9c130595ac..b2fa464a5e 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DSum.php +++ b/src/PhpSpreadsheet/Calculation/Database/DSum.php @@ -38,7 +38,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return MathTrig::SUM( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DVar.php b/src/PhpSpreadsheet/Calculation/Database/DVar.php index 012de03f8c..62c9f6c66a 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DVar.php +++ b/src/PhpSpreadsheet/Calculation/Database/DVar.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::VARFunc( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DVarP.php b/src/PhpSpreadsheet/Calculation/Database/DVarP.php index d8408b6499..3a9744cbef 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DVarP.php +++ b/src/PhpSpreadsheet/Calculation/Database/DVarP.php @@ -39,7 +39,6 @@ public static function evaluate($database, $field, $criteria) return null; } - // Return return Statistical::VARP( self::getFilteredColumn($database, $field, $criteria) ); diff --git a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php index e40b3937bd..22ce0957fb 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php +++ b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php @@ -76,12 +76,17 @@ protected static function getFilteredColumn(array $database, $field, array $crit // extract an array of values for the requested column $columnData = []; foreach ($database as $row) { - $columnData[] = $row[$field]; + $columnData[] = ($field !== null) ? $row[$field] : true; } return $columnData; } + /** + * @TODO Support for Dates (including handling for >, <=, etc) + * @TODO Suport for formatted numerics (e.g. '>12.5%' => '>0.125') + * @TODO Suport for wildcard ? and * in strings (includng escaping) + */ private static function buildQuery(array $criteriaNames, array $criteria): string { $baseQuery = []; @@ -115,12 +120,14 @@ private static function executeQuery(array $database, string $query, $criteriaNa // Substitute actual values from the database row for our [:placeholders] $testConditionList = $query; foreach ($criteriaNames as $key => $criteriaName) { - $k = array_search($criteriaName, $fieldNames); - if (isset($dataValues[$k])) { - $dataValue = $dataValues[$k]; + $key = array_search($criteriaName, $fieldNames, true); + if (isset($dataValues[$key])) { + $dataValue = $dataValues[$key]; $dataValue = (is_string($dataValue)) ? Calculation::wrapResult(strtoupper($dataValue)) : $dataValue; - $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList); + } else { + $dataValue = 'NULL'; } + $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList); } // evaluate the criteria against the row data diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php index 42651cc920..910115045e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php @@ -27,7 +27,7 @@ public function testDAverage($expectedResult, $database, $field, $criteria): voi self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,35 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Quarter', 'Area', 'Sales Rep.', 'Sales'], + [1, 'North', 'Jeff', 223000], + [1, 'North', 'Chris', 125000], + [1, 'South', 'Carol', 456000], + [1, 'South', 'Tina', 289000], + [2, 'North', 'Jeff', 322000], + [2, 'North', 'Chris', 340000], + [2, 'South', 'Carol', 198000], + [2, 'South', 'Tina', 222000], + [3, 'North', 'Jeff', 310000], + [3, 'North', 'Chris', 250000], + [3, 'South', 'Carol', 460000], + [3, 'South', 'Tina', 395000], + [4, 'North', 'Jeff', 261000], + [4, 'North', 'Chris', 389000], + [4, 'South', 'Carol', 305000], + [4, 'South', 'Tina', 188000], + ]; + } + public function providerDAverage() { return [ [ 12, - $this->database(), + $this->database1(), 'Yield', [ ['Tree', 'Height'], @@ -54,15 +77,33 @@ public function providerDAverage() ], [ 13, - $this->database(), + $this->database1(), 3, - $this->database(), + $this->database1(), + ], + [ + 268333.333333333333, + $this->database2(), + 'Sales', + [ + ['Quarter', 'Sales Rep.'], + ['>1', 'Tina'], + ], + ], + [ + 372500, + $this->database2(), + 'Sales', + [ + ['Quarter', 'Area'], + ['1', 'South'], + ], ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php index a16e232845..ea856c57de 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountATest.php @@ -27,7 +27,7 @@ public function testDCountA($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 8, 'Math', 0.63], + ['Amy', 'Female', 8, 'English', 0.78], + ['Amy', 'Female', 8, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 'awaiting'], + ['Sue', 'Female', 9, 'Math', null], + ['Sue', 'Female', 9, 'English', 0.52], + ['Sue', 'Female', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDCountA() { return [ [ 1, - $this->database(), + $this->database1(), 'Profit', [ ['Tree', 'Height', 'Height'], @@ -53,10 +72,36 @@ public function providerDCountA() ], ], [ - null, - $this->database(), - null, - $this->database(), + 2, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Science', 'Male'], + ], + ], + /* + * Null value in datacolumn behaviour for DCOUNTA... will include not include a null value in the count + * if it is an actual cell value; but it will be included if it is a literal... this test case is + * currently passing literals + [ + 1, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Math', 'Female'], + ], + ], + */ + [ + 3, + $this->database2(), + 'Score', + [ + ['Subject', 'Score'], + ['English', '>0.60'], + ], ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php index 93b308b58f..9e12ac96ba 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DCountTest.php @@ -27,7 +27,7 @@ public function testDCount($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 8, 'Math', 0.63], + ['Amy', 'Female', 8, 'English', 0.78], + ['Amy', 'Female', 8, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 'awaiting'], + ['Sue', 'Female', 9, 'Math', null], + ['Sue', 'Female', 9, 'English', 0.52], + ['Sue', 'Female', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDCount() { return [ [ 1, - $this->database(), + $this->database1(), 'Age', [ ['Tree', 'Height', 'Height'], @@ -53,10 +72,31 @@ public function providerDCount() ], ], [ + 1, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Science', 'Male'], + ], + ], + [ + 1, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Math', 'Female'], + ], + ], + [ + 3, + $this->database2(), null, - $this->database(), - null, - $this->database(), + [ + ['Subject', 'Score'], + ['English', '>0.63'], + ], ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php index edfe343239..3d90ff9688 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DGetTest.php @@ -27,7 +27,7 @@ public function testDGet($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,35 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Quarter', 'Area', 'Sales Rep.', 'Sales'], + [1, 'North', 'Jeff', 223000], + [1, 'North', 'Chris', 125000], + [1, 'South', 'Carol', 456000], + [1, 'South', 'Tina', 289000], + [2, 'North', 'Jeff', 322000], + [2, 'North', 'Chris', 340000], + [2, 'South', 'Carol', 198000], + [2, 'South', 'Tina', 222000], + [3, 'North', 'Jeff', 310000], + [3, 'North', 'Chris', 250000], + [3, 'South', 'Carol', 460000], + [3, 'South', 'Tina', 395000], + [4, 'North', 'Jeff', 261000], + [4, 'North', 'Chris', 389000], + [4, 'South', 'Carol', 305000], + [4, 'South', 'Tina', 188000], + ]; + } + public function providerDGet() { return [ [ Functions::NAN(), - $this->database(), + $this->database1(), 'Yield', [ ['Tree'], @@ -55,7 +78,7 @@ public function providerDGet() ], [ 10, - $this->database(), + $this->database1(), 'Yield', [ ['Tree', 'Height', 'Height'], @@ -63,11 +86,29 @@ public function providerDGet() ['=Pear', '>12', null], ], ], + [ + 188000, + $this->database2(), + 'Sales', + [ + ['Sales Rep.', 'Quarter'], + ['Tina', 4], + ], + ], + [ + Functions::NAN(), + $this->database2(), + 'Sales', + [ + ['Area', 'Quarter'], + ['South', 4], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php index 99dc84ed4a..b0ad59a705 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMaxTest.php @@ -27,7 +27,7 @@ public function testDMax($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,35 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Quarter', 'Area', 'Sales Rep.', 'Sales'], + [1, 'North', 'Jeff', 223000], + [1, 'North', 'Chris', 125000], + [1, 'South', 'Carol', 456000], + [1, 'South', 'Tina', 289000], + [2, 'North', 'Jeff', 322000], + [2, 'North', 'Chris', 340000], + [2, 'South', 'Carol', 198000], + [2, 'South', 'Tina', 222000], + [3, 'North', 'Jeff', 310000], + [3, 'North', 'Chris', 250000], + [3, 'South', 'Carol', 460000], + [3, 'South', 'Tina', 395000], + [4, 'North', 'Jeff', 261000], + [4, 'North', 'Chris', 389000], + [4, 'South', 'Carol', 305000], + [4, 'South', 'Tina', 188000], + ]; + } + public function providerDMax() { return [ [ 96, - $this->database(), + $this->database1(), 'Profit', [ ['Tree', 'Height', 'Height'], @@ -53,11 +76,29 @@ public function providerDMax() ['=Pear', null, null], ], ], + [ + 340000, + $this->database2(), + 'Sales', + [ + ['Quarter', 'Area'], + [2, 'North'], + ], + ], + [ + 460000, + $this->database2(), + 'Sales', + [ + ['Sales Rep.', 'Quarter'], + ['Carol', '>1'], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php index 76447f2f3e..bfd2af4c4e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DMinTest.php @@ -27,7 +27,7 @@ public function testDMin($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 8, 'Math', 0.63], + ['Amy', 'Female', 8, 'English', 0.78], + ['Amy', 'Female', 8, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 0.51], + ['Sue', 'Female', 9, 'Math', 0.39], + ['Sue', 'Female', 9, 'English', 0.52], + ['Sue', 'Female', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDMin() { return [ [ 75, - $this->database(), + $this->database1(), 'Profit', [ ['Tree', 'Height', 'Height'], @@ -53,11 +72,29 @@ public function providerDMin() ['=Pear', '>12', null], ], ], + [ + 0.48, + $this->database2(), + 'Score', + [ + ['Subject', 'Age'], + ['Science', '>8'], + ], + ], + [ + 0.55, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Math', 'Male'], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php index dd5377ec63..9b2f92b3d2 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DProductTest.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database; use PhpOffice\PhpSpreadsheet\Calculation\Database; +use PhpOffice\PhpSpreadsheet\Calculation\DateTime; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -27,7 +28,7 @@ public function testDProduct($expectedResult, $database, $field, $criteria): voi self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +41,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Date', 'Test', 'Score'], + ['Gary', DateTime::getDateValue('01-Jan-2017'), 'Test1', 4], + ['Gary', DateTime::getDateValue('01-Jan-2017'), 'Test2', 4], + ['Gary', DateTime::getDateValue('01-Jan-2017'), 'Test3', 3], + ['Gary', DateTime::getDateValue('05-Jan-2017'), 'Test1', 3], + ['Gary', DateTime::getDateValue('05-Jan-2017'), 'Test2', 4], + ['Gary', DateTime::getDateValue('05-Jan-2017'), 'Test3', 3], + ['Kev', DateTime::getDateValue('02-Jan-2017'), 'Test1', 2], + ['Kev', DateTime::getDateValue('02-Jan-2017'), 'Test2', 3], + ['Kev', DateTime::getDateValue('02-Jan-2017'), 'Test3', 5], + ['Kev', DateTime::getDateValue('05-Jan-2017'), 'Test1', 3], + ['Kev', DateTime::getDateValue('05-Jan-2017'), 'Test2', 2], + ['Kev', DateTime::getDateValue('05-Jan-2017'), 'Test3', 5], + ]; + } + public function providerDProduct() { return [ [ 800, - $this->database(), + $this->database1(), 'Yield', [ ['Tree', 'Height', 'Height'], @@ -53,11 +73,32 @@ public function providerDProduct() ['=Pear', null, null], ], ], + /* + * We don't yet support date handling in the search query + [ + 36, + $this->database2(), + 'Score', + [ + ['Name', 'Date'], + ['Gary', '05-Jan-2017'], + ], + ], + [ + 8, + $this->database2(), + 'Score', + [ + ['Test', 'Date'], + ['Test1', '<05-Jan-2017'], + ], + ], + */ [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php index 1f2bc483f6..210325d5c3 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevPTest.php @@ -27,7 +27,7 @@ public function testDStDevP($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 10, 'Math', 0.63], + ['Amy', 'Female', 10, 'English', 0.78], + ['Amy', 'Female', 10, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 0.51], + ['Sam', 'Male', 9, 'Math', 0.39], + ['Sam', 'Male', 9, 'English', 0.52], + ['Sam', 'Male', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDStDevP() { return [ [ 2.653299832284, - $this->database(), + $this->database1(), 'Yield', [ ['Tree'], @@ -53,11 +72,29 @@ public function providerDStDevP() ['=Pear'], ], ], + [ + 0.085244745684, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['English', 'Male'], + ], + ], + [ + 0.160623784042, + $this->database2(), + 'Score', + [ + ['Subject', 'Age'], + ['Math', '>8'], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php index bfdbccb972..71bbcd2a06 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DStDevTest.php @@ -27,7 +27,7 @@ public function testDStDev($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 10, 'Math', 0.63], + ['Amy', 'Female', 10, 'English', 0.78], + ['Amy', 'Female', 10, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 0.51], + ['Sam', 'Male', 9, 'Math', 0.39], + ['Sam', 'Male', 9, 'English', 0.52], + ['Sam', 'Male', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDStDev() { return [ [ 2.966479394838, - $this->database(), + $this->database1(), 'Yield', [ ['Tree'], @@ -53,11 +72,29 @@ public function providerDStDev() ['=Pear'], ], ], + [ + 0.104403065089, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['English', 'Male'], + ], + ], + [ + 0.196723155729, + $this->database2(), + 'Score', + [ + ['Subject', 'Age'], + ['Math', '>8'], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php index 68f5e50752..fbb86bb92f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DSumTest.php @@ -27,7 +27,7 @@ public function testDSum($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,35 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Quarter', 'Area', 'Sales Rep.', 'Sales'], + [1, 'North', 'Jeff', 223000], + [1, 'North', 'Chris', 125000], + [1, 'South', 'Carol', 456000], + [1, 'South', 'Tina', 289000], + [2, 'North', 'Jeff', 322000], + [2, 'North', 'Chris', 340000], + [2, 'South', 'Carol', 198000], + [2, 'South', 'Tina', 222000], + [3, 'North', 'Jeff', 310000], + [3, 'North', 'Chris', 250000], + [3, 'South', 'Carol', 460000], + [3, 'South', 'Tina', 395000], + [4, 'North', 'Jeff', 261000], + [4, 'North', 'Chris', 389000], + [4, 'South', 'Carol', 305000], + [4, 'South', 'Tina', 188000], + ]; + } + public function providerDSum() { return [ [ 225, - $this->database(), + $this->database1(), 'Profit', [ ['Tree'], @@ -54,7 +77,7 @@ public function providerDSum() ], [ 248, - $this->database(), + $this->database1(), 'Profit', [ ['Tree', 'Height', 'Height'], @@ -62,11 +85,32 @@ public function providerDSum() ['=Pear', null, null], ], ], + [ + 1210000, + $this->database2(), + 'Sales', + [ + ['Quarter', 'Area'], + ['>2', 'North'], + ], + ], + /* + * We don't yet support woldcards in text search fields + [ + 710000, + $this->database2(), + 'Sales', + [ + ['Quarter', 'Sales Rep.'], + ['3', 'C*'], + ], + ], + */ [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php index 58e3950de5..0436ea6705 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarPTest.php @@ -27,7 +27,7 @@ public function testDVarP($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 10, 'Math', 0.63], + ['Amy', 'Female', 10, 'English', 0.78], + ['Amy', 'Female', 10, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 0.51], + ['Sam', 'Male', 9, 'Math', 0.39], + ['Sam', 'Male', 9, 'English', 0.52], + ['Sam', 'Male', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDVarP() { return [ [ 7.04, - $this->database(), + $this->database1(), 'Yield', [ ['Tree'], @@ -53,11 +72,29 @@ public function providerDVarP() ['=Pear'], ], ], + [ + 0.025622222222, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Math', 'Male'], + ], + ], + [ + 0.011622222222, + $this->database2(), + 'Score', + [ + ['Subject', 'Age'], + ['Science', '>8'], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php index 4bc869311d..467db2f200 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DVarTest.php @@ -27,7 +27,7 @@ public function testDVar($expectedResult, $database, $field, $criteria): void self::assertSame($expectedResult, $result); } - protected function database() + protected function database1() { return [ ['Tree', 'Height', 'Age', 'Yield', 'Profit'], @@ -40,12 +40,31 @@ protected function database() ]; } + protected function database2() + { + return [ + ['Name', 'Gender', 'Age', 'Subject', 'Score'], + ['Amy', 'Female', 10, 'Math', 0.63], + ['Amy', 'Female', 10, 'English', 0.78], + ['Amy', 'Female', 10, 'Science', 0.39], + ['Bill', 'Male', 8, 'Math', 0.55], + ['Bill', 'Male', 8, 'English', 0.71], + ['Bill', 'Male', 8, 'Science', 0.51], + ['Sam', 'Male', 9, 'Math', 0.39], + ['Sam', 'Male', 9, 'English', 0.52], + ['Sam', 'Male', 9, 'Science', 0.48], + ['Tom', 'Male', 9, 'Math', 0.78], + ['Tom', 'Male', 9, 'English', 0.69], + ['Tom', 'Male', 9, 'Science', 0.65], + ]; + } + public function providerDVar() { return [ [ 8.8, - $this->database(), + $this->database1(), 'Yield', [ ['Tree'], @@ -53,11 +72,29 @@ public function providerDVar() ['=Pear'], ], ], + [ + 0.038433333333, + $this->database2(), + 'Score', + [ + ['Subject', 'Gender'], + ['Math', 'Male'], + ], + ], + [ + 0.017433333333, + $this->database2(), + 'Score', + [ + ['Subject', 'Age'], + ['Science', '>8'], + ], + ], [ null, - $this->database(), + $this->database1(), null, - $this->database(), + $this->database1(), ], ]; }