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

URLImageTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 39
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testURLImageSource() 0 28 3
A testURLImageSourceNotFound() 0 12 2
A testURLImageSourceBadProtocol() 0 8 1
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 testURLImageSourceNotFound(): void
47
    {
48
        if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
49
            self::markTestSkipped('Skipped due to setting of environment variable');
50
        }
51
        $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.notfound.xlsx');
52
        self::assertNotFalse($filename);
53
        $reader = IOFactory::createReader('Xlsx');
54
        $spreadsheet = $reader->load($filename);
55
        $worksheet = $spreadsheet->getActiveSheet();
56
        $collection = $worksheet->getDrawingCollection();
57
        self::assertCount(0, $collection);
58
    }
59
60
    public function testURLImageSourceBadProtocol(): void
61
    {
62
        $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.bad.dontuse');
63
        self::assertNotFalse($filename);
64
        $this->expectException(SpreadsheetException::class);
65
        $this->expectExceptionMessage('Invalid protocol for linked drawing');
66
        $reader = IOFactory::createReader('Xlsx');
67
        $reader->load($filename);
68
    }
69
}
70