Passed
Pull Request — master (#4212)
by Owen
14:08
created

Issue4200Test   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 52
c 1
b 0
f 0
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testIssue4145() 0 28 2
A testIssue4200() 0 27 2
A tearDown() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\Cell\DataType;
8
use PhpOffice\PhpSpreadsheet\RichText\RichText;
9
use PhpOffice\PhpSpreadsheet\Shared\File;
10
use PhpOffice\PhpSpreadsheet\Spreadsheet;
11
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
12
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
13
use PHPUnit\Framework\TestCase;
14
use ZipArchive;
15
16
class Issue4200Test extends TestCase
17
{
18
    private string $outputFile = '';
19
20
    protected function tearDown(): void
21
    {
22
        if ($this->outputFile !== '') {
23
            unlink($this->outputFile);
24
            $this->outputFile = '';
25
        }
26
    }
27
28
    public function testIssue4200(): void
29
    {
30
        // ignoredErrors came after legacyDrawing
31
        $spreadsheet = new Spreadsheet();
32
        $sheet = $spreadsheet->getActiveSheet();
33
        $sheet->getCell('A1')->setValueExplicit('01', DataType::TYPE_STRING);
34
        $sheet->getCell('A1')
35
            ->getIgnoredErrors()
36
            ->setNumberStoredAsText(true);
37
        $richText = new RichText();
38
        $richText->createText('hello');
39
        $sheet->getComment('C1')
40
            ->setText($richText);
41
        $writer = new XlsxWriter($spreadsheet);
42
        $this->outputFile = File::temporaryFilename();
43
        $writer->save($this->outputFile);
44
        $spreadsheet->disconnectWorksheets();
45
46
        $zip = new ZipArchive();
47
        $open = $zip->open($this->outputFile, ZipArchive::RDONLY);
48
        if ($open !== true) {
49
            self::fail("zip open failed for {$this->outputFile}");
50
        } else {
51
            $contents = (string) $zip->getFromName('xl/worksheets/sheet1.xml');
52
            self::assertStringContainsString(
53
                '<ignoredErrors><ignoredError sqref="A1" numberStoredAsText="1"/></ignoredErrors><legacyDrawing r:id="rId_comments_vml1"/>',
54
                $contents
55
            );
56
        }
57
    }
58
59
    public function testIssue4145(): void
60
    {
61
        // ignoredErrors came after drawing
62
        $spreadsheet = new Spreadsheet();
63
        $sheet = $spreadsheet->getActiveSheet();
64
        $sheet->getCell('A1')->setValueExplicit('01', DataType::TYPE_STRING);
65
        $sheet->getCell('A1')
66
            ->getIgnoredErrors()
67
            ->setNumberStoredAsText(true);
68
        $drawing = new Drawing();
69
        $drawing->setName('Blue Square');
70
        $drawing->setPath('tests/data/Writer/XLSX/blue_square.png');
71
        $drawing->setCoordinates('C1');
72
        $drawing->setWorksheet($sheet);
73
        $writer = new XlsxWriter($spreadsheet);
74
        $this->outputFile = File::temporaryFilename();
75
        $writer->save($this->outputFile);
76
        $spreadsheet->disconnectWorksheets();
77
78
        $zip = new ZipArchive();
79
        $open = $zip->open($this->outputFile, ZipArchive::RDONLY);
80
        if ($open !== true) {
81
            self::fail("zip open failed for {$this->outputFile}");
82
        } else {
83
            $contents = (string) $zip->getFromName('xl/worksheets/sheet1.xml');
84
            self::assertStringContainsString(
85
                '<ignoredErrors><ignoredError sqref="A1" numberStoredAsText="1"/></ignoredErrors><drawing r:id="rId1"/>',
86
                $contents
87
            );
88
        }
89
    }
90
}
91