Passed
Pull Request — master (#4170)
by Owen
16:26 queued 05:25
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
            self::assertSame(IMAGETYPE_PNG, $drawing->getType());
34
            self::assertSame(84, $drawing->getWidth());
35
            self::assertSame(44, $drawing->getHeight());
36
        }
37
    }
38
39
    public function testURLImageSourceNotFound(): void
40
    {
41
        if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
42
            self::markTestSkipped('Skipped due to setting of environment variable');
43
        }
44
        $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.notfound.xlsx');
45
        self::assertNotFalse($filename);
46
        $reader = IOFactory::createReader('Xlsx');
47
        $spreadsheet = $reader->load($filename);
48
        $worksheet = $spreadsheet->getActiveSheet();
49
        $collection = $worksheet->getDrawingCollection();
50
        self::assertCount(0, $collection);
51
    }
52
53
    public function testURLImageSourceBadProtocol(): void
54
    {
55
        $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.bad.dontuse');
56
        self::assertNotFalse($filename);
57
        $this->expectException(SpreadsheetException::class);
58
        $this->expectExceptionMessage('Invalid protocol for linked drawing');
59
        $reader = IOFactory::createReader('Xlsx');
60
        $reader->load($filename);
61
    }
62
}
63