71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Services\Ocr\PhotoOcrPreparer;
|
|
use Illuminate\Support\Facades\Process;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Tests\TestCase;
|
|
|
|
class PhotoOcrPreparerTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$bins = new \ReflectionProperty(PhotoOcrPreparer::class, 'bins');
|
|
$bins->setValue(null, []);
|
|
}
|
|
|
|
#[Test]
|
|
public function resize_prefers_imagemagick_over_gd(): void
|
|
{
|
|
$path = $this->jpeg(1800, 1200);
|
|
Process::fake(function () {
|
|
return Process::result();
|
|
});
|
|
|
|
$this->invokeResize($path, 1280);
|
|
|
|
Process::assertRan(function ($process) {
|
|
$cmd = $process->command;
|
|
$bin = is_array($cmd) ? (string) ($cmd[0] ?? '') : (string) $cmd;
|
|
|
|
return is_array($cmd)
|
|
&& in_array('-resize', $cmd, true)
|
|
&& (str_contains($bin, 'magick') || str_contains($bin, 'convert'));
|
|
});
|
|
@unlink($path);
|
|
}
|
|
|
|
#[Test]
|
|
public function gd_skips_when_truecolor_would_exceed_memory(): void
|
|
{
|
|
$prev = ini_get('memory_limit');
|
|
ini_set('memory_limit', '128M');
|
|
try {
|
|
$fits = new \ReflectionMethod(PhotoOcrPreparer::class, 'gdFitsMemory');
|
|
$ok = $fits->invoke(app(PhotoOcrPreparer::class), 20000, 20000);
|
|
$this->assertFalse($ok);
|
|
} finally {
|
|
ini_set('memory_limit', (string) $prev);
|
|
}
|
|
}
|
|
|
|
private function invokeResize(string $path, int $max): void
|
|
{
|
|
config(['coruna.ocr.max_edge' => $max]);
|
|
$resize = new \ReflectionMethod(PhotoOcrPreparer::class, 'resize');
|
|
$resize->invoke(app(PhotoOcrPreparer::class), $path);
|
|
}
|
|
|
|
private function jpeg(int $width, int $height): string
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'ocrprep_').'.jpg';
|
|
$im = imagecreatetruecolor($width, $height);
|
|
imagejpeg($im, $path, 80);
|
|
imagedestroy($im);
|
|
|
|
return $path;
|
|
}
|
|
}
|