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.
This commit is contained in:
oleibman 2021-11-19 14:24:42 -08:00 committed by GitHub
parent f0f7449e2d
commit b67404229a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 49 deletions

View File

@ -8830,11 +8830,6 @@ parameters:
count: 2 count: 2
path: tests/PhpSpreadsheetTests/Reader/Xlsx/SheetsXlsxChartTest.php path: tests/PhpSpreadsheetTests/Reader/Xlsx/SheetsXlsxChartTest.php
-
message: "#^Expression in empty\\(\\) is not falsy\\.$#"
count: 1
path: tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php
- -
message: "#^Part \\$creationDate \\(mixed\\) of encapsed string cannot be cast to string\\.$#" message: "#^Part \\$creationDate \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1 count: 1

View File

@ -4,7 +4,6 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
use PhpOffice\PhpSpreadsheetTests\Reader\Utility\File; use PhpOffice\PhpSpreadsheetTests\Reader\Utility\File;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -12,51 +11,32 @@ class URLImageTest extends TestCase
{ {
public function testURLImageSource(): void public function testURLImageSource(): void
{ {
if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
self::markTestSkipped('Skipped due to setting of environment variable');
}
$filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.xlsx'); $filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.xlsx');
if (!$filename) { self::assertNotFalse($filename);
self::fail('No test file found.'); $reader = IOFactory::createReader('Xlsx');
} else { $spreadsheet = $reader->load($filename);
$reader = IOFactory::createReader('Xlsx'); $worksheet = $spreadsheet->getActiveSheet();
$spreadsheet = $reader->load($filename); $collection = $worksheet->getDrawingCollection();
$worksheet = $spreadsheet->getActiveSheet(); self::assertCount(1, $collection);
foreach ($worksheet->getDrawingCollection() as $drawing) { foreach ($collection as $drawing) {
if ($drawing instanceof MemoryDrawing) { self::assertInstanceOf(Drawing::class, $drawing);
// Skip memory drawings // Check if the source is a URL or a file path
} elseif ($drawing instanceof Drawing) { self::assertTrue($drawing->getIsURL());
// Check if the source is a URL or a file path self::assertSame('https://www.globalipmanager.com/DataFiles/Pics/20/Berniaga.comahp2.jpg', $drawing->getPath());
if ($drawing->getPath() && $drawing->getIsURL()) { $imageContents = file_get_contents($drawing->getPath());
$imageContents = file_get_contents($drawing->getPath()); self::assertNotFalse($imageContents);
$filePath = tempnam(sys_get_temp_dir(), 'Drawing'); $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
if ($filePath) { self::assertNotFalse($filePath);
file_put_contents($filePath, $imageContents); self::assertNotFalse(file_put_contents($filePath, $imageContents));
if (file_exists($filePath)) { $mimeType = mime_content_type($filePath);
$mimeType = mime_content_type($filePath); unlink($filePath);
// You could use the below to find the extension from mime type. self::assertNotFalse($mimeType);
if ($mimeType) { $extension = File::mime2ext($mimeType);
$extension = File::mime2ext($mimeType); self::assertSame('jpeg', $extension);
self::assertEquals('jpeg', $extension);
unlink($filePath);
} else {
self::fail('Could establish mime type.');
}
} else {
self::fail('Could not write file to disk.');
}
} else {
self::fail('Could not create fiel path.');
}
} else {
self::fail('Could not assert that the file contains an image that is URL sourced.');
}
} else {
self::fail('No image path found.');
}
}
if (empty($worksheet->getDrawingCollection())) {
self::fail('No image found in file.');
}
} }
} }
} }