mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-23 00:30:09 +00:00
ec4098c8fd
While we might never be able to have 100% of our code strict, we can at the very least do it for all of our tests. This ensures that our tests are using our API with the types as intended by the test author, and not silently be cast to what our API requires.
84 lines
3.0 KiB
PHP
84 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use GdImage;
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class MemoryDrawingTest extends TestCase
|
|
{
|
|
public function testMemoryDrawing(): void
|
|
{
|
|
$name = 'In-Memory image';
|
|
$gdImage = @imagecreatetruecolor(120, 20);
|
|
if ($gdImage === false) {
|
|
self::markTestSkipped('Unable to create GD Image for MemoryDrawing');
|
|
}
|
|
|
|
$textColor = (int) imagecolorallocate($gdImage, 255, 255, 255);
|
|
imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor);
|
|
|
|
$drawing = new MemoryDrawing();
|
|
$drawing->setName($name);
|
|
$drawing->setDescription('In-Memory image 1');
|
|
$drawing->setCoordinates('A1');
|
|
$drawing->setImageResource($gdImage);
|
|
$drawing->setRenderingFunction(MemoryDrawing::RENDERING_PNG);
|
|
$drawing->setMimeType(MemoryDrawing::MIMETYPE_PNG);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_DEFAULT, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_DEFAULT, $drawing->getRenderingFunction());
|
|
}
|
|
|
|
public function testMemoryDrawingFromString(): void
|
|
{
|
|
$imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg';
|
|
|
|
$imageString = file_get_contents($imageFile);
|
|
if ($imageString === false) {
|
|
self::markTestSkipped('Unable to read Image file for MemoryDrawing');
|
|
}
|
|
$drawing = MemoryDrawing::fromString($imageString);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction());
|
|
}
|
|
|
|
public function testMemoryDrawingFromInvalidString(): void
|
|
{
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage('Value cannot be converted to an image');
|
|
|
|
$imageString = 'I am not an image';
|
|
MemoryDrawing::fromString($imageString);
|
|
}
|
|
|
|
public function testMemoryDrawingFromStream(): void
|
|
{
|
|
$imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg';
|
|
|
|
$imageStream = fopen($imageFile, 'rb');
|
|
if ($imageStream === false) {
|
|
self::markTestSkipped('Unable to read Image file for MemoryDrawing');
|
|
}
|
|
$drawing = MemoryDrawing::fromStream($imageStream);
|
|
fclose($imageStream);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction());
|
|
}
|
|
}
|