Passed
Pull Request — master (#4323)
by Owen
17:30 queued 06:07
created

ParserTest::testCellSheetnameQuoted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
6
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
9
use PhpOffice\PhpSpreadsheet\Writer\Xls\Parser;
10
use PHPUnit\Framework\Attributes\DataProvider;
11
use PHPUnit\Framework\TestCase;
12
13
class ParserTest extends TestCase
14
{
15
    private ?Spreadsheet $spreadsheet = null;
16
17
    protected function tearDown(): void
18
    {
19
        if ($this->spreadsheet !== null) {
20
            $this->spreadsheet->disconnectWorksheets();
21
            $this->spreadsheet = null;
22
        }
23
    }
24
25
    public function testNonArray(): void
26
    {
27
        $this->expectException(WriterException::class);
28
        $this->expectExceptionMessage('Unexpected non-array');
29
        $this->spreadsheet = new Spreadsheet();
30
        $parser = new Parser($this->spreadsheet);
31
        $parser->toReversePolish();
32
    }
33
34
    public function testMissingIndex(): void
35
    {
36
        $this->expectException(WriterException::class);
37
        $this->expectExceptionMessage('Unexpected non-array');
38
        $this->spreadsheet = new Spreadsheet();
39
        $parser = new Parser($this->spreadsheet);
40
        $parser->toReversePolish(['left' => 0]);
41
    }
42
43
    public function testParseError(): void
44
    {
45
        $this->expectException(WriterException::class);
46
        $this->expectExceptionMessage('Unknown token +');
47
        $this->spreadsheet = new Spreadsheet();
48
        $parser = new Parser($this->spreadsheet);
49
        $parser->toReversePolish(['left' => 1, 'right' => 2, 'value' => '+']);
50
    }
51
52
    public function testGoodParse(): void
53
    {
54
        $this->spreadsheet = new Spreadsheet();
55
        $parser = new Parser($this->spreadsheet);
56
        self::assertSame('1e01001e02001e0300', bin2hex($parser->toReversePolish(['left' => 1, 'right' => 2, 'value' => 3])));
57
    }
58
59
    #[DataProvider('cellSheetnameQuotedProvider')]
60
    public function testCellSheetnameQuoted(bool $expected, string $address): void
61
    {
62
        self::assertSame($expected, Parser::matchCellSheetnameQuoted($address));
63
    }
64
65
    public static function cellSheetnameQuotedProvider(): array
66
    {
67
        return [
68
            [true, '\'TS GK Mustermann Hans 2\'!$N$1'],
69
            [true, '\'TS GK Mustermann Hans 2\'!N15'],
70
            [true, '\'TS GK Mus\'\'termann Hans 2\'!N15'],
71
            [false, '\'TS GK Mus\'termann Hans 2\'!N15'],
72
            [false, '\'TS GK Mustermann Hans 2\'!N15:P16'],
73
            [false, '\'TS GK Mustermann Hans 2\'!$N$15:$P$16'],
74
            [false, 'sheet1!N15'],
75
            [false, 'sheet1!N15:P16'],
76
            [false, 'N15'],
77
            [false, 'N15:P16'],
78
        ];
79
    }
80
81
    #[DataProvider('rangeSheetnameQuotedProvider')]
82
    public function testRangeSheetnameQuoted(bool $expected, string $address): void
83
    {
84
        self::assertSame($expected, Parser::matchRangeSheetnameQuoted($address));
85
    }
86
87
    public static function rangeSheetnameQuotedProvider(): array
88
    {
89
        return [
90
            [false, '\'TS GK Mustermann Hans 2\'!$N$1'],
91
            [false, '\'TS GK Mustermann Hans 2\'!N15'],
92
            [false, '\'TS GK Mus\'\'termann Hans 2\'!N15'],
93
            [false, '\'TS GK Mus\'termann Hans 2\'!N15'],
94
            [true, '\'TS GK Mustermann Hans 2\'!N15:P16'],
95
            [true, '\'TS GK Mustermann Hans 2\'!$N$15:$P$16'],
96
            [false, 'sheet1!N15'],
97
            [false, 'sheet1!N15:P16'],
98
            [false, 'N15'],
99
            [false, 'N15:P16'],
100
        ];
101
    }
102
}
103