|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Photobooth; |
| 4 | + |
| 5 | +use Photobooth\Enum\FolderEnum; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class PhotoboothCaptureTest |
| 9 | + */ |
| 10 | +class PhotoboothCaptureTest |
| 11 | +{ |
| 12 | + public string $fileName; |
| 13 | + public string $tmpFolder; |
| 14 | + public string $tmpFile; |
| 15 | + public array $logData = []; |
| 16 | + public array $captureCmds = [ |
| 17 | + 'gphoto2 --capture-image-and-download --filename=%s', |
| 18 | + 'gphoto2 --set-config output=Off --capture-image-and-download --filename=%s', |
| 19 | + 'gphoto2 --trigger-capture --wait-event-and-download=FILEADDED --filename=%s', |
| 20 | + 'gphoto2 --set-config output=Off --trigger-capture --wait-event-and-download=FILEADDED --filename=%s', |
| 21 | + 'gphoto2 --wait-event=300ms --capture-image-and-download --filename=%s', |
| 22 | + 'gphoto2 --set-config output=Off --wait-event=300ms --capture-image-and-download --filename=%s', |
| 23 | + ]; |
| 24 | + |
| 25 | + /** |
| 26 | + * PhotoboothCaptureTest constructor. |
| 27 | + */ |
| 28 | + public function __construct() |
| 29 | + { |
| 30 | + $this->tmpFolder = FolderEnum::TEMP->absolute(); |
| 31 | + } |
| 32 | + |
| 33 | + public function addLog(string $level, string $message, array $context = []): void |
| 34 | + { |
| 35 | + $this->logData[] = [ |
| 36 | + 'level' => $level, |
| 37 | + 'message' => $message, |
| 38 | + 'context' => $context |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Function to iterate through capture commands and execute them. |
| 44 | + */ |
| 45 | + public function executeCaptureTests(): void |
| 46 | + { |
| 47 | + foreach ($this->captureCmds as $index => $command) { |
| 48 | + // Set filename for each test command |
| 49 | + $this->fileName = sprintf('test-%d.jpg', $index + 1); |
| 50 | + $this->tmpFile = $this->tmpFolder . DIRECTORY_SEPARATOR . $this->fileName; |
| 51 | + |
| 52 | + $this->addLog('debug', 'Executing Command #' . ($index + 1), ['command' => $command]); |
| 53 | + |
| 54 | + // Execute the command |
| 55 | + $this->executeCmd($command); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Function to execute a single command. |
| 61 | + * |
| 62 | + * @param string $command The command to execute |
| 63 | + */ |
| 64 | + public function executeCmd(string $command): void |
| 65 | + { |
| 66 | + // Change directory if using gphoto command |
| 67 | + if (substr($command, 0, strlen('gphoto')) === 'gphoto') { |
| 68 | + chdir(dirname($this->tmpFile)); |
| 69 | + } |
| 70 | + |
| 71 | + // Prepare the command and redirect stderr to stdout |
| 72 | + $cmd = sprintf($command, $this->tmpFile); |
| 73 | + $cmd .= ' 2>&1'; |
| 74 | + $start_time = hrtime(true); |
| 75 | + exec($cmd, $output, $returnValue); |
| 76 | + |
| 77 | + // Handle command errors |
| 78 | + if ($returnValue) { |
| 79 | + $this->addLog('error', 'Command failed', [ |
| 80 | + 'command' => $command, |
| 81 | + 'output' => $output, |
| 82 | + 'returnValue' => $returnValue |
| 83 | + ]); |
| 84 | + return; |
| 85 | + } else { |
| 86 | + $this->addLog('info', 'Command executed successfully', [ |
| 87 | + 'command' => $command, |
| 88 | + 'output' => $output |
| 89 | + ]); |
| 90 | + } |
| 91 | + |
| 92 | + // Wait for the file to be created, if necessary |
| 93 | + $i = 0; |
| 94 | + $processingTime = 300; // 30 seconds (300 * 100ms) |
| 95 | + while ($i < $processingTime) { |
| 96 | + if (file_exists($this->tmpFile)) { |
| 97 | + break; |
| 98 | + } else { |
| 99 | + $i++; |
| 100 | + usleep(100000); // Wait 100ms |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // If the file does not exist, print the error and proceed |
| 105 | + if (!file_exists($this->tmpFile)) { |
| 106 | + $this->addLog('error', 'File was not created', [ |
| 107 | + 'command' => $command, |
| 108 | + 'output' => $output, |
| 109 | + 'returnValue' => $returnValue |
| 110 | + ]); |
| 111 | + } |
| 112 | + $end_time = hrtime(true); |
| 113 | + $execution_time = $end_time - $start_time; |
| 114 | + $execution_time_in_seconds = $execution_time / 1e9; |
| 115 | + $this->addLog('info', 'Execution time', [ |
| 116 | + 'output' => $execution_time_in_seconds . ' seconds' |
| 117 | + ]); |
| 118 | + } |
| 119 | +} |
0 commit comments