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

ColumnOnSpreadsheetTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 39
c 0
b 0
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A providerCOLUMNonSpreadsheet() 0 3 1
A testCOLUMNLocalDefinedName() 0 16 1
A testColumnOnSpreadsheet() 0 21 2
A testCOLUMNSheetWithApostrophe() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
6
7
use PhpOffice\PhpSpreadsheet\NamedRange;
8
use PHPUnit\Framework\Attributes\DataProvider;
9
10
class ColumnOnSpreadsheetTest extends AllSetupTeardown
11
{
12
    #[DataProvider('providerCOLUMNonSpreadsheet')]
13
    public function testColumnOnSpreadsheet(mixed $expectedResult, string $cellReference = 'omitted'): void
14
    {
15
        $this->mightHaveException($expectedResult);
16
        $this->setArrayAsValue();
17
        $sheet = $this->getSheet();
18
        $this->getSpreadsheet()->addNamedRange(new NamedRange('namedrangex', $sheet, '$E$2:$E$6'));
19
        $this->getSpreadsheet()->addNamedRange(new NamedRange('namedrangey', $sheet, '$F$2:$H$2'));
20
        $this->getSpreadsheet()->addNamedRange(new NamedRange('namedrange3', $sheet, '$F$4:$H$4'));
21
22
        $sheet1 = $this->getSpreadsheet()->createSheet();
23
        $sheet1->setTitle('OtherSheet');
24
25
        if ($cellReference === 'omitted') {
26
            $sheet->getCell('B3')->setValue('=COLUMN()');
27
        } else {
28
            $sheet->getCell('B3')->setValue("=COLUMN($cellReference)");
29
        }
30
31
        $result = $sheet->getCell('B3')->getCalculatedValue();
32
        self::assertSame($expectedResult, $result);
33
    }
34
35
    public static function providerCOLUMNonSpreadsheet(): array
36
    {
37
        return require 'tests/data/Calculation/LookupRef/COLUMNonSpreadsheet.php';
38
    }
39
40
    public function testCOLUMNLocalDefinedName(): void
41
    {
42
        $this->setArrayAsValue();
43
        $sheet = $this->getSheet();
44
45
        $sheet1 = $this->getSpreadsheet()->createSheet();
46
        $sheet1->setTitle('OtherSheet');
47
        $this->getSpreadsheet()->addNamedRange(new NamedRange('newnr', $sheet1, '$F$5:$H$5', true)); // defined locally, only usable on sheet1
48
49
        $sheet1->getCell('B3')->setValue('=COLUMN(newnr)');
50
        $result = $sheet1->getCell('B3')->getCalculatedValue();
51
        self::assertSame(6, $result);
52
53
        $sheet->getCell('B3')->setValue('=COLUMN(newnr)');
54
        $result = $sheet->getCell('B3')->getCalculatedValue();
55
        self::assertSame('#NAME?', $result);
56
    }
57
58
    public function testCOLUMNSheetWithApostrophe(): void
59
    {
60
        $this->setArrayAsValue();
61
        $sheet = $this->getSheet();
62
63
        $sheet1 = $this->getSpreadsheet()->createSheet();
64
        $sheet1->setTitle("apo''strophe");
65
        $this->getSpreadsheet()->addNamedRange(new NamedRange('newnr', $sheet1, '$F$5:$H$5', true)); // defined locally, only usable on sheet1
66
67
        $sheet1->getCell('B3')->setValue('=COLUMN(newnr)');
68
        $result = $sheet1->getCell('B3')->getCalculatedValue();
69
        self::assertSame(6, $result);
70
71
        $sheet->getCell('B3')->setValue('=COLUMN(newnr)');
72
        $result = $sheet->getCell('B3')->getCalculatedValue();
73
        self::assertSame('#NAME?', $result);
74
    }
75
}
76