| 1 | <?php |
||||
| 2 | namespace Upyun\Tests; |
||||
| 3 | |||||
| 4 | use Upyun\Config; |
||||
| 5 | use Upyun\Upyun; |
||||
| 6 | use PHPUnit\Framework\TestCase; |
||||
| 7 | |||||
| 8 | class UpyunTest extends TestCase |
||||
| 9 | { |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * @var Upyun |
||||
| 13 | */ |
||||
| 14 | public static $upyun; |
||||
| 15 | |||||
| 16 | |||||
| 17 | protected static $taskId; |
||||
| 18 | |||||
| 19 | protected static $tempFilePath; |
||||
| 20 | |||||
| 21 | public static function setUpBeforeClass(): void |
||||
| 22 | { |
||||
| 23 | $config = new Config(BUCKET, USER_NAME, PWD); |
||||
| 24 | $config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw='); |
||||
| 25 | $config->processNotifyUrl = 'http://localhost:9999'; |
||||
| 26 | self::$upyun = new Upyun($config); |
||||
| 27 | self::$tempFilePath = __DIR__ . '/assets/test.txt'; |
||||
| 28 | touch(self::$tempFilePath); |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | public static function tearDownAfterClass(): void |
||||
| 32 | { |
||||
| 33 | unlink(self::$tempFilePath); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function testWriteString() |
||||
| 37 | { |
||||
| 38 | $filename = '/中文/测试 +.txt'; |
||||
| 39 | $content = 'test file content'; |
||||
| 40 | self::$upyun->write($filename, $content); |
||||
| 41 | $size = getUpyunFileSize($filename); |
||||
| 42 | $this->assertEquals($size, strlen($content)); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | public function testWriteStream() |
||||
| 46 | { |
||||
| 47 | $filename = 'test.jpeg'; |
||||
| 48 | $f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb'); |
||||
| 49 | if (!$f) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 50 | throw new \Exception('open test file failed!'); |
||||
| 51 | } |
||||
| 52 | self::$upyun->write($filename, $f); |
||||
| 53 | $size = getUpyunFileSize($filename); |
||||
| 54 | $this->assertEquals($size, PIC_SIZE); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | public function testWriteWithAsyncProcess() |
||||
| 58 | { |
||||
| 59 | $filename = 'test_async.jpeg'; |
||||
| 60 | $newFilename = 'test_async.png'; |
||||
| 61 | $f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb'); |
||||
| 62 | if (!$f) { |
||||
|
0 ignored issues
–
show
|
|||||
| 63 | throw new \Exception('open test file failed!'); |
||||
| 64 | } |
||||
| 65 | $result = self::$upyun->write($filename, $f, array( |
||||
| 66 | 'apps' => array( |
||||
| 67 | array( |
||||
| 68 | 'name' => 'thumb', |
||||
| 69 | 'x-gmkerl-thumb' => '/format/png/fw/50', |
||||
| 70 | 'save_as' => $newFilename, |
||||
| 71 | ) |
||||
| 72 | ) |
||||
| 73 | ), true); |
||||
| 74 | $size = getUpyunFileSize($filename); |
||||
| 75 | $this->assertEquals($size, PIC_SIZE); |
||||
| 76 | $this->assertEquals($result, true); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | public function testWriteWithException() |
||||
| 80 | { |
||||
| 81 | $this->expectException(\Exception::class); |
||||
| 82 | |||||
| 83 | $fs = new Upyun(new Config(BUCKET, USER_NAME, 'error-password')); |
||||
| 84 | $fs->write('test.txt', 'test file content'); |
||||
| 85 | } |
||||
| 86 | |||||
| 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(1); |
||||
| 115 | self::$upyun->delete('test-delete.txt'); |
||||
| 116 | |||||
| 117 | $this->expectException(\Exception::class); |
||||
| 118 | self::$upyun->read('test-delete.txt'); |
||||
| 119 | } |
||||
| 120 | |||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * @expectedException \Exception |
||||
| 124 | */ |
||||
| 125 | public function testDeleteNotExistsFile() |
||||
| 126 | { |
||||
| 127 | $this->expectException(\Exception::class); |
||||
| 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 | sleep(1); |
||||
| 139 | self::$upyun->delete($name); |
||||
| 140 | sleep(1); |
||||
| 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(1); |
||||
| 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 | /** |
||||
| 208 | * @depends testWriteString |
||||
| 209 | */ |
||||
| 210 | public function testCopy() |
||||
| 211 | { |
||||
| 212 | $source = 'test-copy.txt'; |
||||
| 213 | $target = 'test-copy-target.txt'; |
||||
| 214 | self::$upyun->write($source, 'test file content 6'); |
||||
| 215 | sleep(1); |
||||
| 216 | self::$upyun->copy($source, $target); |
||||
| 217 | $this->assertEquals(self::$upyun->has($target), true); |
||||
| 218 | } |
||||
| 219 | |||||
| 220 | /** |
||||
| 221 | * @depends testWriteString |
||||
| 222 | */ |
||||
| 223 | public function testMove() |
||||
| 224 | { |
||||
| 225 | $source = 'test-move.txt'; |
||||
| 226 | $target = 'test-move-target.txt'; |
||||
| 227 | self::$upyun->write($source, 'test file content 7'); |
||||
| 228 | sleep(1); |
||||
| 229 | self::$upyun->move($source, $target); |
||||
| 230 | $this->assertEquals(self::$upyun->has($source), false); |
||||
| 231 | $this->assertEquals(self::$upyun->has($target), true); |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | public function testPurge() |
||||
| 235 | { |
||||
| 236 | $urls = self::$upyun->purge(getFileUrl('test.txt')); |
||||
| 237 | $this->assertTrue(empty($urls)); |
||||
| 238 | |||||
| 239 | $invalidUrl = 'http://xxxx.b0.xxxxxxxx-upyun.com/test.txt'; |
||||
| 240 | $urls = self::$upyun->purge($invalidUrl); |
||||
| 241 | $this->assertTrue(count($urls) === 1); |
||||
| 242 | $this->assertTrue($urls[0] === $invalidUrl); |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | public function testProcess() |
||||
| 246 | { |
||||
| 247 | $source = 'php-sdk-sample.mp4'; |
||||
| 248 | self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r')); |
||||
| 249 | $result = self::$upyun->process(array( |
||||
| 250 | array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4') |
||||
| 251 | ), Upyun::$PROCESS_TYPE_MEDIA, $source); |
||||
| 252 | $this->assertTrue(strlen($result[0]) === 32); |
||||
| 253 | self::$taskId = $result[0]; |
||||
| 254 | |||||
| 255 | // test zip |
||||
| 256 | $result2 = self::$upyun->process(array(array( |
||||
| 257 | 'sources' => ['./php-sdk-sample.mp4'], |
||||
| 258 | 'save_as' => '/php-sdk-sample-mp4.zip' |
||||
| 259 | )), Upyun::$PROCESS_TYPE_ZIP); |
||||
| 260 | $this->assertTrue(strlen($result2[0]) === 32); |
||||
| 261 | } |
||||
| 262 | |||||
| 263 | /** |
||||
| 264 | * @depends testProcess |
||||
| 265 | */ |
||||
| 266 | public function testQueryProcessStatus() |
||||
| 267 | { |
||||
| 268 | sleep(1); |
||||
| 269 | $status = self::$upyun->queryProcessStatus(array(self::$taskId)); |
||||
| 270 | $this->assertTrue(array_key_exists(self::$taskId, $status)); |
||||
|
0 ignored issues
–
show
It seems like
$status can also be of type boolean; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 271 | } |
||||
| 272 | |||||
| 273 | /** |
||||
| 274 | * @depends testProcess |
||||
| 275 | */ |
||||
| 276 | public function testQueryProcessResult() |
||||
| 277 | { |
||||
| 278 | sleep(30); |
||||
| 279 | $result = self::$upyun->queryProcessResult(array(self::$taskId)); |
||||
| 280 | $this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4'); |
||||
| 281 | $this->assertTrue($result[self::$taskId]['status_code'] === 200); |
||||
| 282 | } |
||||
| 283 | |||||
| 284 | public function testAvMeta() |
||||
| 285 | { |
||||
| 286 | $source = 'php-sdk-sample.mp4'; |
||||
| 287 | self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r')); |
||||
| 288 | $result = self::$upyun->avMeta('/php-sdk-sample.mp4'); |
||||
| 289 | $this->assertTrue(count($result) === 2); |
||||
| 290 | $this->assertTrue($result['streams'][0]['type'] === 'video'); |
||||
| 291 | } |
||||
| 292 | |||||
| 293 | public function testSnapshot() |
||||
| 294 | { |
||||
| 295 | sleep(1); |
||||
| 296 | $source = 'php-sdk-sample.mp4'; |
||||
| 297 | self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r')); |
||||
| 298 | $result = self::$upyun->snapshot('/php-sdk-sample.mp4', '/snapshot.jpg', '00:00:01', '720x480', 'jpg'); |
||||
| 299 | $this->assertTrue($result['status_code'] === 200); |
||||
| 300 | } |
||||
| 301 | |||||
| 302 | public function testParallelUpload() |
||||
| 303 | { |
||||
| 304 | $config = new Config(BUCKET, USER_NAME, PWD); |
||||
| 305 | $config->setUploadType('BLOCK_PARALLEL'); |
||||
| 306 | $upyun = new Upyun($config); |
||||
| 307 | $filename = 'test_parallel.jpeg'; |
||||
| 308 | $upyun->write($filename, fopen(__DIR__ . '/assets/sample.jpeg', 'rb')); |
||||
| 309 | |||||
| 310 | $size = getUpyunFileSize($filename); |
||||
| 311 | $this->assertEquals($size, PIC_SIZE); |
||||
| 312 | } |
||||
| 313 | } |
||||
| 314 |