Skip to content

Commit

Permalink
test: getmerged, invalid image file
Browse files Browse the repository at this point in the history
  • Loading branch information
naomai committed Aug 4, 2024
1 parent 8c0f4bc commit 0f3c2f3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
Binary file added tests/TestImages/GarbageBytes.dat
Binary file not shown.
91 changes: 69 additions & 22 deletions tests/Unit/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
* @covers Naomai\PHPLayers\Image
*/
final class ImageTest extends TestCase {



public function testCreateFromGD() : void {
$gdSource = imagecreatetruecolor(50, 60);
imagefill($gdSource, 0, 0, 0xFF0000);
Expand All @@ -35,6 +32,13 @@ function() {
Image::createFromFile(__DIR__ . "/../TestImages/NonExistingImage.png");
}
);

$this->assertCallableThrows(
\RuntimeException::class,
function() {
Image::createFromFile(__DIR__ . "/../TestImages/GarbageBytes.dat");
}
);
}

public function testGetLayerCount() : void {
Expand Down Expand Up @@ -80,8 +84,7 @@ public function testNewLayer() : void {
$layer = $testImg->newLayer();
$layersCountAfter = $testImg->getLayerCount();

$this->assertSame(1, $layersCountBefore);
$this->assertSame(2, $layersCountAfter);
$this->assertSame($layersCountBefore+1, $layersCountAfter);

$layerStack = $testImg->getLayerStack();
$newLayerIndex = $layerStack->getIndexOf($layer);
Expand Down Expand Up @@ -141,26 +144,42 @@ public function testReorder() {
$this->assertSame(1, $fgPosition);
}

public function testGetDataUrlPNG() {
$testImg = static::createEmptyImage();
$dataUrl = $testImg->getDataUrlPNG();
public function testGetMerged(){
$testImg = static::createTwoLayerImage();
$mergedLayer = $testImg->getMerged();

$this->assertStringStartsWith("data:", $dataUrl);
$this->assertStringContainsString("image/png", $dataUrl);
$this->assertTrue($mergedLayer instanceof \Naomai\PHPLayers\Layer);

//The original layer set is left intact.
$layerCount = $testImg->getLayerCount();
$this->assertEquals(2, $layerCount);

//The new layer is not attached to the image
$mergedLayerIndexInStack = $testImg->getLayerStack()->getIndexOf($mergedLayer);
$this->assertFalse($mergedLayerIndexInStack);

//image without layers should also produce a result
$nullImg = new Image(50, 80, false);
$mergedLayer = $nullImg->getMerged();
$this->assertTrue($mergedLayer instanceof \Naomai\PHPLayers\Layer);

$imageData = file_get_contents($dataUrl);
$this->assertValidImageData($imageData);
}

public function testGetDataUrlJPEG() {
$testImg = static::createEmptyImage();
$dataUrl = $testImg->getDataUrlJPEG();
public function testGetMergedGD(){
$testImg = static::createTwoLayerImage();
$mergedGD = $testImg->getMergedGD();

$this->assertStringStartsWith("data:", $dataUrl);
$this->assertStringContainsString("image/jpeg", $dataUrl);
$this->assertTrue($mergedGD instanceof \GdImage);

//The original layer set is left intact.
$layerCount = $testImg->getLayerCount();
$this->assertEquals(2, $layerCount);

//image without layers should also produce a result
$nullImg = new Image(50, 80, false);
$mergedGD = $nullImg->getMergedGD();
$this->assertTrue($mergedGD instanceof \GdImage);

$imageData = file_get_contents($dataUrl);
$this->assertValidImageData($imageData);
}

public function testGetComposer() {
Expand All @@ -186,6 +205,37 @@ public function testSetComposer() {
$this->assertSame($composerMock, $composer);
}

public function testExport(){
$testImg = static::createEmptyImage();
$exporter = $testImg->export();
$this->assertTrue(
$exporter instanceof \Naomai\PHPLayers\Helpers\ImageExporter
);
}

/* TODO delegate to ImageExporterTest */
public function testGetDataUrlPNG() {
$testImg = static::createEmptyImage();
$dataUrl = $testImg->getDataUrlPNG();

$this->assertStringStartsWith("data:", $dataUrl);
$this->assertStringContainsString("image/png", $dataUrl);

$imageData = file_get_contents($dataUrl);
$this->assertValidImageData($imageData);
}

public function testGetDataUrlJPEG() {
$testImg = static::createEmptyImage();
$dataUrl = $testImg->getDataUrlJPEG();

$this->assertStringStartsWith("data:", $dataUrl);
$this->assertStringContainsString("image/jpeg", $dataUrl);

$imageData = file_get_contents($dataUrl);
$this->assertValidImageData($imageData);
}


public function testConstructWithInvalidDimensions() : void {
$this->assertCallableThrows(
Expand All @@ -211,9 +261,6 @@ public function testConstructWithInvalidDimensions() : void {
);
}




private static function createEmptyImage() : Image {
$testImg = new Image(200, 100);
return $testImg;
Expand Down

0 comments on commit 0f3c2f3

Please sign in to comment.