Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Skipping One Unit Test #2402

Merged
merged 3 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8830,11 +8830,6 @@ parameters:
count: 2
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\\.$#"
count: 1
Expand Down
68 changes: 24 additions & 44 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,39 @@

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
use PhpOffice\PhpSpreadsheetTests\Reader\Utility\File;
use PHPUnit\Framework\TestCase;

class URLImageTest extends TestCase
{
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');
if (!$filename) {
self::fail('No test file found.');
} else {
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$worksheet = $spreadsheet->getActiveSheet();

foreach ($worksheet->getDrawingCollection() as $drawing) {
if ($drawing instanceof MemoryDrawing) {
// Skip memory drawings
} elseif ($drawing instanceof Drawing) {
// Check if the source is a URL or a file path
if ($drawing->getPath() && $drawing->getIsURL()) {
$imageContents = file_get_contents($drawing->getPath());
$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
if ($filePath) {
file_put_contents($filePath, $imageContents);
if (file_exists($filePath)) {
$mimeType = mime_content_type($filePath);
// You could use the below to find the extension from mime type.
if ($mimeType) {
$extension = File::mime2ext($mimeType);
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.');
}
}
self::assertNotFalse($filename);
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$worksheet = $spreadsheet->getActiveSheet();
$collection = $worksheet->getDrawingCollection();
self::assertCount(1, $collection);

if (empty($worksheet->getDrawingCollection())) {
self::fail('No image found in file.');
}
foreach ($collection as $drawing) {
self::assertInstanceOf(Drawing::class, $drawing);
// Check if the source is a URL or a file path
self::assertTrue($drawing->getIsURL());
self::assertSame('https://www.globalipmanager.com/DataFiles/Pics/20/Berniaga.comahp2.jpg', $drawing->getPath());
$imageContents = file_get_contents($drawing->getPath());
self::assertNotFalse($imageContents);
$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
self::assertNotFalse($filePath);
self::assertNotFalse(file_put_contents($filePath, $imageContents));
$mimeType = mime_content_type($filePath);
unlink($filePath);
self::assertNotFalse($mimeType);
$extension = File::mime2ext($mimeType);
self::assertSame('jpeg', $extension);
}
}
}