Passed
Pull Request — master (#4180)
by Owen
14:48
created

Issue4179Test::testIssue4179()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 35
rs 9.472
cc 4
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\Shared\File;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
10
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
11
use PHPUnit\Framework\TestCase;
12
use ZipArchive;
13
14
class Issue4179Test extends TestCase
15
{
16
    private string $outputFile = '';
17
18
    protected function tearDown(): void
19
    {
20
        if ($this->outputFile !== '') {
21
            unlink($this->outputFile);
22
            $this->outputFile = '';
23
        }
24
    }
25
26
    public function testIssue4179(): void
27
    {
28
        // duplicate entry in ContentTypes
29
        $spreadsheet = new Spreadsheet();
30
        $sheet = $spreadsheet->getActiveSheet();
31
        $imageFile = 'tests/data/Writer/XLSX/backgroundtest.png';
32
        $image = (string) file_get_contents($imageFile);
33
        $sheet->setBackgroundImage($image);
34
        $drawing = new Drawing();
35
        $drawing->setName('Blue Square');
36
        $drawing->setPath('tests/data/Writer/XLSX/blue_square.png');
37
        $drawing->setCoordinates('A1');
38
        $drawing->setWorksheet($sheet);
39
        $writer = new XlsxWriter($spreadsheet);
40
        $this->outputFile = File::temporaryFilename();
41
        $writer->save($this->outputFile);
42
        $spreadsheet->disconnectWorksheets();
43
44
        $zip = new ZipArchive();
45
        $open = $zip->open($this->outputFile, ZipArchive::RDONLY);
46
        $pngCount = 0;
47
        if ($open !== true) {
48
            self::fail("zip open failed for {$this->outputFile}");
49
        } else {
50
            $contents = (string) $zip->getFromName('[Content_Types].xml');
51
            $subCount = substr_count($contents, '"png"');
52
            self::assertSame(1, $subCount);
53
            for ($i = 0; $i < $zip->numFiles; ++$i) {
54
                $filename = (string) $zip->getNameIndex($i);
55
                if (preg_match('~^xl/media/\\w+[.]png$~', $filename) === 1) {
56
                    ++$pngCount;
57
                }
58
            }
59
        }
60
        self::assertSame(2, $pngCount);
61
    }
62
}
63