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

CountIfTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
dl 0
loc 38
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testOutliers() 0 14 2
A testCOUNTIF() 0 4 1
A testMultipleRows() 0 9 1
A providerCOUNTIF() 0 3 1
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