33
44use PHPUnit \Framework \TestCase ;
55
6+ use Qiniu \Auth ;
67use Qiniu \Processing \PersistentFop ;
78use Qiniu \Storage \UploadManager ;
8- use Qiniu \Region ;
9- use Qiniu \Config ;
9+
10+ //use Qiniu\Region;
11+ //use Qiniu\Config;
1012
1113class PfopTest extends TestCase
1214{
15+ /**
16+ * @var Auth
17+ */
18+ private static $ testAuth ;
19+
20+ private static $ bucketName ;
21+
22+ /**
23+ * @beforeClass
24+ */
25+ public static function prepareEnvironment ()
26+ {
27+ global $ bucketName ;
28+ global $ testAuth ;
29+
30+ self ::$ bucketName = $ bucketName ;
31+ self ::$ testAuth = $ testAuth ;
32+ }
33+
1334 private static function getConfig ()
1435 {
1536 // use this func to test in test env
@@ -52,7 +73,7 @@ public function testPfopExecuteAndStatusWithMultipleFops()
5273 $ this ->assertNull ($ error );
5374 }
5475
55- private function pfopTypeTestData ()
76+ private function pfopOptionsTestData ()
5677 {
5778 return array (
5879 array (
@@ -69,66 +90,147 @@ private function pfopTypeTestData()
6990 ),
7091 array (
7192 'type ' => 2
93+ ),
94+ array (
95+ 'workflowTemplateID ' => 'test-workflow '
7296 )
7397 );
7498 }
7599
76- public function testPfopWithIdleTimeType ()
100+ public function testPfopExecuteWithOptions ()
77101 {
78- global $ testAuth ;
79-
80- $ bucket = 'testres ' ;
81- $ key = 'sintel_trailer.mp4 ' ;
82- $ persistentEntry = \Qiniu \entry ($ bucket , 'test-pfop-type_1 ' );
83- $ fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/ ' . $ persistentEntry ;
84- $ pfop = new PersistentFop ($ testAuth , self ::getConfig ());
102+ $ bucket = self ::$ bucketName ;
103+ $ key = 'qiniu.png ' ;
104+ $ pfop = new PersistentFop (self ::$ testAuth , self ::getConfig ());
85105
86- $ testCases = $ this ->pfopTypeTestData ();
106+ $ testCases = $ this ->pfopOptionsTestData ();
87107
88108 foreach ($ testCases as $ testCase ) {
109+ $ workflowTemplateID = null ;
110+ $ type = null ;
111+
112+ if (array_key_exists ('workflowTemplateID ' , $ testCase )) {
113+ $ workflowTemplateID = $ testCase ['workflowTemplateID ' ];
114+ }
115+ if (array_key_exists ('type ' , $ testCase )) {
116+ $ type = $ testCase ['type ' ];
117+ }
118+
119+ if ($ workflowTemplateID ) {
120+ $ fops = null ;
121+ } else {
122+ $ persistentEntry = \Qiniu \entry (
123+ $ bucket ,
124+ implode (
125+ '_ ' ,
126+ array (
127+ 'test-pfop/test-pfop-by-api ' ,
128+ 'type ' ,
129+ $ type
130+ )
131+ )
132+ );
133+ $ fops = 'avinfo|saveas/ ' . $ persistentEntry ;
134+ }
89135 list ($ id , $ error ) = $ pfop ->execute (
90136 $ bucket ,
91137 $ key ,
92138 $ fops ,
93139 null ,
94140 null ,
95141 false ,
96- $ testCase ['type ' ]
142+ $ type ,
143+ $ workflowTemplateID
97144 );
98145
99- if (in_array ($ testCase [ ' type ' ] , array (null , 0 , 1 ))) {
146+ if (in_array ($ type , array (null , 0 , 1 ))) {
100147 $ this ->assertNull ($ error );
101148 list ($ status , $ error ) = $ pfop ->status ($ id );
102149 $ this ->assertNotNull ($ status );
103150 $ this ->assertNull ($ error );
104- if ($ testCase [ ' type ' ] == 1 ) {
151+ if ($ type == 1 ) {
105152 $ this ->assertEquals (1 , $ status ['type ' ]);
106153 }
154+ if ($ workflowTemplateID ) {
155+ // assertStringContainsString when PHPUnit >= 8.0
156+ $ this ->assertTrue (
157+ strpos (
158+ $ status ['taskFrom ' ],
159+ $ workflowTemplateID
160+ ) !== false
161+ );
162+ }
107163 $ this ->assertNotEmpty ($ status ['creationDate ' ]);
108164 } else {
109165 $ this ->assertNotNull ($ error );
110166 }
111167 }
112168 }
113169
170+ public function testPfopWithInvalidArgument ()
171+ {
172+ $ bucket = self ::$ bucketName ;
173+ $ key = 'qiniu.png ' ;
174+ $ pfop = new PersistentFop (self ::$ testAuth , self ::getConfig ());
175+ $ err = null ;
176+ try {
177+ $ pfop ->execute (
178+ $ bucket ,
179+ $ key
180+ );
181+ } catch (\Exception $ e ) {
182+ $ err = $ e ;
183+ }
184+
185+ $ this ->assertNotEmpty ($ err );
186+ $ this ->assertTrue (
187+ strpos (
188+ $ err ->getMessage (),
189+ 'Must provide one of fops or template_id '
190+ ) !== false
191+ );
192+ }
114193
115- public function testPfopByUploadPolicy ()
194+ public function testPfopWithUploadPolicy ()
116195 {
117- global $ testAuth ;
118- $ bucket = 'testres ' ;
119- $ key = 'sintel_trailer.mp4 ' ;
120- $ persistentEntry = \Qiniu \entry ($ bucket , 'test-pfop-type_1 ' );
121- $ fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/ ' . $ persistentEntry ;
196+ $ bucket = self ::$ bucketName ;
197+ $ testAuth = self ::$ testAuth ;
198+ $ key = 'test-pfop/upload-file ' ;
122199
123- $ testCases = $ this ->pfopTypeTestData ();
200+ $ testCases = $ this ->pfopOptionsTestData ();
124201
125202 foreach ($ testCases as $ testCase ) {
203+ $ workflowTemplateID = null ;
204+ $ type = null ;
205+
206+ if (array_key_exists ('workflowTemplateID ' , $ testCase )) {
207+ $ workflowTemplateID = $ testCase ['workflowTemplateID ' ];
208+ }
209+ if (array_key_exists ('type ' , $ testCase )) {
210+ $ type = $ testCase ['type ' ];
211+ }
212+
126213 $ putPolicy = array (
127- 'persistentOps ' => $ fops ,
128- 'persistentType ' => $ testCase ['type ' ]
214+ 'persistentType ' => $ type
129215 );
216+ if ($ workflowTemplateID ) {
217+ $ putPolicy ['persistentWorkflowTemplateID ' ] = $ workflowTemplateID ;
218+ } else {
219+ $ persistentEntry = \Qiniu \entry (
220+ $ bucket ,
221+ implode (
222+ '_ ' ,
223+ array (
224+ 'test-pfop/test-pfop-by-upload ' ,
225+ 'type ' ,
226+ $ type
227+ )
228+ )
229+ );
230+ $ putPolicy ['persistentOps ' ] = 'avinfo|saveas/ ' . $ persistentEntry ;
231+ }
130232
131- if ($ testCase [ ' type ' ] == null ) {
233+ if ($ type == null ) {
132234 unset($ putPolicy ['persistentType ' ]);
133235 }
134236
@@ -148,7 +250,7 @@ public function testPfopByUploadPolicy()
148250 true
149251 );
150252
151- if (in_array ($ testCase [ ' type ' ] , array (null , 0 , 1 ))) {
253+ if (in_array ($ type , array (null , 0 , 1 ))) {
152254 $ this ->assertNull ($ error );
153255 $ this ->assertNotEmpty ($ ret ['persistentId ' ]);
154256 $ id = $ ret ['persistentId ' ];
@@ -162,19 +264,27 @@ public function testPfopByUploadPolicy()
162264
163265 $ this ->assertNotNull ($ status );
164266 $ this ->assertNull ($ error );
165- if ($ testCase [ ' type ' ] == 1 ) {
267+ if ($ type == 1 ) {
166268 $ this ->assertEquals (1 , $ status ['type ' ]);
167269 }
270+ if ($ workflowTemplateID ) {
271+ // assertStringContainsString when PHPUnit >= 8.0
272+ $ this ->assertTrue (
273+ strpos (
274+ $ status ['taskFrom ' ],
275+ $ workflowTemplateID
276+ ) !== false
277+ );
278+ }
168279 $ this ->assertNotEmpty ($ status ['creationDate ' ]);
169280 }
170281 }
171282
172283 public function testMkzip ()
173284 {
174- global $ testAuth ;
175- $ bucket = 'phpsdk ' ;
285+ $ bucket = self ::$ bucketName ;
176286 $ key = 'php-logo.png ' ;
177- $ pfop = new PersistentFop ($ testAuth , null );
287+ $ pfop = new PersistentFop (self :: $ testAuth , null );
178288
179289 $ url1 = 'http://phpsdk.qiniudn.com/php-logo.png ' ;
180290 $ url2 = 'http://phpsdk.qiniudn.com/php-sdk.html ' ;
0 commit comments