Passed
Pull Request — master (#4142)
by Owen
20:09
created

URLImageTest::testURLImageSourceBadProtocol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
8
use PhpOffice\PhpSpreadsheet\IOFactory;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
10
use PhpOffice\PhpSpreadsheetTests\Reader\Utility\File;
11
use PHPUnit\Framework\TestCase;
12
13
class URLImageTest extends TestCase
14
{
15
    public function testURLImageSource(): void
16
    {
17
        if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
18
            self::markTestSkipped('Skipped due to setting of environment variable');
19
        }
20
        $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.xlsx');
21
        self::assertNotFalse($filename);
22
        $reader = IOFactory::createReader('Xlsx');
23
        $spreadsheet = $reader->load($filename);
24
        $worksheet = $spreadsheet->getActiveSheet();
25
        $collection = $worksheet->getDrawingCollection();
26
        self::assertCount(1, $collection);
27
28
        foreach ($collection as $drawing) {
29
            self::assertInstanceOf(Drawing::class, $drawing);
30
            // Check if the source is a URL or a file path
31
            self::assertTrue($drawing->getIsURL());
32
            self::assertSame('https://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png', $drawing->getPath());
33
            $imageContents = file_get_contents($drawing->getPath());
34
            self::assertNotFalse($imageContents);
35
            $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
36
            self::assertNotFalse($filePath);
37
            self::assertNotFalse(file_put_contents($filePath, $imageContents));
38
            $mimeType = mime_content_type($filePath);
39
            unlink($filePath);
40
            self::assertNotFalse($mimeType);
41
            $extension = File::mime2ext($mimeType);
42
            self::assertSame('png', $extension);
43
        }
44
    }
45
46
    public function testURLImageSourceBadProtocol(): void
47
    {
48
        $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.bad.xlsx');
49
        self::assertNotFalse($filename);
50
        $this->expectException(SpreadsheetException::class);
51
        $this->expectExceptionMessage('Invalid protocol for linked drawing');
52
        $reader = IOFactory::createReader('Xlsx');
53
        $reader->load($filename);
54
    }
55
}
56