Passed
Pull Request — master (#4418)
by Owen
12:11
created

TextGridTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 104
dl 0
loc 146
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testBool() 0 28 1
B providerTextGrid() 0 80 1
A testTextGrid() 0 29 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Helper;
6
7
use PhpOffice\PhpSpreadsheet\Helper\TextGrid;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PHPUnit\Framework\Attributes\DataProvider;
10
use PHPUnit\Framework\TestCase;
11
12
class TextGridTest extends TestCase
13
{
14
    #[DataProvider('providerTextGrid')]
15
    public function testTextGrid(
16
        bool $cli,
17
        bool $rowDividers,
18
        bool $rowHeaders,
19
        bool $columnHeaders,
20
        array $expected
21
    ): void {
22
        $spreadsheet = new Spreadsheet();
23
        $sheet = $spreadsheet->getActiveSheet();
24
        $sheet->fromArray([
25
            [6, '=TEXT(A1,"yyyy-mm-dd hh:mm")', null, 0.572917],
26
            ['="6"', '=TEXT(A2,"yyyy-mm-dd hh:mm")', null, '1<>2'],
27
            ['xyz', '=TEXT(A3,"yyyy-mm-dd hh:mm")'],
28
        ], strictNullComparison: true);
29
        $textGrid = new TextGrid(
30
            $sheet->toArray(null, true, true, true),
31
            $cli,
32
            rowDividers: $rowDividers,
33
            rowHeaders: $rowHeaders,
34
            columnHeaders: $columnHeaders
35
        );
36
        $result = $textGrid->render();
37
        // Note that, for cli, string will end with PHP_EOL,
38
        //    so explode will add an extra null-string element
39
        //    to its array output.
40
        $lines = explode(PHP_EOL, $result);
41
        self::assertSame($expected, $lines);
42
        $spreadsheet->disconnectWorksheets();
43
    }
44
45
    public static function providerTextGrid(): array
46
    {
47
        return [
48
            'cli default values' => [
49
                true, false, true, true,
50
                [
51
                    '    +-----+------------------+---+----------+',
52
                    '    | A   | B                | C | D        |',
53
                    '+---+-----+------------------+---+----------+',
54
                    '| 1 | 6   | 1900-01-06 00:00 |   | 0.572917 |',
55
                    '| 2 | 6   | 1900-01-06 00:00 |   | 1<>2     |',
56
                    '| 3 | xyz | xyz              |   |          |',
57
                    '+---+-----+------------------+---+----------+',
58
                    '',
59
                ],
60
            ],
61
            'html default values' => [
62
                false, false, true, true,
63
                [
64
                    '<pre>',
65
                    '    +-----+------------------+---+----------+',
66
                    '    | A   | B                | C | D        |',
67
                    '+---+-----+------------------+---+----------+',
68
                    '| 1 | 6   | 1900-01-06 00:00 |   | 0.572917 |',
69
                    '| 2 | 6   | 1900-01-06 00:00 |   | 1&lt;&gt;2     |',
70
                    '| 3 | xyz | xyz              |   |          |',
71
                    '+---+-----+------------------+---+----------+',
72
                    '</pre>',
73
                ],
74
            ],
75
            'cli rowDividers' => [
76
                true, true, true, true,
77
                [
78
                    '    +-----+------------------+---+----------+',
79
                    '    | A   | B                | C | D        |',
80
                    '+---+-----+------------------+---+----------+',
81
                    '| 1 | 6   | 1900-01-06 00:00 |   | 0.572917 |',
82
                    '+---+-----+------------------+---+----------+',
83
                    '| 2 | 6   | 1900-01-06 00:00 |   | 1<>2     |',
84
                    '+---+-----+------------------+---+----------+',
85
                    '| 3 | xyz | xyz              |   |          |',
86
                    '+---+-----+------------------+---+----------+',
87
                    '',
88
                ],
89
            ],
90
            'cli no columnHeaders' => [
91
                true, false, true, false,
92
                [
93
                    '+---+-----+------------------+--+----------+',
94
                    '| 1 | 6   | 1900-01-06 00:00 |  | 0.572917 |',
95
                    '| 2 | 6   | 1900-01-06 00:00 |  | 1<>2     |',
96
                    '| 3 | xyz | xyz              |  |          |',
97
                    '+---+-----+------------------+--+----------+',
98
                    '',
99
                ],
100
            ],
101
            'cli no row headers' => [
102
                true, false, false, true,
103
                [
104
                    '+-----+------------------+---+----------+',
105
                    '| A   | B                | C | D        |',
106
                    '+-----+------------------+---+----------+',
107
                    '| 6   | 1900-01-06 00:00 |   | 0.572917 |',
108
                    '| 6   | 1900-01-06 00:00 |   | 1<>2     |',
109
                    '| xyz | xyz              |   |          |',
110
                    '+-----+------------------+---+----------+',
111
                    '',
112
                ],
113
            ],
114
            'cli row dividers, no row nor column headers' => [
115
                true, true, false, false,
116
                [
117
                    '+-----+------------------+--+----------+',
118
                    '| 6   | 1900-01-06 00:00 |  | 0.572917 |',
119
                    '+-----+------------------+--+----------+',
120
                    '| 6   | 1900-01-06 00:00 |  | 1<>2     |',
121
                    '+-----+------------------+--+----------+',
122
                    '| xyz | xyz              |  |          |',
123
                    '+-----+------------------+--+----------+',
124
                    '',
125
                ],
126
            ],
127
        ];
128
    }
129
130
    public function testBool(): void
131
    {
132
        $spreadsheet = new Spreadsheet();
133
        $sheet = $spreadsheet->getActiveSheet();
134
        $sheet->fromArray([
135
            [0, 1],
136
            [true, false],
137
            [true, true],
138
        ], strictNullComparison: true);
139
        $textGrid = new TextGrid(
140
            $sheet->toArray(null, true, false, true),
141
            true,
142
            rowDividers: false,
143
            rowHeaders: false,
144
            columnHeaders: false,
145
        );
146
        $expected = [
147
            '+------+-------+',
148
            '| 0    | 1     |',
149
            '| TRUE | FALSE |',
150
            '| TRUE | TRUE  |',
151
            '+------+-------+',
152
            '',
153
        ];
154
        $result = $textGrid->render();
155
        $lines = explode(PHP_EOL, $result);
156
        self::assertSame($expected, $lines);
157
        $spreadsheet->disconnectWorksheets();
158
    }
159
}
160