nhance_partner_be/tests/_support/Storage/TestUploadedFile.php

50 lines
1.4 KiB
PHP

<?php
namespace Tests\Support\Storage;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\Files\UploadedFile;
/**
* UploadedFile test double that works outside HTTP upload context.
*/
class TestUploadedFile extends UploadedFile
{
public function isValid(): bool
{
return $this->getError() === UPLOAD_ERR_OK && is_file($this->getTempName());
}
public function move(string $targetPath, ?string $name = null, bool $overwrite = false)
{
$targetPath = rtrim($targetPath, '/') . '/';
if (! is_dir($targetPath) && ! mkdir($targetPath, 0777, true) && ! is_dir($targetPath)) {
throw HTTPException::forMoveFailed(basename($this->getTempName()), $targetPath, 'Unable to create target directory');
}
if ($this->hasMoved()) {
throw HTTPException::forAlreadyMoved();
}
if (! $this->isValid()) {
throw HTTPException::forInvalidFile();
}
$name = $name ?? $this->getName();
$destination = $overwrite ? $targetPath . $name : $targetPath . $name;
if (! @rename($this->getTempName(), $destination)) {
if (! @copy($this->getTempName(), $destination)) {
throw HTTPException::forMoveFailed(basename($this->getTempName()), $targetPath, 'rename/copy failed');
}
@unlink($this->getTempName());
}
$this->hasMoved = true;
return true;
}
}