1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\Laravel\Request; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class UploadedFileTestable |
9
|
|
|
* Trait to easy get an istance of Illuminate\Http\UploadedFile for testing. |
10
|
|
|
* @package Padosoft\Laravel\Request |
11
|
|
|
*/ |
12
|
|
|
trait UploadedFileTestable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Implemented in PHPUnit |
16
|
|
|
* Asserts that a file exists. |
17
|
|
|
* |
18
|
|
|
* @param string $filename |
19
|
|
|
* @param string $message |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
abstract public static function assertFileExists(string $filename, string $message = ''): void; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create an instance of Illuminate\Http\UploadedFile for testing (param test=true). |
26
|
|
|
* Before creating UploadedFile class check if file exists with assertFileExists($fullPath). |
27
|
|
|
* @param string $fullPath |
28
|
|
|
* @param string $mimeType if empty try to resolve mimeType automatically. |
29
|
|
|
* @param int $errorCode default 0 (no error). |
30
|
|
|
* For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage() |
31
|
|
|
* @return UploadedFile |
32
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException |
33
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException |
34
|
|
|
*/ |
35
|
|
|
public function getUploadedFileForTest(string $fullPath, string $mimeType = '', int $errorCode = 0) : UploadedFile |
36
|
|
|
{ |
37
|
|
|
$this->assertFileExists($fullPath); |
38
|
|
|
|
39
|
|
|
$uploadedFile = new UploadedFile( |
40
|
|
|
$fullPath, |
41
|
|
|
pathinfo($fullPath, PATHINFO_BASENAME), |
42
|
|
|
($mimeType === null || $mimeType == '') ? mime_content_type($fullPath) : $mimeType, |
43
|
|
|
filesize($fullPath), |
44
|
|
|
$errorCode, |
45
|
|
|
true // true for test |
46
|
|
|
); |
47
|
|
|
return $uploadedFile; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Take $fullPath existing file, copy it to system temp dir with random name and |
52
|
|
|
* Create an instance of Illuminate\Http\UploadedFile for testing (param test=true). |
53
|
|
|
* Before creating UploadedFile class check if file exists with assertFileExists($fullPath). |
54
|
|
|
* @param string $fullPath |
55
|
|
|
* @param string $mimeType if empty try to resolve mimeType automatically. |
56
|
|
|
* @param int $errorCode default 0 (no error). |
57
|
|
|
* For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage() |
58
|
|
|
* @return UploadedFile |
59
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException |
60
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException |
61
|
|
|
*/ |
62
|
|
|
public function getUploadedFileForTestAndCopyInSysTempDir( |
63
|
|
|
string $fullPath, |
64
|
|
|
string $mimeType = '', |
65
|
|
|
int $errorCode = 0 |
66
|
|
|
) : UploadedFile |
67
|
|
|
{ |
68
|
|
|
$this->assertFileExists($fullPath); |
69
|
|
|
|
70
|
|
|
$origname = pathinfo($fullPath, PATHINFO_BASENAME); |
71
|
|
|
$path = sys_get_temp_dir() . '/' . $origname; |
72
|
|
|
if (file_exists($path)) { |
73
|
|
|
unlink($path); |
74
|
|
|
} |
75
|
|
|
copy($fullPath, $path); |
76
|
|
|
|
77
|
|
|
$uploadedFile = $this->getUploadedFileForTest($path, $mimeType, $errorCode); |
78
|
|
|
return $uploadedFile; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|