Passed
Pull Request — master (#4360)
by Owen
14:10
created

OffsetTest::providerOFFSET()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
6
7
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
8
use PhpOffice\PhpSpreadsheet\NamedRange;
9
use PHPUnit\Framework\Attributes\DataProvider;
10
11
class OffsetTest extends AllSetupTeardown
12
{
13
    #[DataProvider('providerOFFSET')]
14
    public function testOFFSET(mixed $expectedResult, null|string $cellReference = null): void
15
    {
16
        $result = LookupRef\Offset::OFFSET($cellReference);
17
        self::assertSame($expectedResult, $result);
18
    }
19
20
    public static function providerOFFSET(): array
21
    {
22
        return require 'tests/data/Calculation/LookupRef/OFFSET.php';
23
    }
24
25
    public function testOffsetSpreadsheet(): void
26
    {
27
        $sheet = $this->getSheet();
28
        $sheet->getCell('B6')->setValue(4);
29
        $sheet->getCell('B7')->setValue(8);
30
        $sheet->getCell('B8')->setValue(3);
31
        $sheet->getCell('D6')->setValue(10);
32
        $sheet->getCell('D7')->setValue(3);
33
        $sheet->getCell('D8')->setValue(6);
34
35
        $sheet->getCell('A1')->setValue('=OFFSET(D3,3,-2,1,1)');
36
        self::assertSame(4, $sheet->getCell('A1')->getCalculatedValue());
37
        $sheet->getCell('A2')->setValue('=SUM(OFFSET(D3:F5,3,-2, 3, 3))');
38
        self::assertSame(34, $sheet->getCell('A2')->getCalculatedValue());
39
        $sheet->getCell('A3')->setValue('=OFFSET(D3, -3, -3)');
40
        self::assertSame('#REF!', $sheet->getCell('A3')->getCalculatedValue());
41
42
        $sheet->getCell('C1')->setValue(5);
43
        $sheet->getCell('A4')->setValue('=OFFSET(C1, 0, 0, 0, 0)');
44
        self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue());
45
        $sheet->getCell('A5')->setValue('=OFFSET(C1, 0, 0)');
46
        self::assertSame(5, $sheet->getCell('A5')->getCalculatedValue());
47
    }
48
49
    public function testOffsetNamedRange(): void
50
    {
51
        $workSheet = $this->getSheet();
52
        $workSheet->setCellValue('A1', 1);
53
        $workSheet->setCellValue('A2', 2);
54
55
        $this->getSpreadsheet()->addNamedRange(new NamedRange('demo', $workSheet, '=$A$1'));
56
57
        $workSheet->setCellValue('B1', '=demo');
58
        $workSheet->setCellValue('B2', '=OFFSET(demo, 1, 0)');
59
60
        self::assertSame(2, $workSheet->getCell('B2')->getCalculatedValue());
61
    }
62
63
    public function testOffsetNamedRangeApostropheSheet(): void
64
    {
65
        $workSheet = $this->getSheet();
66
        $workSheet->setTitle("apo'strophe");
67
        $workSheet->setCellValue('A1', 1);
68
        $workSheet->setCellValue('A2', 2);
69
70
        $this->getSpreadsheet()->addNamedRange(new NamedRange('demo', $workSheet, '=$A$1'));
71
72
        $workSheet->setCellValue('B1', '=demo');
73
        $workSheet->setCellValue('B2', '=OFFSET(demo, 1, 0)');
74
75
        self::assertSame(2, $workSheet->getCell('B2')->getCalculatedValue());
76
    }
77
}
78