Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/Functions/Database/DAverageTest.php
T
Adrien Crivelli 1b05dfab8b Rector AddParamTypeBasedOnPHPUnitDataProviderRector
And quite a bit more manual changes. The idea is that typing of our
tests can be a bit more loose, so we assume PHPDoc is mostly correct. If
that happens to be wrong, it should be caught by the tests themselves.
2023-09-07 17:00:24 +08:00

116 lines
3.6 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
use PhpOffice\PhpSpreadsheet\Calculation\Database\DAverage;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
class DAverageTest extends SetupTeardownDatabases
{
/**
* @dataProvider providerDAverage
*/
public function testDirectCallToDAverage(int|float|string $expectedResult, array $database, string|int|null $field, array $criteria): void
{
$result = DAverage::evaluate($database, $field, $criteria);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
}
/**
* @dataProvider providerDAverage
*/
public function testDAverageAsWorksheetFormula(int|float|string $expectedResult, array $database, string|int|null $field, array $criteria): void
{
$this->prepareWorksheetWithFormula('DAVERAGE', $database, $field, $criteria);
$result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
}
public static function providerDAverage(): array
{
return [
[
12,
self::database1(),
'Yield',
[
['Tree', 'Height'],
['=Apple', '>10'],
],
],
[
268333.333333333333,
self::database2(),
'Sales',
[
['Quarter', 'Sales Rep.'],
['>1', 'Tina'],
],
],
[
372500,
self::database2(),
'Sales',
[
['Quarter', 'Area'],
['1', 'South'],
],
],
'numeric column, in this case referring to age' => [
13,
self::database1(),
3,
self::database1(),
],
'null field' => [
ExcelError::VALUE(),
self::database1(),
null,
self::database1(),
],
'field unknown column' => [
ExcelError::VALUE(),
self::database1(),
'xyz',
self::database1(),
],
'multiple criteria, omit equal sign' => [
10.5,
self::database1(),
'Yield',
[
['Tree', 'Height'],
['=Apple', '>10'],
['Pear'],
],
],
'multiple criteria for same field' => [
10,
self::database1(),
'Yield',
[
['Tree', 'Height', 'Age', 'Height'],
['=Apple', '>10', null, '<16'],
],
],
/* Excel seems to return #NAME? when column number
is too high or too low. This makes so little sense
to me that I'm not going to bother coding that up,
content to return #VALUE! as an invalid name would */
'field column number too high' => [
ExcelError::VALUE(),
self::database1(),
99,
self::database1(),
],
'field column number too low' => [
ExcelError::VALUE(),
self::database1(),
0,
self::database1(),
],
];
}
}