1
|
|
|
<?php |
2
|
|
|
namespace Upyun\Tests; |
3
|
|
|
use Upyun\Config; |
4
|
|
|
use Upyun\Upyun; |
5
|
|
|
|
6
|
|
|
class UpyunTest extends \PHPUnit_Framework_TestCase{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var Upyun |
10
|
|
|
*/ |
11
|
|
|
public static $upyun; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
protected static $taskId; |
15
|
|
|
|
16
|
|
|
protected static $tempFilePath; |
17
|
|
|
|
18
|
|
|
public static function setUpBeforeClass() { |
19
|
|
|
$config = new Config(BUCKET, USER_NAME, PWD); |
20
|
|
|
$config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw='); |
21
|
|
|
$config->processNotifyUrl = 'http://localhost:9999'; |
22
|
|
|
self::$upyun = new Upyun($config); |
23
|
|
|
self::$tempFilePath = __DIR__ . '/assets/test.txt'; |
24
|
|
|
touch(self::$tempFilePath); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function tearDownAfterClass() { |
28
|
|
|
unlink(self::$tempFilePath); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testWriteString() { |
32
|
|
|
$filename = 'test.txt'; |
33
|
|
|
$content = 'test file content'; |
34
|
|
|
self::$upyun->write($filename, $content); |
35
|
|
|
$size = getUpyunFileSize($filename); |
36
|
|
|
$this->assertEquals($size, strlen($content)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testWriteStream() { |
40
|
|
|
$filename = 'test.jpeg'; |
41
|
|
|
$f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb'); |
42
|
|
|
if(!$f) { |
|
|
|
|
43
|
|
|
throw new \Exception('open test file failed!'); |
44
|
|
|
} |
45
|
|
|
self::$upyun->write($filename, $f); |
46
|
|
|
$size = getUpyunFileSize($filename); |
47
|
|
|
$this->assertEquals($size, PIC_SIZE); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testWriteWithAsyncProcess() { |
51
|
|
|
$filename = 'test_async.jpeg'; |
52
|
|
|
$newFilename = 'test_async.png'; |
53
|
|
|
$f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb'); |
54
|
|
|
if(!$f) { |
|
|
|
|
55
|
|
|
throw new \Exception('open test file failed!'); |
56
|
|
|
} |
57
|
|
|
$result = self::$upyun->write($filename, $f, array( |
58
|
|
|
'apps' => array( |
59
|
|
|
array( |
60
|
|
|
'name' => 'thumb', |
61
|
|
|
'x-gmkerl-thumb' => '/format/png/fw/50', |
62
|
|
|
'save_as' => $newFilename, |
63
|
|
|
) |
64
|
|
|
) |
65
|
|
|
), true); |
66
|
|
|
$size = getUpyunFileSize($filename); |
67
|
|
|
$this->assertEquals($size, PIC_SIZE); |
68
|
|
|
$this->assertEquals($result, true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testWriteWithException() { |
72
|
|
|
$fs = new Upyun(new Config(BUCKET, USER_NAME, 'error-password')); |
73
|
|
|
try { |
74
|
|
|
$fs->write('test.txt', 'test file content'); |
75
|
|
|
} catch(\Exception $e) { |
|
|
|
|
76
|
|
|
return ; |
77
|
|
|
} |
78
|
|
|
throw new \Exception('should get sign error.'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @depends testWriteString |
83
|
|
|
*/ |
84
|
|
|
public function testReadFile() { |
85
|
|
|
$name = 'test-read.txt'; |
86
|
|
|
$str = 'test file content 2'; |
87
|
|
|
self::$upyun->write($name, $str); |
88
|
|
|
|
89
|
|
|
//读取内容写入字符串 |
90
|
|
|
$content = self::$upyun->read($name); |
91
|
|
|
$this->assertEquals($content, $str); |
92
|
|
|
|
93
|
|
|
//读取内容写入文件流 |
94
|
|
|
$this->assertTrue(self::$upyun->read($name, fopen(self::$tempFilePath, 'wb'))); |
95
|
|
|
$this->assertEquals($str, file_get_contents(self::$tempFilePath)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @depends testWriteString |
100
|
|
|
* @depends testReadFile |
101
|
|
|
*/ |
102
|
|
|
public function testDeleteFile() { |
103
|
|
|
self::$upyun->write('test-delete.txt', 'test file content 3'); |
104
|
|
|
$r = self::$upyun->delete('test-delete.txt'); |
|
|
|
|
105
|
|
|
try { |
106
|
|
|
self::$upyun->read('test-delete.txt'); |
107
|
|
|
} catch(\Exception $e) { |
|
|
|
|
108
|
|
|
return ; |
109
|
|
|
} |
110
|
|
|
throw new \Exception('delete file failed'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @expectedException \Exception |
115
|
|
|
*/ |
116
|
|
|
public function testDeleteNotExistsFile() { |
117
|
|
|
self::$upyun->delete('not-exists-test.txt'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
*/ |
122
|
|
|
public function testHas() { |
123
|
|
|
$name = 'test-has.txt'; |
124
|
|
|
self::$upyun->write($name, 'test file content 4'); |
125
|
|
|
$this->assertEquals(self::$upyun->has($name), true); |
126
|
|
|
self::$upyun->delete($name); |
127
|
|
|
sleep(5); |
128
|
|
|
$this->assertEquals(self::$upyun->has($name), false); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @depends testWriteString |
133
|
|
|
* @depends testDeleteFile |
134
|
|
|
*/ |
135
|
|
|
public function testInfo() { |
136
|
|
|
self::$upyun->write('test-info.txt', 'test file content 4'); |
137
|
|
|
$info = self::$upyun->info('test-info.txt'); |
138
|
|
|
$this->assertEquals($info['x-upyun-file-type'], 'file'); |
139
|
|
|
$this->assertEquals($info['x-upyun-file-size'], 19); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
*/ |
144
|
|
|
public function testCreateDir() { |
145
|
|
|
self::$upyun->createDir('/test-dir'); |
146
|
|
|
$this->assertEquals(self::$upyun->has('/test-dir'), true); |
147
|
|
|
self::$upyun->createDir('/test-dir2/'); |
148
|
|
|
$this->assertEquals(self::$upyun->has('/test-dir2'), true); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testReadDir() { |
152
|
|
|
$list = self::$upyun->read('/test-dir2/'); |
153
|
|
|
$this->assertEquals($list['is_end'], true); |
154
|
|
|
self::$upyun->write('/test-dir2/test.txt', 'test file content 5'); |
155
|
|
|
$list = self::$upyun->read('/test-dir2/'); |
156
|
|
|
$this->assertEquals($list['is_end'], true); |
157
|
|
|
$this->assertEquals(count($list['files']), 1); |
158
|
|
|
$file = $list['files'][0]; |
159
|
|
|
$this->assertEquals($file['name'], 'test.txt'); |
160
|
|
|
$this->assertEquals($file['type'], 'N'); |
161
|
|
|
$this->assertEquals($file['size'], 19); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @depends testCreateDir |
166
|
|
|
*/ |
167
|
|
|
public function testDeleteDir() { |
168
|
|
|
$result = self::$upyun->createDir('/test-delete-dir'); |
169
|
|
|
$this->assertEquals($result, true); |
170
|
|
|
sleep(5); |
171
|
|
|
$result = self::$upyun->deleteDir('/test-delete-dir'); |
172
|
|
|
$this->assertEquals($result, true); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testUsage() { |
176
|
|
|
$size = self::$upyun->usage(); |
177
|
|
|
$this->assertTrue($size > 0); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testPurge() { |
181
|
|
|
$urls = self::$upyun->purge(getFileUrl('test.txt')); |
182
|
|
|
$this->assertTrue(empty($urls)); |
183
|
|
|
|
184
|
|
|
$invalidUrl = 'http://xxxx.b0.xxxxxxxx-upyun.com/test.txt'; |
185
|
|
|
$urls = self::$upyun->purge($invalidUrl); |
186
|
|
|
$this->assertTrue(count($urls) === 1); |
187
|
|
|
$this->assertTrue($urls[0] === $invalidUrl); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testProcess() { |
191
|
|
|
$source = 'php-sdk-sample.mp4'; |
192
|
|
|
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r')); |
193
|
|
|
$result = self::$upyun->process($source, array( |
194
|
|
|
array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4') |
195
|
|
|
)); |
196
|
|
|
$this->assertTrue(strlen($result[0]) === 32); |
197
|
|
|
self::$taskId = $result[0]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @depends testProcess |
202
|
|
|
*/ |
203
|
|
|
public function testQueryProcessStatus() { |
204
|
|
|
sleep(5); |
205
|
|
|
$status = self::$upyun->queryProcessStatus(array(self::$taskId)); |
206
|
|
|
$this->assertTrue(array_key_exists(self::$taskId, $status)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @depends testProcess |
211
|
|
|
*/ |
212
|
|
|
public function testQueryProcessResult() { |
213
|
|
|
sleep(5); |
214
|
|
|
$result = self::$upyun->queryProcessResult(array(self::$taskId)); |
215
|
|
|
$this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4'); |
216
|
|
|
$this->assertTrue($result[self::$taskId]['status_code'] === 200); |
217
|
|
|
} |
218
|
|
|
} |