1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChecksTests\TraitsTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_mime\Check\Traits\TToLocalFile; |
8
|
|
|
use kalanis\kw_mime\MimeException; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ToLocalFileTest extends CommonTestClass |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @throws MimeException |
15
|
|
|
*/ |
16
|
|
|
public function testNone1(): void |
17
|
|
|
{ |
18
|
|
|
$lib = new XToLocalFile(); |
19
|
|
|
$lib->initTempFile(); |
20
|
|
|
@unlink($lib->getTempFile()); |
|
|
|
|
21
|
|
|
$this->expectException(MimeException::class); |
22
|
|
|
$lib->convert('file', false); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws MimeException |
27
|
|
|
*/ |
28
|
|
|
public function testNone2(): void |
29
|
|
|
{ |
30
|
|
|
$lib = new XToLocalFile(); |
31
|
|
|
$lib->initTempFile(); |
32
|
|
|
@unlink($lib->getTempFile()); |
|
|
|
|
33
|
|
|
$this->expectException(MimeException::class); |
34
|
|
|
$lib->convert('file', null); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws MimeException |
39
|
|
|
*/ |
40
|
|
|
public function testString(): void |
41
|
|
|
{ |
42
|
|
|
$lib = new XToLocalFile(); |
43
|
|
|
$lib->initTempFile(); |
44
|
|
|
$pt1 = $lib->convert('data', 'test data'); |
45
|
|
|
$this->assertEquals('test data', file_get_contents($pt1)); |
46
|
|
|
@unlink($pt1); |
|
|
|
|
47
|
|
|
$lib->initTempFile(); |
48
|
|
|
$pt2 = $lib->convert('nums', 123456); |
49
|
|
|
$this->assertEquals('123456', file_get_contents($pt2)); |
50
|
|
|
@unlink($pt2); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws MimeException |
55
|
|
|
*/ |
56
|
|
|
public function testResource(): void |
57
|
|
|
{ |
58
|
|
|
$lib = new XToLocalFile(); |
59
|
|
|
$res = @fopen('php://memory', 'r+'); |
60
|
|
|
fwrite($res, 'okmijnuhb'); |
|
|
|
|
61
|
|
|
$lib->initTempFile(); |
62
|
|
|
$pt1 = $lib->convert('res', $res); |
|
|
|
|
63
|
|
|
$this->assertEquals('okmijnuhb', file_get_contents($pt1)); |
64
|
|
|
@unlink($pt1); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
class XToLocalFile |
70
|
|
|
{ |
71
|
|
|
use TToLocalFile; |
72
|
|
|
|
73
|
|
|
/** @var string|null */ |
74
|
|
|
protected $tempFile = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* @param string|resource $content |
79
|
|
|
* @throws MimeException |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function convert(string $name, $content): string |
83
|
|
|
{ |
84
|
|
|
$localPath = strval($this->getTempFile()); |
85
|
|
|
$this->readSourceToLocalFile($name, $content, $localPath); |
86
|
|
|
return $localPath; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function initTempFile(): void |
90
|
|
|
{ |
91
|
|
|
// beware! it immediately creates that temporary file |
92
|
|
|
$this->tempFile = tempnam(sys_get_temp_dir(), 'MimeSrcTest'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getTempFile(): ?string |
96
|
|
|
{ |
97
|
|
|
return $this->tempFile; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: