1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException; |
8
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
9
|
|
|
|
10
|
|
|
class SumIfTest extends AllSetupTeardown |
11
|
|
|
{ |
12
|
|
|
#[DataProvider('providerSUMIF')] |
13
|
|
|
public function testSUMIF2(mixed $expectedResult, array $array1, mixed $condition, ?array $array2 = null): void |
14
|
|
|
{ |
15
|
|
|
$this->mightHaveException($expectedResult); |
16
|
|
|
if ($expectedResult === 'incomplete') { |
17
|
|
|
self::markTestIncomplete('Raises formula error - researching solution'); |
18
|
|
|
} |
19
|
|
|
$this->setArrayAsValue(); |
20
|
|
|
$sheet = $this->getSheet(); |
21
|
|
|
$sheet->fromArray($array1, null, 'A1', true); |
22
|
|
|
$maxARow = count($array1); |
23
|
|
|
$firstArg = "A1:A$maxARow"; |
24
|
|
|
$this->setCell('B1', $condition); |
25
|
|
|
$secondArg = 'B1'; |
26
|
|
|
if (empty($array2)) { |
27
|
|
|
$sheet->getCell('D1')->setValue("=SUMIF($firstArg, $secondArg)"); |
28
|
|
|
} else { |
29
|
|
|
$sheet->fromArray($array2, null, 'C1', true); |
30
|
|
|
$maxCRow = count($array2); |
31
|
|
|
$thirdArg = "C1:C$maxCRow"; |
32
|
|
|
$sheet->getCell('D1')->setValue("=SUMIF($firstArg, $secondArg, $thirdArg)"); |
33
|
|
|
} |
34
|
|
|
$result = $sheet->getCell('D1')->getCalculatedValue(); |
35
|
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function providerSUMIF(): array |
39
|
|
|
{ |
40
|
|
|
return require 'tests/data/Calculation/MathTrig/SUMIF.php'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testOutliers(): void |
44
|
|
|
{ |
45
|
|
|
$sheet = $this->getSheet(); |
46
|
|
|
$sheet->getCell('A1')->setValue('=SUMIF(5,"<32")'); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$sheet->getCell('A1')->getCalculatedValue(); |
50
|
|
|
self::fail('Should receive exception for non-array arg'); |
51
|
|
|
} catch (CalcException $e) { |
52
|
|
|
self::assertStringContainsString('Must specify range of cells', $e->getMessage()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$sheet->getCell('A4')->setValue('=SUMIF(#REF!,"<32")'); |
56
|
|
|
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue()); |
57
|
|
|
$sheet->getCell('A5')->setValue('=SUMIF(D1:D4, 1, #REF!)'); |
58
|
|
|
self::assertSame('#REF!', $sheet->getCell('A5')->getCalculatedValue()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|