-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathUpyunTest.php
314 lines (278 loc) · 9.29 KB
/
UpyunTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
namespace Upyun\Tests;
use Upyun\Config;
use Upyun\Upyun;
class UpyunTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Upyun
*/
public static $upyun;
protected static $taskId;
protected static $tempFilePath;
public static function setUpBeforeClass()
{
$config = new Config(BUCKET, USER_NAME, PWD);
$config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw=');
$config->processNotifyUrl = 'http://localhost:9999';
self::$upyun = new Upyun($config);
self::$tempFilePath = __DIR__ . '/assets/test.txt';
touch(self::$tempFilePath);
}
public static function tearDownAfterClass()
{
unlink(self::$tempFilePath);
}
public function testWriteString()
{
$filename = '/中文/测试 +.txt';
$content = 'test file content';
self::$upyun->write($filename, $content);
$size = getUpyunFileSize($filename);
$this->assertEquals($size, strlen($content));
}
public function testWriteStream()
{
$filename = 'test.jpeg';
$f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb');
if (!$f) {
throw new \Exception('open test file failed!');
}
self::$upyun->write($filename, $f);
$size = getUpyunFileSize($filename);
$this->assertEquals($size, PIC_SIZE);
}
public function testWriteWithAsyncProcess()
{
$filename = 'test_async.jpeg';
$newFilename = 'test_async.png';
$f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb');
if (!$f) {
throw new \Exception('open test file failed!');
}
$result = self::$upyun->write($filename, $f, array(
'apps' => array(
array(
'name' => 'thumb',
'x-gmkerl-thumb' => '/format/png/fw/50',
'save_as' => $newFilename,
)
)
), true);
$size = getUpyunFileSize($filename);
$this->assertEquals($size, PIC_SIZE);
$this->assertEquals($result, true);
}
public function testWriteWithException()
{
$fs = new Upyun(new Config(BUCKET, USER_NAME, 'error-password'));
try {
$fs->write('test.txt', 'test file content');
} catch (\Exception $e) {
return ;
}
throw new \Exception('should get sign error.');
}
/**
* @depends testWriteString
*/
public function testReadFile()
{
$name = 'test-read.txt';
$str = 'test file content 2';
self::$upyun->write($name, $str);
//读取内容写入字符串
$content = self::$upyun->read($name);
$this->assertEquals($content, $str);
//读取内容写入文件流
$this->assertTrue(self::$upyun->read($name, fopen(self::$tempFilePath, 'wb')));
$this->assertEquals($str, file_get_contents(self::$tempFilePath));
}
/**
* @depends testWriteString
* @depends testReadFile
*/
public function testDeleteFile()
{
self::$upyun->write('test-delete.txt', 'test file content 3');
sleep(5);
self::$upyun->delete('test-delete.txt');
try {
self::$upyun->read('test-delete.txt');
} catch (\Exception $e) {
return ;
}
throw new \Exception('delete file failed');
}
/**
* @expectedException \Exception
*/
public function testDeleteNotExistsFile()
{
self::$upyun->delete('not-exists-test.txt');
}
/**
*/
public function testHas()
{
$name = 'test-has.txt';
self::$upyun->write($name, 'test file content 4');
$this->assertEquals(self::$upyun->has($name), true);
sleep(5);
self::$upyun->delete($name);
sleep(5);
$this->assertEquals(self::$upyun->has($name), false);
}
/**
* @depends testWriteString
* @depends testDeleteFile
*/
public function testInfo()
{
self::$upyun->write('test-info.txt', 'test file content 4');
$info = self::$upyun->info('test-info.txt');
$this->assertEquals($info['x-upyun-file-type'], 'file');
$this->assertEquals($info['x-upyun-file-size'], 19);
}
/**
* @depends testInfo
*/
public function testGetMimetype()
{
$type = self::$upyun->getMimetype('test-info.txt');
$this->assertEquals($type, 'text/plain');
}
/**
*/
public function testCreateDir()
{
self::$upyun->createDir('/test-dir');
$this->assertEquals(self::$upyun->has('/test-dir'), true);
self::$upyun->createDir('/test-dir2/');
$this->assertEquals(self::$upyun->has('/test-dir2'), true);
}
public function testReadDir()
{
$list = self::$upyun->read('/test-dir2/');
$this->assertEquals($list['is_end'], true);
self::$upyun->write('/test-dir2/test.txt', 'test file content 5');
$list = self::$upyun->read('/test-dir2/');
$this->assertEquals($list['is_end'], true);
$this->assertEquals(count($list['files']), 1);
$file = $list['files'][0];
$this->assertEquals($file['name'], 'test.txt');
$this->assertEquals($file['type'], 'N');
$this->assertEquals($file['size'], 19);
}
/**
* @depends testCreateDir
*/
public function testDeleteDir()
{
$result = self::$upyun->createDir('/test-delete-dir');
$this->assertEquals($result, true);
sleep(5);
$result = self::$upyun->deleteDir('/test-delete-dir');
$this->assertEquals($result, true);
}
public function testUsage()
{
$size = self::$upyun->usage();
$this->assertTrue($size > 0);
}
/**
* @depends testWriteString
*/
public function testCopy()
{
$source = 'test-copy.txt';
$target = 'test-copy-target.txt';
self::$upyun->write($source, 'test file content 6');
sleep(5);
self::$upyun->copy($source, $target);
$this->assertEquals(self::$upyun->has($target), true);
}
/**
* @depends testWriteString
*/
public function testMove()
{
$source = 'test-move.txt';
$target = 'test-move-target.txt';
self::$upyun->write($source, 'test file content 7');
sleep(5);
self::$upyun->move($source, $target);
$this->assertEquals(self::$upyun->has($source), false);
$this->assertEquals(self::$upyun->has($target), true);
}
public function testPurge()
{
$urls = self::$upyun->purge(getFileUrl('test.txt'));
$this->assertTrue(empty($urls));
$invalidUrl = 'http://xxxx.b0.xxxxxxxx-upyun.com/test.txt';
$urls = self::$upyun->purge($invalidUrl);
$this->assertTrue(count($urls) === 1);
$this->assertTrue($urls[0] === $invalidUrl);
}
public function testProcess()
{
$source = 'php-sdk-sample.mp4';
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
$result = self::$upyun->process(array(
array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4')
), Upyun::$PROCESS_TYPE_MEDIA, $source);
$this->assertTrue(strlen($result[0]) === 32);
self::$taskId = $result[0];
// test zip
$result2 = self::$upyun->process(array(array(
'sources' => ['./php-sdk-sample.mp4'],
'save_as' => '/php-sdk-sample-mp4.zip'
)), Upyun::$PROCESS_TYPE_ZIP);
$this->assertTrue(strlen($result2[0]) === 32);
}
/**
* @depends testProcess
*/
public function testQueryProcessStatus()
{
sleep(5);
$status = self::$upyun->queryProcessStatus(array(self::$taskId));
$this->assertTrue(array_key_exists(self::$taskId, $status));
}
/**
* @depends testProcess
*/
public function testQueryProcessResult()
{
sleep(5);
$result = self::$upyun->queryProcessResult(array(self::$taskId));
$this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4');
$this->assertTrue($result[self::$taskId]['status_code'] === 200);
}
public function testAvMeta()
{
$source = 'php-sdk-sample.mp4';
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
$result = self::$upyun->avMeta('/php-sdk-sample.mp4');
$this->assertTrue(count($result) === 2);
$this->assertTrue($result['streams'][0]['type'] === 'video');
}
public function testSnapshot()
{
sleep(5);
$source = 'php-sdk-sample.mp4';
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
$result = self::$upyun->snapshot('/php-sdk-sample.mp4', '/snapshot.jpg', '00:00:01', '720x480', 'jpg');
$this->assertTrue($result['status_code'] === 200);
}
public function testParallelUpload()
{
$config = new Config(BUCKET, USER_NAME, PWD);
$config->setUploadType('BLOCK_PARALLEL');
$upyun = new Upyun($config);
$filename = 'test_parallel.jpeg';
$upyun->write($filename, fopen(__DIR__ . '/assets/sample.jpeg', 'rb'));
$size = getUpyunFileSize($filename);
$this->assertEquals($size, PIC_SIZE);
}
}