1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Koded\Http\UploadedFile; |
7
|
|
|
use Koded\Http\UploadedFileException; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\StreamInterface; |
10
|
|
|
|
11
|
|
|
class UploadedFileTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
use AssertionTestSupportTrait; |
14
|
|
|
|
15
|
|
|
private UploadedFile $SUT; |
16
|
|
|
private string $file = '/tmp/y4k9a7fm'; |
17
|
|
|
|
18
|
|
|
public function test_constructor() |
19
|
|
|
{ |
20
|
|
|
$properties = $this->getObjectProperties($this->SUT, ['file', 'moved']); |
21
|
|
|
|
22
|
|
|
$this->assertSame('text/plain', $this->SUT->getClientMediaType()); |
23
|
|
|
$this->assertSame('filename.txt', $this->SUT->getClientFilename()); |
24
|
|
|
$this->assertSame(5, $this->SUT->getSize()); |
25
|
|
|
$this->assertSame(UPLOAD_ERR_OK, $this->SUT->getError()); |
26
|
|
|
$this->assertSame('w+b', $this->SUT->getStream()->getMetadata('mode')); |
27
|
|
|
|
28
|
|
|
$this->assertSame($this->file, $properties['file']); |
29
|
|
|
$this->assertSame(false, $properties['moved']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider invalidTmpName |
34
|
|
|
*/ |
35
|
|
|
public function test_should_fail_when_tmp_name_is_invalid($resource) |
36
|
|
|
{ |
37
|
|
|
$this->expectException(InvalidArgumentException::class); |
38
|
|
|
|
39
|
|
|
$SUT = $this->prepareFile($resource); |
40
|
|
|
$this->assertInstanceOf(StreamInterface::class, $SUT->getStream()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test_move_to_invalid_target_path() |
44
|
|
|
{ |
45
|
|
|
$this->expectException(InvalidArgumentException::class); |
46
|
|
|
$this->expectExceptionMessage('The provided path for moveTo operation is not valid'); |
47
|
|
|
|
48
|
|
|
$this->SUT->moveTo(''); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function test_should_throw_exception_when_file_is_not_set() |
52
|
|
|
{ |
53
|
|
|
$this->expectException(InvalidArgumentException::class); |
54
|
|
|
$this->expectExceptionMessage('The uploaded file is not supported'); |
55
|
|
|
|
56
|
|
|
$file = (include __DIR__ . '/fixtures/simple-file-array.php')['test']; |
57
|
|
|
unset($file['tmp_name']); |
58
|
|
|
|
59
|
|
|
$SUT = new UploadedFile($file); |
60
|
|
|
$SUT->moveTo('/tmp/test-moved-to'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function invalidTmpName() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
[null], |
67
|
|
|
[true], |
68
|
|
|
[0], |
69
|
|
|
[1.2], |
70
|
|
|
[new \stdClass], |
71
|
|
|
[''], |
72
|
|
|
[[fopen('php://temp', 'r')]], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function test_should_throw_exception_on_upload_error() |
77
|
|
|
{ |
78
|
|
|
$this->expectException(UploadedFileException::class); |
79
|
|
|
$this->expectExceptionCode(UPLOAD_ERR_CANT_WRITE); |
80
|
|
|
|
81
|
|
|
$file = (include __DIR__ . '/fixtures/simple-file-array.php')['test']; |
82
|
|
|
|
83
|
|
|
$file['error'] = UPLOAD_ERR_CANT_WRITE; |
84
|
|
|
|
85
|
|
|
$SUT = new UploadedFile($file); |
86
|
|
|
$SUT->moveTo('/tmp/test-moved-to/test-copy.txt'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function setUp(): void |
90
|
|
|
{ |
91
|
|
|
touch($this->file); |
92
|
|
|
file_put_contents($this->file, 'hello'); |
93
|
|
|
|
94
|
|
|
$files = include __DIR__ . '/fixtures/simple-file-array.php'; |
95
|
|
|
$this->SUT = new UploadedFile($files['test']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function tearDown(): void |
99
|
|
|
{ |
100
|
|
|
@unlink($this->file); |
|
|
|
|
101
|
|
|
@unlink('/tmp/test-moved-to/filename.txt'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function prepareFile($resource): UploadedFIle |
105
|
|
|
{ |
106
|
|
|
$file = (include __DIR__ . '/fixtures/simple-file-array.php')['test']; |
107
|
|
|
$file['tmp_name'] = $resource; |
108
|
|
|
return new UploadedFile($file); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: