1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException; |
8
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
9
|
|
|
|
10
|
|
|
class CountIfTest extends AllSetupTeardown |
11
|
|
|
{ |
12
|
|
|
#[DataProvider('providerCOUNTIF')] |
13
|
|
|
public function testCOUNTIF(mixed $expectedResult, mixed ...$args): void |
14
|
|
|
{ |
15
|
|
|
$this->runTestCaseNoBracket('COUNTIF', $expectedResult, ...$args); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testMultipleRows(): void |
19
|
|
|
{ |
20
|
|
|
$sheet = $this->getSheet(); |
21
|
|
|
$sheet->fromArray([ |
22
|
|
|
['apples', 'oranges', 'peaches', 'apples'], |
23
|
|
|
['bananas', 'mangoes', 'grapes', 'cherries'], |
24
|
|
|
]); |
25
|
|
|
$sheet->getCell('Z99')->setValue('=COUNTIF(A1:D2,"*p*e*")'); |
26
|
|
|
self::assertSame(4, $sheet->getCell('Z99')->getCalculatedValue()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function providerCOUNTIF(): array |
30
|
|
|
{ |
31
|
|
|
return require 'tests/data/Calculation/Statistical/COUNTIF.php'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testOutliers(): void |
35
|
|
|
{ |
36
|
|
|
$sheet = $this->getSheet(); |
37
|
|
|
$sheet->getCell('A1')->setValue('=COUNTIF(5,"<32")'); |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
$sheet->getCell('A1')->getCalculatedValue(); |
41
|
|
|
self::fail('Should receive exception for non-array arg'); |
42
|
|
|
} catch (CalcException $e) { |
43
|
|
|
self::assertStringContainsString('Must specify range of cells', $e->getMessage()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$sheet->getCell('A4')->setValue('=COUNTIF(#REF!,1)'); |
47
|
|
|
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|