PfopTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace Qiniu\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use Qiniu\Auth;
  5. use Qiniu\Processing\PersistentFop;
  6. use Qiniu\Storage\UploadManager;
  7. //use Qiniu\Region;
  8. //use Qiniu\Config;
  9. class PfopTest extends TestCase
  10. {
  11. /**
  12. * @var Auth
  13. */
  14. private static $testAuth;
  15. private static $bucketName;
  16. /**
  17. * @beforeClass
  18. */
  19. public static function prepareEnvironment()
  20. {
  21. global $bucketName;
  22. global $testAuth;
  23. self::$bucketName = $bucketName;
  24. self::$testAuth = $testAuth;
  25. }
  26. private static function getConfig()
  27. {
  28. // use this func to test in test env
  29. // `null` means to use production env
  30. return null;
  31. }
  32. public function testPfopExecuteAndStatusWithSingleFop()
  33. {
  34. global $testAuth;
  35. $bucket = 'testres';
  36. $key = 'sintel_trailer.mp4';
  37. $pfop = new PersistentFop($testAuth, self::getConfig());
  38. $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240';
  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. public function testPfopExecuteAndStatusWithMultipleFops()
  46. {
  47. global $testAuth;
  48. $bucket = 'testres';
  49. $key = 'sintel_trailer.mp4';
  50. $fops = array(
  51. 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240',
  52. 'vframe/jpg/offset/7/w/480/h/360',
  53. );
  54. $pfop = new PersistentFop($testAuth, self::getConfig());
  55. list($id, $error) = $pfop->execute($bucket, $key, $fops);
  56. $this->assertNull($error);
  57. list($status, $error) = $pfop->status($id);
  58. $this->assertNotNull($status);
  59. $this->assertNull($error);
  60. }
  61. private function pfopOptionsTestData()
  62. {
  63. return array(
  64. array(
  65. 'type' => null
  66. ),
  67. array(
  68. 'type' => -1
  69. ),
  70. array(
  71. 'type' => 0
  72. ),
  73. array(
  74. 'type' => 1
  75. ),
  76. array(
  77. 'type' => 2
  78. ),
  79. array(
  80. 'workflowTemplateID' => 'test-workflow'
  81. )
  82. );
  83. }
  84. public function testPfopExecuteWithOptions()
  85. {
  86. $bucket = self::$bucketName;
  87. $key = 'qiniu.png';
  88. $pfop = new PersistentFop(self::$testAuth, self::getConfig());
  89. $testCases = $this->pfopOptionsTestData();
  90. foreach ($testCases as $testCase) {
  91. $workflowTemplateID = null;
  92. $type = null;
  93. if (array_key_exists('workflowTemplateID', $testCase)) {
  94. $workflowTemplateID = $testCase['workflowTemplateID'];
  95. }
  96. if (array_key_exists('type', $testCase)) {
  97. $type = $testCase['type'];
  98. }
  99. if ($workflowTemplateID) {
  100. $fops = null;
  101. } else {
  102. $persistentEntry = \Qiniu\entry(
  103. $bucket,
  104. implode(
  105. '_',
  106. array(
  107. 'test-pfop/test-pfop-by-api',
  108. 'type',
  109. $type
  110. )
  111. )
  112. );
  113. $fops = 'avinfo|saveas/' . $persistentEntry;
  114. }
  115. list($id, $error) = $pfop->execute(
  116. $bucket,
  117. $key,
  118. $fops,
  119. null,
  120. null,
  121. false,
  122. $type,
  123. $workflowTemplateID
  124. );
  125. if (in_array($type, array(null, 0, 1))) {
  126. $this->assertNull($error);
  127. list($status, $error) = $pfop->status($id);
  128. $this->assertNotNull($status);
  129. $this->assertNull($error);
  130. if ($type == 1) {
  131. $this->assertEquals(1, $status['type']);
  132. }
  133. if ($workflowTemplateID) {
  134. // assertStringContainsString when PHPUnit >= 8.0
  135. $this->assertTrue(
  136. strpos(
  137. $status['taskFrom'],
  138. $workflowTemplateID
  139. ) !== false
  140. );
  141. }
  142. $this->assertNotEmpty($status['creationDate']);
  143. } else {
  144. $this->assertNotNull($error);
  145. }
  146. }
  147. }
  148. public function testPfopWithInvalidArgument()
  149. {
  150. $bucket = self::$bucketName;
  151. $key = 'qiniu.png';
  152. $pfop = new PersistentFop(self::$testAuth, self::getConfig());
  153. $err = null;
  154. try {
  155. $pfop->execute(
  156. $bucket,
  157. $key
  158. );
  159. } catch (\Exception $e) {
  160. $err = $e;
  161. }
  162. $this->assertNotEmpty($err);
  163. $this->assertTrue(
  164. strpos(
  165. $err->getMessage(),
  166. 'Must provide one of fops or template_id'
  167. ) !== false
  168. );
  169. }
  170. public function testPfopWithUploadPolicy()
  171. {
  172. $bucket = self::$bucketName;
  173. $testAuth = self::$testAuth;
  174. $key = 'test-pfop/upload-file';
  175. $testCases = $this->pfopOptionsTestData();
  176. foreach ($testCases as $testCase) {
  177. $workflowTemplateID = null;
  178. $type = null;
  179. if (array_key_exists('workflowTemplateID', $testCase)) {
  180. $workflowTemplateID = $testCase['workflowTemplateID'];
  181. }
  182. if (array_key_exists('type', $testCase)) {
  183. $type = $testCase['type'];
  184. }
  185. $putPolicy = array(
  186. 'persistentType' => $type
  187. );
  188. if ($workflowTemplateID) {
  189. $putPolicy['persistentWorkflowTemplateID'] = $workflowTemplateID;
  190. } else {
  191. $persistentEntry = \Qiniu\entry(
  192. $bucket,
  193. implode(
  194. '_',
  195. array(
  196. 'test-pfop/test-pfop-by-upload',
  197. 'type',
  198. $type
  199. )
  200. )
  201. );
  202. $putPolicy['persistentOps'] = 'avinfo|saveas/' . $persistentEntry;
  203. }
  204. if ($type == null) {
  205. unset($putPolicy['persistentType']);
  206. }
  207. $token = $testAuth->uploadToken(
  208. $bucket,
  209. $key,
  210. 3600,
  211. $putPolicy
  212. );
  213. $upManager = new UploadManager(self::getConfig());
  214. list($ret, $error) = $upManager->putFile(
  215. $token,
  216. $key,
  217. __file__,
  218. null,
  219. 'text/plain',
  220. true
  221. );
  222. if (in_array($type, array(null, 0, 1))) {
  223. $this->assertNull($error);
  224. $this->assertNotEmpty($ret['persistentId']);
  225. $id = $ret['persistentId'];
  226. } else {
  227. $this->assertNotNull($error);
  228. return;
  229. }
  230. $pfop = new PersistentFop($testAuth, self::getConfig());
  231. list($status, $error) = $pfop->status($id);
  232. $this->assertNotNull($status);
  233. $this->assertNull($error);
  234. if ($type == 1) {
  235. $this->assertEquals(1, $status['type']);
  236. }
  237. if ($workflowTemplateID) {
  238. // assertStringContainsString when PHPUnit >= 8.0
  239. $this->assertTrue(
  240. strpos(
  241. $status['taskFrom'],
  242. $workflowTemplateID
  243. ) !== false
  244. );
  245. }
  246. $this->assertNotEmpty($status['creationDate']);
  247. }
  248. }
  249. public function testMkzip()
  250. {
  251. $bucket = self::$bucketName;
  252. $key = 'php-logo.png';
  253. $pfop = new PersistentFop(self::$testAuth, null);
  254. $url1 = 'http://phpsdk.qiniudn.com/php-logo.png';
  255. $url2 = 'http://phpsdk.qiniudn.com/php-sdk.html';
  256. $zipKey = 'test.zip';
  257. $fops = 'mkzip/2/url/' . \Qiniu\base64_urlSafeEncode($url1);
  258. $fops .= '/url/' . \Qiniu\base64_urlSafeEncode($url2);
  259. $fops .= '|saveas/' . \Qiniu\base64_urlSafeEncode("$bucket:$zipKey");
  260. list($id, $error) = $pfop->execute($bucket, $key, $fops);
  261. $this->assertNull($error);
  262. list($status, $error) = $pfop->status($id);
  263. $this->assertNotNull($status);
  264. $this->assertNull($error);
  265. }
  266. }