1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* slince upload handler library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\Upload\Tests; |
7
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Slince\Upload\Exception\UploadException; |
10
|
|
|
use Slince\Upload\FileInfo; |
11
|
|
|
|
12
|
|
|
class FileInfoTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create instance |
16
|
|
|
* @return FileInfo |
17
|
|
|
*/ |
18
|
|
|
protected function createInstance() |
19
|
|
|
{ |
20
|
|
|
return new FileInfo([ |
21
|
|
|
'name' => 'thumb.zip', |
22
|
|
|
'type' => 'application/x-zip-compressed', |
23
|
|
|
'tmp_name' => __DIR__ . '/Fixtures/foo.zip', |
24
|
|
|
'error' => 0, |
25
|
|
|
'size' => 105190, |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGetBaseInfo() |
30
|
|
|
{ |
31
|
|
|
$fileInfo = $this->createInstance(); |
32
|
|
|
$this->assertEquals('thumb.zip', $fileInfo->getOriginName()); |
33
|
|
|
$this->assertEquals('application/x-zip-compressed', $fileInfo->getType()); |
34
|
|
|
$this->assertEquals(__DIR__ . '/Fixtures/foo.zip', $fileInfo->getTmpName()); |
35
|
|
|
$this->assertEquals(0, $fileInfo->getError()); |
36
|
|
|
$this->assertEquals(105190, $fileInfo->getSize()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testFromArray() |
40
|
|
|
{ |
41
|
|
|
$fileInfo = FileInfo::fromArray([ |
42
|
|
|
'name' => 'thumb.zip', |
43
|
|
|
'type' => 'application/x-zip-compressed', |
44
|
|
|
'tmp_name' => __DIR__ . '/Fixtures/foo.zip', |
45
|
|
|
'error' => 0, |
46
|
|
|
'size' => 105190, |
47
|
|
|
]); |
48
|
|
|
$this->assertInstanceOf(FileInfo::class, $fileInfo); |
49
|
|
|
$this->assertEquals('thumb.zip', $fileInfo->getOriginName()); |
50
|
|
|
$this->assertEquals('application/x-zip-compressed', $fileInfo->getType()); |
51
|
|
|
$this->assertEquals(__DIR__ . '/Fixtures/foo.zip', $fileInfo->getTmpName()); |
52
|
|
|
$this->assertEquals(0, $fileInfo->getError()); |
53
|
|
|
$this->assertEquals(105190, $fileInfo->getSize()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testSetErrorCode() |
57
|
|
|
{ |
58
|
|
|
$fileInfo = $this->createInstance(); |
59
|
|
|
$this->assertNull($fileInfo->getErrorCode()); |
60
|
|
|
$fileInfo->setErrorCode(123); |
61
|
|
|
$this->assertEquals(123, $fileInfo->getErrorCode()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testSetErrorMessage() |
65
|
|
|
{ |
66
|
|
|
$fileInfo = $this->createInstance(); |
67
|
|
|
$this->assertNull($fileInfo->getErrorMsg()); |
68
|
|
|
$fileInfo->setErrorMsg('foo message'); |
69
|
|
|
$this->assertEquals('foo message', $fileInfo->getErrorMsg()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testSetPath() |
73
|
|
|
{ |
74
|
|
|
$fileInfo = $this->createInstance(); |
75
|
|
|
$this->assertNull($fileInfo->getPath()); |
76
|
|
|
$fileInfo->setPath('/path/to/file'); |
77
|
|
|
$this->assertEquals('/path/to/file', $fileInfo->getPath()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testSetHasError() |
81
|
|
|
{ |
82
|
|
|
$fileInfo = $this->createInstance(); |
83
|
|
|
$this->assertNull($fileInfo->hasError()); |
84
|
|
|
$fileInfo->setHasError(false); |
85
|
|
|
$this->assertFalse($fileInfo->hasError()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetMimeType() |
89
|
|
|
{ |
90
|
|
|
$fileInfo = $this->createInstance(); |
91
|
|
|
$mimeType = $fileInfo->getMimeType(); |
92
|
|
|
$this->assertEquals('application/zip', $mimeType); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testInvalidArrayException() |
96
|
|
|
{ |
97
|
|
|
$this->setExpectedException(UploadException::class); |
98
|
|
|
new FileInfo([ |
99
|
|
|
'name' => 'thumb.zip', |
100
|
|
|
'type' => 'application/x-zip-compressed', |
101
|
|
|
'error' => 0, |
102
|
|
|
'size' => 105190, |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testConvertHumanReadableSize() |
107
|
|
|
{ |
108
|
|
|
$size = '5M'; |
109
|
|
|
$this->assertEquals(5 * 1024 * 1024, FileInfo::humanReadableToBytes($size)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|