UpyunTest.php 7.4 KB

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