Passed
Pull Request — master (#54)
by
unknown
10:38
created

UpyunTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 279
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 3

23 Methods

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