mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 09:48:18 +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.
129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Database\DCount;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
|
class DCountTest extends SetupTeardownDatabases
|
|
{
|
|
/**
|
|
* @dataProvider providerDCount
|
|
*/
|
|
public function testDirectCallToDCount(int|string $expectedResult, array $database, string|int|null $field, array $criteria): void
|
|
{
|
|
$result = DCount::evaluate($database, $field, $criteria);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDCount
|
|
*/
|
|
public function testDCountAsWorksheetFormula(int|string $expectedResult, array $database, string|int|null $field, array $criteria): void
|
|
{
|
|
$this->prepareWorksheetWithFormula('DCOUNT', $database, $field, $criteria);
|
|
|
|
$result = $this->getSheet()->getCell(self::RESULT_CELL)->getCalculatedValue();
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
|
|
}
|
|
|
|
private static function database4(): array
|
|
{
|
|
return [
|
|
['Status', 'Value'],
|
|
[false, 1],
|
|
[true, 2],
|
|
[true, 4],
|
|
[false, 8],
|
|
[true, 16],
|
|
[false, 32],
|
|
[false, 64],
|
|
[false, 128],
|
|
];
|
|
}
|
|
|
|
public static function providerDCount(): array
|
|
{
|
|
return [
|
|
[
|
|
1,
|
|
self::database1(),
|
|
'Age',
|
|
[
|
|
['Tree', 'Height', 'Height'],
|
|
['=Apple', '>10', '<16'],
|
|
],
|
|
],
|
|
[
|
|
1,
|
|
self::database3(),
|
|
'Score',
|
|
[
|
|
['Subject', 'Gender'],
|
|
['Science', 'Male'],
|
|
],
|
|
],
|
|
[
|
|
1,
|
|
self::database3(),
|
|
'Score',
|
|
[
|
|
['Subject', 'Gender'],
|
|
['Math', 'Female'],
|
|
],
|
|
],
|
|
[
|
|
3,
|
|
self::database4(),
|
|
'Value',
|
|
[
|
|
['Status'],
|
|
[true],
|
|
],
|
|
],
|
|
[
|
|
5,
|
|
self::database4(),
|
|
'Value',
|
|
[
|
|
['Status'],
|
|
['<>true'],
|
|
],
|
|
],
|
|
'field column number okay' => [
|
|
0,
|
|
self::database1(),
|
|
1,
|
|
self::database1(),
|
|
],
|
|
'omitted field name' => [
|
|
ExcelError::VALUE(),
|
|
self::database3(),
|
|
null,
|
|
[
|
|
['Subject', 'Score'],
|
|
['English', '>63%'],
|
|
],
|
|
],
|
|
/* 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(),
|
|
],
|
|
];
|
|
}
|
|
}
|