Skip to content

Commit b674042

Browse files
authored
Allow Skipping One Unit Test (#2402)
* Allow Skipping One Unit Test Alone in the test suite, URLImageTest needs to access the internet. It's a little fragile (the site that it's looking for may go away or change), but no real problem. However, on my system, it runs afoul of my proxy. Rather than jumping through hoops when I run the test suite (which happens very often), I am changing the test to skip if an environment variable is set to a specific value. This should not adversely affect anyone, and the test will still run in github, but it will help me a lot. * Scrutinizer It complained that my one new if statement made the module too complex. There actually were a number of if-then-else situations that could be handled just as well with assertions. I have changed it accordingly.
1 parent f0f7449 commit b674042

File tree

2 files changed

+24
-49
lines changed

2 files changed

+24
-49
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8830,11 +8830,6 @@ parameters:
88308830
count: 2
88318831
path: tests/PhpSpreadsheetTests/Reader/Xlsx/SheetsXlsxChartTest.php
88328832

8833-
-
8834-
message: "#^Expression in empty\\(\\) is not falsy\\.$#"
8835-
count: 1
8836-
path: tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php
8837-
88388833
-
88398834
message: "#^Part \\$creationDate \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
88408835
count: 1

tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,39 @@
44

55
use PhpOffice\PhpSpreadsheet\IOFactory;
66
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
7-
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
87
use PhpOffice\PhpSpreadsheetTests\Reader\Utility\File;
98
use PHPUnit\Framework\TestCase;
109

1110
class URLImageTest extends TestCase
1211
{
1312
public function testURLImageSource(): void
1413
{
14+
if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
15+
self::markTestSkipped('Skipped due to setting of environment variable');
16+
}
1517
$filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.xlsx');
16-
if (!$filename) {
17-
self::fail('No test file found.');
18-
} else {
19-
$reader = IOFactory::createReader('Xlsx');
20-
$spreadsheet = $reader->load($filename);
21-
$worksheet = $spreadsheet->getActiveSheet();
22-
23-
foreach ($worksheet->getDrawingCollection() as $drawing) {
24-
if ($drawing instanceof MemoryDrawing) {
25-
// Skip memory drawings
26-
} elseif ($drawing instanceof Drawing) {
27-
// Check if the source is a URL or a file path
28-
if ($drawing->getPath() && $drawing->getIsURL()) {
29-
$imageContents = file_get_contents($drawing->getPath());
30-
$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
31-
if ($filePath) {
32-
file_put_contents($filePath, $imageContents);
33-
if (file_exists($filePath)) {
34-
$mimeType = mime_content_type($filePath);
35-
// You could use the below to find the extension from mime type.
36-
if ($mimeType) {
37-
$extension = File::mime2ext($mimeType);
38-
self::assertEquals('jpeg', $extension);
39-
unlink($filePath);
40-
} else {
41-
self::fail('Could establish mime type.');
42-
}
43-
} else {
44-
self::fail('Could not write file to disk.');
45-
}
46-
} else {
47-
self::fail('Could not create fiel path.');
48-
}
49-
} else {
50-
self::fail('Could not assert that the file contains an image that is URL sourced.');
51-
}
52-
} else {
53-
self::fail('No image path found.');
54-
}
55-
}
18+
self::assertNotFalse($filename);
19+
$reader = IOFactory::createReader('Xlsx');
20+
$spreadsheet = $reader->load($filename);
21+
$worksheet = $spreadsheet->getActiveSheet();
22+
$collection = $worksheet->getDrawingCollection();
23+
self::assertCount(1, $collection);
5624

57-
if (empty($worksheet->getDrawingCollection())) {
58-
self::fail('No image found in file.');
59-
}
25+
foreach ($collection as $drawing) {
26+
self::assertInstanceOf(Drawing::class, $drawing);
27+
// Check if the source is a URL or a file path
28+
self::assertTrue($drawing->getIsURL());
29+
self::assertSame('https://www.globalipmanager.com/DataFiles/Pics/20/Berniaga.comahp2.jpg', $drawing->getPath());
30+
$imageContents = file_get_contents($drawing->getPath());
31+
self::assertNotFalse($imageContents);
32+
$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
33+
self::assertNotFalse($filePath);
34+
self::assertNotFalse(file_put_contents($filePath, $imageContents));
35+
$mimeType = mime_content_type($filePath);
36+
unlink($filePath);
37+
self::assertNotFalse($mimeType);
38+
$extension = File::mime2ext($mimeType);
39+
self::assertSame('jpeg', $extension);
6040
}
6141
}
6242
}

0 commit comments

Comments
 (0)