Completed
Push — master ( 644547...7e345a )
by Mark
41s queued 33s
created

testStructuredReferenceHiddenHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
6
use PhpOffice\PhpSpreadsheet\IOFactory;
7
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
8
use PHPUnit\Framework\TestCase;
9
10
class StructuredReferenceFormulaTest extends TestCase
11
{
12
    /**
13
     * @dataProvider structuredReferenceProvider
14
     */
15
    public function testStructuredReferences(float $expectedValue, string $cellAddress): void
16
    {
17
        $inputFileType = 'Xlsx';
18
        $inputFileName = __DIR__ . '/../../data/Calculation/TableFormulae.xlsx';
19
20
        $reader = IOFactory::createReader($inputFileType);
21
        $spreadsheet = $reader->load($inputFileName);
22
23
        $calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue();
24
        self::assertEqualsWithDelta($expectedValue, $calculatedCellValue, 1.0e-14, "Failed calculation for cell {$cellAddress}");
25
    }
26
27
    public function testStructuredReferenceHiddenHeaders(): void
28
    {
29
        $inputFileType = 'Xlsx';
30
        $inputFileName = __DIR__ . '/../../data/Calculation/TableFormulae.xlsx';
31
32
        $reader = IOFactory::createReader($inputFileType);
33
        $spreadsheet = $reader->load($inputFileName);
34
        /** @var Table $table */
35
        $table = $spreadsheet->getActiveSheet()->getTableByName('DeptSales');
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $table is correct as $spreadsheet->getActiveS...ableByName('DeptSales') targeting PhpOffice\PhpSpreadsheet...sheet::getTableByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
37
        $cellAddress = 'G8';
38
        $spreadsheet->getActiveSheet()->getCell($cellAddress)->setValue('=DeptSales[[#Headers][Region]]');
39
        $result = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue();
40
        self::assertSame('Region', $result);
41
42
        $spreadsheet->getCalculationEngine()->flushInstance(); // @phpstan-ignore-line
43
        $table->setShowHeaderRow(false);
44
45
        $result = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue();
46
        self::assertSame(ExcelError::REF(), $result);
47
    }
48
49
    public function structuredReferenceProvider(): array
50
    {
51
        return [
52
            [26.0, 'E2'],
53
            [99.0, 'E3'],
54
            [141.0, 'E4'],
55
            [49.2, 'E5'],
56
            [120.0, 'E6'],
57
            [135.0, 'E7'],
58
            [570.2, 'E8'],
59
            [3970.0, 'C8'],
60
        ];
61
    }
62
}
63