Files
backend/tests/Service/ImageServiceTest.php
2026-05-27 19:36:32 +03:00

77 lines
2.4 KiB
PHP

<?php
namespace Tests\Service;
use App\Service\Image\ImageService;
use App\Service\Image\Interfaces\ImageServiceInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use PHPUnit\Framework\MockObject\MockObject;
class ImageServiceTest extends TestCase
{
private ImageServiceInterface $imageService;
protected function setUp(): void
{
$this->imageService = new ImageService();
}
/**
* @dataProvider imageDataProvider
*/
public function testGetPicture(string $filePath, int $width, int $height, string $expectedType): void
{
// Создаем тестовый файл изображения
$testImagePath = __DIR__ . '/test_image.jpg';
$this->createTestImage($testImagePath);
$response = $this->imageService->getPicture($testImagePath, $width, $height);
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals($expectedType, $response->headers->get('Content-Type'));
$this->assertNotEmpty($response->getContent());
// Удаляем тестовый файл
unlink($testImagePath);
}
public function imageDataProvider(): array
{
return [
['test_image.jpg', 200, 200, 'image/jpeg'],
['test_image.png', 100, 100, 'image/png'],
['test_image.gif', 150, 150, 'image/gif'],
];
}
public function testInvalidFile(): void
{
$response = $this->imageService->getPicture('non_existent_file.jpg');
$this->assertInstanceOf(Response::class, $response);
$this->assertEmpty($response->getContent());
}
public function testDefaultSize(): void
{
$testImagePath = __DIR__ . '/test_image.jpg';
$this->createTestImage($testImagePath);
$response = $this->imageService->getPicture($testImagePath);
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('image/jpeg', $response->headers->get('Content-Type'));
$this->assertNotEmpty($response->getContent());
unlink($testImagePath);
}
private function createTestImage(string $path): void
{
$image = imagecreatetruecolor(800, 600);
$color = imagecolorallocate($image, 255, 0, 0);
imagefilledrectangle($image, 0, 0, 800, 600, $color);
imagejpeg($image, $path);
imagedestroy($image);
}
}