UpyunTest::testWriteWithException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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 View Code Duplication
    public function testHas()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $name = 'test-has.txt';
137
        self::$upyun->write($name, 'test file content 4');
138
        $this->assertEquals(self::$upyun->has($name), true);
139
        sleep(5);
140
        self::$upyun->delete($name);
141
        sleep(5);
142
        $this->assertEquals(self::$upyun->has($name), false);
143
    }
144
145
    /**
146
     * @depends testWriteString
147
     * @depends testDeleteFile
148
     */
149
    public function testInfo()
150
    {
151
        self::$upyun->write('test-info.txt', 'test file content 4');
152
        $info = self::$upyun->info('test-info.txt');
153
        $this->assertEquals($info['x-upyun-file-type'], 'file');
154
        $this->assertEquals($info['x-upyun-file-size'], 19);
155
    }
156
157
    /**
158
     * @depends testInfo
159
     */
160
    public function testGetMimetype()
161
    {
162
        $type = self::$upyun->getMimetype('test-info.txt');
163
        $this->assertEquals($type, 'text/plain');
164
    }
165
166
    /**
167
     */
168
    public function testCreateDir()
169
    {
170
        self::$upyun->createDir('/test-dir');
171
        $this->assertEquals(self::$upyun->has('/test-dir'), true);
172
        self::$upyun->createDir('/test-dir2/');
173
        $this->assertEquals(self::$upyun->has('/test-dir2'), true);
174
    }
175
176
    public function testReadDir()
177
    {
178
        $list = self::$upyun->read('/test-dir2/');
179
        $this->assertEquals($list['is_end'], true);
180
        self::$upyun->write('/test-dir2/test.txt', 'test file content 5');
181
        $list = self::$upyun->read('/test-dir2/');
182
        $this->assertEquals($list['is_end'], true);
183
        $this->assertEquals(count($list['files']), 1);
184
        $file = $list['files'][0];
185
        $this->assertEquals($file['name'], 'test.txt');
186
        $this->assertEquals($file['type'], 'N');
187
        $this->assertEquals($file['size'], 19);
188
    }
189
190
    /**
191
     * @depends testCreateDir
192
     */
193 View Code Duplication
    public function testDeleteDir()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        $result = self::$upyun->createDir('/test-delete-dir');
196
        $this->assertEquals($result, true);
197
        sleep(5);
198
        $result = self::$upyun->deleteDir('/test-delete-dir');
199
        $this->assertEquals($result, true);
200
    }
201
202
    public function testUsage()
203
    {
204
        $size = self::$upyun->usage();
205
        $this->assertTrue($size > 0);
206
    }
207
208
    /**
209
     * @depends testWriteString
210
     */
211 View Code Duplication
    public function testCopy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213
        $source = 'test-copy.txt';
214
        $target = 'test-copy-target.txt';
215
        self::$upyun->write($source, 'test file content 6');
216
        sleep(5);
217
        self::$upyun->copy($source, $target);
218
        $this->assertEquals(self::$upyun->has($target), true);
219
    }
220
221
    /**
222
     * @depends testWriteString
223
     */
224 View Code Duplication
    public function testMove()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
    {
226
        $source = 'test-move.txt';
227
        $target = 'test-move-target.txt';
228
        self::$upyun->write($source, 'test file content 7');
229
        sleep(5);
230
        self::$upyun->move($source, $target);
231
        $this->assertEquals(self::$upyun->has($source), false);
232
        $this->assertEquals(self::$upyun->has($target), true);
233
    }
234
235
    public function testPurge()
236
    {
237
        $urls = self::$upyun->purge(getFileUrl('test.txt'));
238
        $this->assertTrue(empty($urls));
239
240
        $invalidUrl = 'http://xxxx.b0.xxxxxxxx-upyun.com/test.txt';
241
        $urls = self::$upyun->purge($invalidUrl);
242
        $this->assertTrue(count($urls) === 1);
243
        $this->assertTrue($urls[0] === $invalidUrl);
244
    }
245
246
    public function testProcess()
247
    {
248
        $source = 'php-sdk-sample.mp4';
249
        self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
250
        $result = self::$upyun->process(array(
251
            array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4')
252
        ), Upyun::$PROCESS_TYPE_MEDIA, $source);
253
        $this->assertTrue(strlen($result[0]) === 32);
254
        self::$taskId = $result[0];
255
256
        // test zip
257
        $result2 = self::$upyun->process(array(array(
258
            'sources' => ['./php-sdk-sample.mp4'],
259
            'save_as' => '/php-sdk-sample-mp4.zip'
260
        )), Upyun::$PROCESS_TYPE_ZIP);
261
        $this->assertTrue(strlen($result2[0]) === 32);
262
    }
263
264
    /**
265
     * @depends testProcess
266
     */
267
    public function testQueryProcessStatus()
268
    {
269
        sleep(5);
270
        $status = self::$upyun->queryProcessStatus(array(self::$taskId));
271
        $this->assertTrue(array_key_exists(self::$taskId, $status));
272
    }
273
274
    /**
275
     * @depends testProcess
276
     */
277
    public function testQueryProcessResult()
278
    {
279
        sleep(5);
280
        $result = self::$upyun->queryProcessResult(array(self::$taskId));
281
        $this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4');
282
        $this->assertTrue($result[self::$taskId]['status_code'] === 200);
283
    }
284
285
    public function testAvMeta()
286
    {
287
        $source = 'php-sdk-sample.mp4';
288
        self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
289
        $result = self::$upyun->avMeta('/php-sdk-sample.mp4');
290
        $this->assertTrue(count($result) === 2);
291
        $this->assertTrue($result['streams'][0]['type'] === 'video');
292
    }
293
294
    public function testSnapshot()
295
    {
296
        sleep(5);
297
        $source = 'php-sdk-sample.mp4';
298
        self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
299
        $result = self::$upyun->snapshot('/php-sdk-sample.mp4', '/snapshot.jpg', '00:00:01', '720x480', 'jpg');
300
        $this->assertTrue($result['status_code'] === 200);
301
    }
302
303
    public function testParallelUpload()
304
    {
305
        $config = new Config(BUCKET, USER_NAME, PWD);
306
        $config->setUploadType('BLOCK_PARALLEL');
307
        $upyun = new Upyun($config);
308
        $filename = 'test_parallel.jpeg';
309
        $upyun->write($filename, fopen(__DIR__ . '/assets/sample.jpeg', 'rb'));
310
311
        $size = getUpyunFileSize($filename);
312
        $this->assertEquals($size, PIC_SIZE);
313
    }
314
}
315