2026-01-30 17:58:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Tests\Command;
|
|
|
|
|
|
|
|
|
|
use App\Command\GenerateVideoThumbsCommand;
|
|
|
|
|
use App\Service\Media\VideoThumbnailer;
|
2026-01-31 13:49:25 +01:00
|
|
|
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
|
2026-01-30 17:58:12 +01:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
|
|
|
|
2026-01-31 13:49:25 +01:00
|
|
|
#[AllowMockObjectsWithoutExpectations]
|
2026-01-30 17:58:12 +01:00
|
|
|
class GenerateVideoThumbsCommandTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
private MockObject&ParameterBagInterface $parameterBag;
|
|
|
|
|
private MockObject&VideoThumbnailer $videoThumbnailer;
|
|
|
|
|
private string $tempDir;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->parameterBag = $this->createMock(ParameterBagInterface::class);
|
|
|
|
|
$this->videoThumbnailer = $this->createMock(VideoThumbnailer::class);
|
|
|
|
|
|
|
|
|
|
$this->tempDir = sys_get_temp_dir() . '/thumbs_test_' . uniqid();
|
|
|
|
|
mkdir($this->tempDir . '/public/provider/video', 0777, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
$this->removeDirectory($this->tempDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecuteVideoNotFound()
|
|
|
|
|
{
|
|
|
|
|
// Setup mock
|
|
|
|
|
$this->parameterBag->method('get')->with('kernel.project_dir')->willReturn($this->tempDir);
|
|
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
|
$command = new GenerateVideoThumbsCommand($this->parameterBag, $this->videoThumbnailer);
|
|
|
|
|
$application = new Application();
|
2026-01-31 13:49:25 +01:00
|
|
|
$application->addCommand($command);
|
2026-01-30 17:58:12 +01:00
|
|
|
$commandTester = new CommandTester($application->find('app:generate-video-thumbs'));
|
|
|
|
|
|
|
|
|
|
$commandTester->execute([]);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
$output = $commandTester->getDisplay();
|
|
|
|
|
$this->assertStringContainsString('Vidéo introuvable', $output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecuteSuccess()
|
|
|
|
|
{
|
|
|
|
|
// Setup file
|
|
|
|
|
touch($this->tempDir . '/public/provider/video/video.mp4');
|
|
|
|
|
|
|
|
|
|
// Setup mock
|
|
|
|
|
$this->parameterBag->method('get')->with('kernel.project_dir')->willReturn($this->tempDir);
|
|
|
|
|
|
|
|
|
|
$this->videoThumbnailer->expects($this->once())
|
|
|
|
|
->method('generateThumbnail')
|
|
|
|
|
->with(
|
|
|
|
|
$this->stringEndsWith('video.mp4'),
|
|
|
|
|
$this->stringEndsWith('video.jpg')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
|
$command = new GenerateVideoThumbsCommand($this->parameterBag, $this->videoThumbnailer);
|
|
|
|
|
$application = new Application();
|
2026-01-31 13:49:25 +01:00
|
|
|
$application->addCommand($command);
|
2026-01-30 17:58:12 +01:00
|
|
|
$commandTester = new CommandTester($application->find('app:generate-video-thumbs'));
|
|
|
|
|
|
|
|
|
|
$commandTester->execute([]);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
$output = $commandTester->getDisplay();
|
|
|
|
|
$this->assertStringContainsString('[OK] Miniature générée', $output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function removeDirectory($dir) {
|
|
|
|
|
if (!is_dir($dir)) return;
|
|
|
|
|
$files = array_diff(scandir($dir), array('.','..'));
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
(is_dir("$dir/$file")) ? $this->removeDirectory("$dir/$file") : unlink("$dir/$file");
|
|
|
|
|
}
|
|
|
|
|
rmdir($dir);
|
|
|
|
|
}
|
|
|
|
|
}
|