Passed
Push — master ( bd792e...df3a06 )
by
unknown
15:51 queued 08:04
created

TorowTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 35
dl 0
loc 47
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A providerTorow() 0 12 1
B testTorow() 0 31 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
6
7
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
8
use PHPUnit\Framework\Attributes\DataProvider;
9
10
class TorowTest extends AllSetupTeardown
11
{
12
    #[DataProvider('providerTorow')]
13
    public function testTorow(mixed $expectedResult, mixed $ignore = 'omitted', mixed $byColumn = 'omitted'): void
14
    {
15
        $this->mightHaveException($expectedResult);
16
        $sheet = $this->getSheet();
17
        $this->setArrayAsArray();
18
        if (is_string($ignore) && $ignore !== 'omitted') {
19
            $ignore = '"' . $ignore . '"';
20
        }
21
        if (is_string($byColumn) && $byColumn !== 'omitted') {
22
            $byColumn = '"' . $byColumn . '"';
23
        }
24
        $ignore = StringHelper::convertToString($ignore);
25
        $byColumn = StringHelper::convertToString($byColumn, convertBool: true);
26
        if ($ignore === 'omitted') {
27
            $formula = '=TOROW(A1:D3)';
28
        } elseif ($byColumn === 'omitted') {
29
            $formula = "=TOROW(A1:D3,$ignore)";
30
        } else {
31
            $formula = "=TOROW(A1:D3,$ignore,$byColumn)";
32
        }
33
34
        $data = [
35
            ['a-one', 'b-one', 'c-one', 'd-one'],
36
            [null, 'b-two', 'c-two', '=2/0'],
37
            [' ', 'b-three', 'c-three', 'd-three'],
38
        ];
39
        $sheet->fromArray($data, null, 'A1', true);
40
        $sheet->setCellValue('A5', $formula);
41
        $result = $sheet->getCell('A5')->getCalculatedValue();
42
        self::assertSame($expectedResult, $result);
43
    }
44
45
    public static function providerTorow(): array
46
    {
47
        return [
48
            'defaults' => [['a-one', 'b-one', 'c-one', 'd-one', 0, 'b-two', 'c-two', '#DIV/0!', ' ', 'b-three', 'c-three', 'd-three']],
49
            'ignore=0' => [['a-one', 'b-one', 'c-one', 'd-one', 0, 'b-two', 'c-two', '#DIV/0!', ' ', 'b-three', 'c-three', 'd-three'], 0],
50
            'ignore=1 supplied as 1.1' => [['a-one', 'b-one', 'c-one', 'd-one', 'b-two', 'c-two', '#DIV/0!', ' ', 'b-three', 'c-three', 'd-three'], 1.1],
51
            'ignore=2' => [['a-one', 'b-one', 'c-one', 'd-one', 0, 'b-two', 'c-two', ' ', 'b-three', 'c-three', 'd-three'], 2],
52
            'ignore=3' => [['a-one', 'b-one', 'c-one', 'd-one', 'b-two', 'c-two', ' ', 'b-three', 'c-three', 'd-three'], 3],
53
            'ignore=-1 invalid' => ['#VALUE!', -1],
54
            'ignore=string invalid' => ['#VALUE!', 'x'],
55
            'by column' => [['a-one', 0, ' ', 'b-one', 'b-two', 'b-three', 'c-one', 'c-two', 'c-three', 'd-one', '#DIV/0!', 'd-three'], 0, true],
56
            'by column using int rather than bool, ignore=1' => [['a-one', ' ', 'b-one', 'b-two', 'b-three', 'c-one', 'c-two', 'c-three', 'd-one', '#DIV/0!', 'd-three'], 1, -15],
57
        ];
58
    }
59
}
60