Passed
Push — master ( e39da6...9f5525 )
by sabaku
04:31
created

UpyunTest::testSnapshot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
        self::$upyun->delete('test-delete.txt');
115
        try {
116
            self::$upyun->read('test-delete.txt');
117
        } catch (\Exception $e) {
118
            return ;
119
        }
120
        throw new \Exception('delete file failed');
121
    }
122
123
    /**
124
     * @expectedException \Exception
125
     */
126
    public function testDeleteNotExistsFile()
127
    {
128
        self::$upyun->delete('not-exists-test.txt');
129
    }
130
131
    /**
132
     */
133
    public function testHas()
134
    {
135
        $name = 'test-has.txt';
136
        self::$upyun->write($name, 'test file content 4');
137
        $this->assertEquals(self::$upyun->has($name), true);
138
        self::$upyun->delete($name);
139
        sleep(5);
140
        $this->assertEquals(self::$upyun->has($name), false);
141
    }
142
143
    /**
144
     * @depends testWriteString
145
     * @depends testDeleteFile
146
     */
147
    public function testInfo()
148
    {
149
        self::$upyun->write('test-info.txt', 'test file content 4');
150
        $info = self::$upyun->info('test-info.txt');
151
        $this->assertEquals($info['x-upyun-file-type'], 'file');
152
        $this->assertEquals($info['x-upyun-file-size'], 19);
153
    }
154
155
    /**
156
     * @depends testInfo
157
     */
158
    public function testGetMimetype()
159
    {
160
        $type = self::$upyun->getMimetype('test-info.txt');
161
        $this->assertEquals($type, 'text/plain');
162
    }
163
164
    /**
165
     */
166
    public function testCreateDir()
167
    {
168
        self::$upyun->createDir('/test-dir');
169
        $this->assertEquals(self::$upyun->has('/test-dir'), true);
170
        self::$upyun->createDir('/test-dir2/');
171
        $this->assertEquals(self::$upyun->has('/test-dir2'), true);
172
    }
173
174
    public function testReadDir()
175
    {
176
        $list = self::$upyun->read('/test-dir2/');
177
        $this->assertEquals($list['is_end'], true);
178
        self::$upyun->write('/test-dir2/test.txt', 'test file content 5');
179
        $list = self::$upyun->read('/test-dir2/');
180
        $this->assertEquals($list['is_end'], true);
181
        $this->assertEquals(count($list['files']), 1);
182
        $file = $list['files'][0];
183
        $this->assertEquals($file['name'], 'test.txt');
184
        $this->assertEquals($file['type'], 'N');
185
        $this->assertEquals($file['size'], 19);
186
    }
187
188
    /**
189
     * @depends testCreateDir
190
     */
191
    public function testDeleteDir()
192
    {
193
        $result = self::$upyun->createDir('/test-delete-dir');
194
        $this->assertEquals($result, true);
195
        sleep(5);
196
        $result = self::$upyun->deleteDir('/test-delete-dir');
197
        $this->assertEquals($result, true);
198
    }
199
200
    public function testUsage()
201
    {
202
        $size = self::$upyun->usage();
203
        $this->assertTrue($size > 0);
204
    }
205
206
    public function testPurge()
207
    {
208
        $urls = self::$upyun->purge(getFileUrl('test.txt'));
209
        $this->assertTrue(empty($urls));
210
211
        $invalidUrl = 'http://xxxx.b0.xxxxxxxx-upyun.com/test.txt';
212
        $urls = self::$upyun->purge($invalidUrl);
213
        $this->assertTrue(count($urls) === 1);
214
        $this->assertTrue($urls[0] === $invalidUrl);
215
    }
216
217
    public function testProcess()
218
    {
219
        $source = 'php-sdk-sample.mp4';
220
        self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
221
        $result = self::$upyun->process(array(
222
            array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4')
223
        ), Upyun::$PROCESS_TYPE_MEDIA, $source);
224
        $this->assertTrue(strlen($result[0]) === 32);
225
        self::$taskId = $result[0];
226
227
        // test zip
228
        $result2 = self::$upyun->process(array(array(
229
            'sources' => ['./php-sdk-sample.mp4'],
230
            'save_as' => '/php-sdk-sample-mp4.zip'
231
        )), Upyun::$PROCESS_TYPE_ZIP);
232
        $this->assertTrue(strlen($result2[0]) === 32);
233
    }
234
235
    /**
236
     * @depends testProcess
237
     */
238
    public function testQueryProcessStatus()
239
    {
240
        sleep(5);
241
        $status = self::$upyun->queryProcessStatus(array(self::$taskId));
242
        $this->assertTrue(array_key_exists(self::$taskId, $status));
243
    }
244
245
    /**
246
     * @depends testProcess
247
     */
248
    public function testQueryProcessResult()
249
    {
250
        sleep(5);
251
        $result = self::$upyun->queryProcessResult(array(self::$taskId));
252
        $this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4');
253
        $this->assertTrue($result[self::$taskId]['status_code'] === 200);
254
    }
255
256
    public function testAvMeta()
257
    {
258
        $source = 'php-sdk-sample.mp4';
259
        self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
260
        $result = self::$upyun->avMeta('/php-sdk-sample.mp4');
261
        $this->assertTrue(count($result) === 2);
262
        $this->assertTrue($result['streams'][0]['type'] === 'video');
263
    }
264
265
    public function testSnapshot()
266
    {
267
        $source = 'php-sdk-sample.mp4';
268
        self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
269
        $result = self::$upyun->snapshot('/php-sdk-sample.mp4', '/snapshot.jpg', '00:00:01', '720x480', 'jpg');
270
        $this->assertTrue($result['status_code'] === 200);
271
    }
272
}
273