PfopTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Qiniu\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use Qiniu\Processing\PersistentFop;
  5. use Qiniu\Storage\UploadManager;
  6. use Qiniu\Region;
  7. use Qiniu\Config;
  8. class PfopTest extends TestCase
  9. {
  10. private static function getConfig()
  11. {
  12. // use this func to test in test env
  13. // `null` means to use production env
  14. return null;
  15. }
  16. public function testPfopExecuteAndStatusWithSingleFop()
  17. {
  18. global $testAuth;
  19. $bucket = 'testres';
  20. $key = 'sintel_trailer.mp4';
  21. $pfop = new PersistentFop($testAuth, self::getConfig());
  22. $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240';
  23. list($id, $error) = $pfop->execute($bucket, $key, $fops);
  24. $this->assertNull($error);
  25. list($status, $error) = $pfop->status($id);
  26. $this->assertNotNull($status);
  27. $this->assertNull($error);
  28. }
  29. public function testPfopExecuteAndStatusWithMultipleFops()
  30. {
  31. global $testAuth;
  32. $bucket = 'testres';
  33. $key = 'sintel_trailer.mp4';
  34. $fops = array(
  35. 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240',
  36. 'vframe/jpg/offset/7/w/480/h/360',
  37. );
  38. $pfop = new PersistentFop($testAuth, self::getConfig());
  39. list($id, $error) = $pfop->execute($bucket, $key, $fops);
  40. $this->assertNull($error);
  41. list($status, $error) = $pfop->status($id);
  42. $this->assertNotNull($status);
  43. $this->assertNull($error);
  44. }
  45. private function pfopTypeTestData()
  46. {
  47. return array(
  48. array(
  49. 'type' => null
  50. ),
  51. array(
  52. 'type' => -1
  53. ),
  54. array(
  55. 'type' => 0
  56. ),
  57. array(
  58. 'type' => 1
  59. ),
  60. array(
  61. 'type' => 2
  62. )
  63. );
  64. }
  65. public function testPfopWithIdleTimeType()
  66. {
  67. global $testAuth;
  68. $bucket = 'testres';
  69. $key = 'sintel_trailer.mp4';
  70. $persistentEntry = \Qiniu\entry($bucket, 'test-pfop-type_1');
  71. $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/' . $persistentEntry;
  72. $pfop = new PersistentFop($testAuth, self::getConfig());
  73. $testCases = $this->pfopTypeTestData();
  74. foreach ($testCases as $testCase) {
  75. list($id, $error) = $pfop->execute(
  76. $bucket,
  77. $key,
  78. $fops,
  79. null,
  80. null,
  81. false,
  82. $testCase['type']
  83. );
  84. if (in_array($testCase['type'], array(null, 0, 1))) {
  85. $this->assertNull($error);
  86. list($status, $error) = $pfop->status($id);
  87. $this->assertNotNull($status);
  88. $this->assertNull($error);
  89. if ($testCase['type'] == 1) {
  90. $this->assertEquals(1, $status['type']);
  91. }
  92. $this->assertNotEmpty($status['creationDate']);
  93. } else {
  94. $this->assertNotNull($error);
  95. }
  96. }
  97. }
  98. public function testPfopByUploadPolicy()
  99. {
  100. global $testAuth;
  101. $bucket = 'testres';
  102. $key = 'sintel_trailer.mp4';
  103. $persistentEntry = \Qiniu\entry($bucket, 'test-pfop-type_1');
  104. $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/' . $persistentEntry;
  105. $testCases = $this->pfopTypeTestData();
  106. foreach ($testCases as $testCase) {
  107. $putPolicy = array(
  108. 'persistentOps' => $fops,
  109. 'persistentType' => $testCase['type']
  110. );
  111. if ($testCase['type'] == null) {
  112. unset($putPolicy['persistentType']);
  113. }
  114. $token = $testAuth->uploadToken(
  115. $bucket,
  116. $key,
  117. 3600,
  118. $putPolicy
  119. );
  120. $upManager = new UploadManager(self::getConfig());
  121. list($ret, $error) = $upManager->putFile(
  122. $token,
  123. $key,
  124. __file__,
  125. null,
  126. 'text/plain',
  127. true
  128. );
  129. if (in_array($testCase['type'], array(null, 0, 1))) {
  130. $this->assertNull($error);
  131. $this->assertNotEmpty($ret['persistentId']);
  132. $id = $ret['persistentId'];
  133. } else {
  134. $this->assertNotNull($error);
  135. return;
  136. }
  137. $pfop = new PersistentFop($testAuth, self::getConfig());
  138. list($status, $error) = $pfop->status($id);
  139. $this->assertNotNull($status);
  140. $this->assertNull($error);
  141. if ($testCase['type'] == 1) {
  142. $this->assertEquals(1, $status['type']);
  143. }
  144. $this->assertNotEmpty($status['creationDate']);
  145. }
  146. }
  147. public function testMkzip()
  148. {
  149. global $testAuth;
  150. $bucket = 'phpsdk';
  151. $key = 'php-logo.png';
  152. $pfop = new PersistentFop($testAuth, null);
  153. $url1 = 'http://phpsdk.qiniudn.com/php-logo.png';
  154. $url2 = 'http://phpsdk.qiniudn.com/php-sdk.html';
  155. $zipKey = 'test.zip';
  156. $fops = 'mkzip/2/url/' . \Qiniu\base64_urlSafeEncode($url1);
  157. $fops .= '/url/' . \Qiniu\base64_urlSafeEncode($url2);
  158. $fops .= '|saveas/' . \Qiniu\base64_urlSafeEncode("$bucket:$zipKey");
  159. list($id, $error) = $pfop->execute($bucket, $key, $fops);
  160. $this->assertNull($error);
  161. list($status, $error) = $pfop->status($id);
  162. $this->assertNotNull($status);
  163. $this->assertNull($error);
  164. }
  165. }