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
|
|
|
* Create an instance of Illuminate\Http\UploadedFile for testing (param test=true). |
16
|
|
|
* Before creating UploadedFile class check if file exists with assertFileExists($fullPath). |
17
|
|
|
* @param string $fullPath |
18
|
|
|
* @param string $mimeType if empty try to resolve mimeType automatically. |
19
|
|
|
* @param int $errorCode default 0 (no error). |
20
|
|
|
* For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage() |
21
|
|
|
* @return UploadedFile |
22
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException |
23
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException |
24
|
|
|
*/ |
25
|
|
|
public function getUploadedFileForTest(string $fullPath, string $mimeType = '', int $errorCode = 0) : UploadedFile |
26
|
|
|
{ |
27
|
|
|
$this->assertFileExists($fullPath); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$uploadedFile = new UploadedFile( |
30
|
|
|
$fullPath, |
31
|
|
|
pathinfo($fullPath, PATHINFO_BASENAME), |
32
|
|
|
($mimeType === null || $mimeType == '') ? mime_content_type($fullPath) : $mimeType, |
33
|
|
|
filesize($fullPath), |
34
|
|
|
$errorCode, |
35
|
|
|
true // true for test |
36
|
|
|
); |
37
|
|
|
return $uploadedFile; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Take $fullPath existing file, copy it to system temp dir with random name and |
42
|
|
|
* Create an instance of Illuminate\Http\UploadedFile for testing (param test=true). |
43
|
|
|
* Before creating UploadedFile class check if file exists with assertFileExists($fullPath). |
44
|
|
|
* @param string $fullPath |
45
|
|
|
* @param string $mimeType if empty try to resolve mimeType automatically. |
46
|
|
|
* @param int $errorCode default 0 (no error). |
47
|
|
|
* For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage() |
48
|
|
|
* @return UploadedFile |
49
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException |
50
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function getUploadedFileForTestAndCopyInSysTempDir( |
53
|
|
|
string $fullPath, |
54
|
|
|
string $mimeType = '', |
55
|
|
|
int $errorCode = 0 |
56
|
|
|
) : UploadedFile |
57
|
|
|
{ |
58
|
|
|
$this->assertFileExists($fullPath); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$origname = pathinfo($fullPath, PATHINFO_BASENAME); |
61
|
|
|
$path = sys_get_temp_dir() . '/' . $origname; |
62
|
|
|
if (file_exists($path)) { |
63
|
|
|
unlink($path); |
64
|
|
|
} |
65
|
|
|
copy($fullPath, $path); |
66
|
|
|
|
67
|
|
|
$uploadedFile = $this->getUploadedFileForTest($path, $mimeType, $errorCode); |
68
|
|
|
return $uploadedFile; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.