Passed
Push — master ( c91c8c...4b8a92 )
by Owen
21:17
created

testSheetWithBadImageRightOfData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
6
7
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
10
use PhpOffice\PhpSpreadsheet\Writer\Html;
11
use PhpOffice\PhpSpreadsheetTests\Functional;
12
13
class ExtendForChartsAndImagesTest extends Functional\AbstractFunctional
14
{
15
    public function testEmptySheet(): void
16
    {
17
        $spreadsheet = new Spreadsheet();
18
19
        $this->assertMaxColumnAndMaxRow($spreadsheet, 1, 1);
20
    }
21
22
    public function testSimpleSheet(): void
23
    {
24
        $spreadsheet = new Spreadsheet();
25
        $sheet = $spreadsheet->getActiveSheet();
26
        $sheet->setCellValue('B3', 'foo');
27
28
        $this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
29
    }
30
31
    public function testSheetWithExtraColumnDimensions(): void
32
    {
33
        $spreadsheet = new Spreadsheet();
34
        $sheet = $spreadsheet->getActiveSheet();
35
        $sheet->setCellValue('B3', 'foo');
36
37
        // Artificially expend the sheet column count without any real cells
38
        $sheet->getColumnDimension('E');
39
40
        $this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
41
    }
42
43
    public function testSheetWithExtraRowDimensions(): void
44
    {
45
        $spreadsheet = new Spreadsheet();
46
        $sheet = $spreadsheet->getActiveSheet();
47
        $sheet->setCellValue('B3', 'foo');
48
49
        // Artificially expend the sheet row count without any real cells
50
        $sheet->getRowDimension(5);
51
52
        $this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
53
    }
54
55
    public function testSheetWithImageBelowData(): void
56
    {
57
        $spreadsheet = new Spreadsheet();
58
        $sheet = $spreadsheet->getActiveSheet();
59
        $sheet->setCellValue('B3', 'foo');
60
61
        // Add a drawing to the worksheet
62
        $drawing = new Drawing();
63
        $path = 'tests/data/Writer/XLSX/blue_square.png';
64
        $drawing->setPath($path);
65
        self::assertSame($path, $drawing->getPath());
66
        $drawing->setCoordinates('A5');
67
        $drawing->setWorksheet($sheet);
68
69
        $this->assertMaxColumnAndMaxRow($spreadsheet, 2, 5);
70
    }
71
72
    public function testSheetWithImageRightOfData(): void
73
    {
74
        $spreadsheet = new Spreadsheet();
75
        $sheet = $spreadsheet->getActiveSheet();
76
        $sheet->setCellValue('B3', 'foo');
77
78
        // Add a drawing to the worksheet
79
        $drawing = new Drawing();
80
        $path = 'tests/data/Writer/XLSX/blue_square.png';
81
        $drawing->setPath($path);
82
        self::assertSame($path, $drawing->getPath());
83
        $drawing->setCoordinates('E1');
84
        $drawing->setWorksheet($sheet);
85
86
        $this->assertMaxColumnAndMaxRow($spreadsheet, 5, 3);
87
    }
88
89
    public function testSheetWithBadImageRightOfData(): void
90
    {
91
        $spreadsheet = new Spreadsheet();
92
        $sheet = $spreadsheet->getActiveSheet();
93
        $sheet->setCellValue('B3', 'foo');
94
95
        // Add a drawing to the worksheet
96
        $drawing = new Drawing();
97
        $path = 'tests/data/Writer/XLSX/xblue_square.png';
98
        $drawing->setPath($path, false);
99
        self::assertSame('', $drawing->getPath());
100
        $drawing->setCoordinates('E1');
101
        $drawing->setWorksheet($sheet);
102
103
        $this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
104
    }
105
106
    public function testSheetWithBadImageRightOfDataThrow(): void
107
    {
108
        $this->expectException(PhpSpreadsheetException::class);
109
        $this->expectExceptionMessage('not found!');
110
        $spreadsheet = new Spreadsheet();
111
        $sheet = $spreadsheet->getActiveSheet();
112
        $sheet->setCellValue('B3', 'foo');
113
114
        // Add a drawing to the worksheet
115
        $drawing = new Drawing();
116
        $path = 'tests/data/Writer/XLSX/xblue_square.png';
117
        $drawing->setPath($path);
118
        self::assertSame('', $drawing->getPath());
119
        $drawing->setCoordinates('E1');
120
        $drawing->setWorksheet($sheet);
121
122
        $this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
123
    }
124
125
    private function assertMaxColumnAndMaxRow(Spreadsheet $spreadsheet, int $expectedColumnCount, int $expectedRowCount): void
126
    {
127
        $writer = new Html($spreadsheet);
128
        $html = $writer->generateHtmlAll();
129
130
        $rowCount = substr_count($html, '<tr ');
131
        self::assertSame($expectedRowCount, $rowCount);
132
133
        $columnCount = substr_count($html, '<td ') / $rowCount;
134
135
        self::assertSame($expectedColumnCount, $columnCount);
136
    }
137
}
138