*/ protected array $imageFiles = []; /** * Writes a picture to a temporary file. * * @param int $width Width in pixels * @param int $height Height in pixels * @param string $format jpeg|png|gif|webp * @return string Path */ protected function makeImage(int $width = 400, int $height = 300, string $format = 'jpeg'): string { $image = imagecreatetruecolor($width, $height); imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, (int)imagecolorallocate($image, 200, 40, 40)); imagefilledellipse($image, intdiv($width, 2), intdiv($height, 2), intdiv($width, 2), intdiv($height, 2), (int)imagecolorallocate($image, 250, 250, 250)); $path = tempnam(sys_get_temp_dir(), 'plen'); match ($format) { 'png' => imagepng($image, $path), 'gif' => imagegif($image, $path), 'webp' => imagewebp($image, $path), default => imagejpeg($image, $path), }; $this->imageFiles[] = $path; return $path; } /** * Posts a form with an uploaded file in the field `image` (the test request carries files * separately from the form data). * * @param string $url URL * @param array $data Form data * @param string|null $path File to upload, null = no upload field at all * @param int $error Upload error code (UPLOAD_ERR_NO_FILE = field left empty) * @param string $name Client file name * @return void */ protected function postWithImage(string $url, array $data, ?string $path, int $error = UPLOAD_ERR_OK, string $name = 'bild.jpg'): void { $this->_request['files'] = $path === null ? [] : [ 'image' => new UploadedFile($path, (int)filesize($path), $error, $name, 'image/jpeg'), ]; try { $this->post($url, $data); } finally { $this->_request['files'] = []; } } /** * @return string The upload directory of the tests */ protected function uploadRoot(): string { return (string)Configure::read('Plenario.images.path'); } /** * Files stored for a poll. * * @param int $pollId Poll id * @return list File names */ protected function storedImages(int $pollId): array { $files = array_map('basename', glob($this->uploadRoot() . DS . $pollId . DS . '*') ?: []); sort($files); return $files; } /** * Removes temporary pictures and the upload directory of the tests. * * @return void */ protected function clearImages(): void { foreach ($this->imageFiles as $file) { @unlink($file); } $this->imageFiles = []; foreach (glob($this->uploadRoot() . DS . '*', GLOB_ONLYDIR) ?: [] as $directory) { array_map('unlink', glob($directory . DS . '*') ?: []); @rmdir($directory); } } }