diff options
Diffstat (limited to 'tests/unit/includes/PhotodriverTest.php')
-rw-r--r-- | tests/unit/includes/PhotodriverTest.php | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/unit/includes/PhotodriverTest.php b/tests/unit/includes/PhotodriverTest.php index 34dc058b7..db9883589 100644 --- a/tests/unit/includes/PhotodriverTest.php +++ b/tests/unit/includes/PhotodriverTest.php @@ -20,4 +20,62 @@ class PhotodriverTest extends UnitTestCase { $photo = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png'); $this->assertInstanceOf('Zotlabs\Photo\PhotoGd', $photo); } + + // Helper to create a temporary image file + private function createTempImage($type = 'jpeg'): string + { + $tmp = tempnam(sys_get_temp_dir(), 'img'); + switch ($type) { + case 'png': + $im = imagecreatetruecolor(10, 10); + imagepng($im, $tmp); + imagedestroy($im); + break; + case 'jpeg': + default: + $im = imagecreatetruecolor(10, 10); + imagejpeg($im, $tmp); + imagedestroy($im); + break; + } + return $tmp; + } + + public function testGuessImageTypeFromRawData() + { + $filename = 'irrelevant'; + $data = [ + 'body' => file_get_contents($this->createTempImage('jpeg')) + ]; + $result = guess_image_type($filename, $data); + $this->assertEquals('image/jpeg', $result); + } + + public function testGuessImageTypeFromLocalFile() + { + $file = $this->createTempImage('png'); + $result = guess_image_type($file); + $this->assertEquals('image/png', $result); + unlink($file); + } + + public function testGuessImageTypeFromHeaders() + { + $filename = 'irrelevant'; + $data = [ + 'header' => "Content-Type: image/jpeg\nOther: value" + ]; + $result = guess_image_type($filename, $data); + $this->assertEquals('image/jpeg', $result); + } + + public function testGuessImageTypeUnknownTypeReturnsNull() + { + $filename = 'not_an_image.txt'; + $data = [ + 'body' => 'not an image' + ]; + $result = guess_image_type($filename, $data); + $this->assertNull($result); + } } |