mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 11:58:15 +00:00
ec4098c8fd
While we might never be able to have 100% of our code strict, we can at the very least do it for all of our tests. This ensures that our tests are using our API with the types as intended by the test author, and not silently be cast to what our API requires.
118 lines
3.6 KiB
PHP
118 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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(),
|
|
],
|
|
];
|
|
}
|
|
}
|