Passed
Pull Request — master (#4382)
by Owen
13:59
created

CountIfTest::testCOUNTIF()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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