3
3
4
4
class MultipartTest extends MultipartTestBase
5
5
{
6
+ public function testConstructWithNonStringBoundary ()
7
+ {
8
+ try {
9
+ new TestMultipart (0 );
10
+
11
+ $ this ->fail ('Expected an InvalidArgumentException ' );
12
+ } catch (\InvalidArgumentException $ e ) {
13
+ $ this ->assertEquals ('$boundary is incorrectly typed ' , $ e ->getMessage ());
14
+ }
15
+ }
16
+
17
+ public function testConstructWithNonStringContentType ()
18
+ {
19
+ try {
20
+ new TestMultipart ('' , 0 );
21
+
22
+ $ this ->fail ('Expected an InvalidArgumentException ' );
23
+ } catch (\InvalidArgumentException $ e ) {
24
+ $ this ->assertEquals ('$contentType is incorrectly typed ' , $ e ->getMessage ());
25
+ }
26
+ }
27
+
28
+ public function testConstructWithEmptyContentType ()
29
+ {
30
+ try {
31
+ new TestMultipart ('' , '' );
32
+
33
+ $ this ->fail ('Expected an InvalidArgumentException ' );
34
+ } catch (\InvalidArgumentException $ e ) {
35
+ $ this ->assertEquals ('$contentType must be non-empty ' , $ e ->getMessage ());
36
+ }
37
+ }
38
+
6
39
public function testAddWhenFinished ()
7
40
{
8
41
$ multipart = new TestMultipart ();
@@ -17,6 +50,19 @@ public function testAddWhenFinished()
17
50
}
18
51
}
19
52
53
+ public function testAddInvalidType ()
54
+ {
55
+ $ multipart = new TestMultipart ();
56
+
57
+ try {
58
+ $ multipart ->add (0 );
59
+
60
+ $ this ->fail ('Expected an InvalidArgumentException ' );
61
+ } catch (\InvalidArgumentException $ e ) {
62
+ $ this ->assertEquals ('non-supported part type: integer ' , $ e ->getMessage ());
63
+ }
64
+ }
65
+
20
66
public function testReadBeforeFinish ()
21
67
{
22
68
$ multipart = new TestMultipart ();
@@ -45,6 +91,40 @@ public function testBufferBeforeFinish()
45
91
}
46
92
}
47
93
94
+ public function testReadNonIntegerLength ()
95
+ {
96
+ $ multipart = new TestMultipart ('test-boundary ' );
97
+ $ multipart ->add ('Hello World ' );
98
+ $ multipart ->finish ();
99
+
100
+ try {
101
+ $ multipart ->read ('' );
102
+
103
+ $ this ->fail ('Expected an InvalidArgumentException ' );
104
+ } catch (\InvalidArgumentException $ e ) {
105
+ $ this ->assertEquals ('$length is incorrectly typed ' , $ e ->getMessage ());
106
+ }
107
+ }
108
+
109
+ public function testReadNonPositiveLength ()
110
+ {
111
+ $ multipart = new TestMultipart ('test-boundary ' );
112
+ $ multipart ->add ('Hello World ' );
113
+ $ multipart ->finish ();
114
+
115
+ $ this ->assertEquals ('' , $ multipart ->read (0 ));
116
+ $ this ->assertEquals ('' , $ multipart ->read (-1 ));
117
+
118
+ $ expected = "Hello World--test-boundary-- \r\n" ;
119
+
120
+ $ result = '' ;
121
+ while ($ data = $ multipart ->read (20 )) {
122
+ $ result .= $ data ;
123
+ }
124
+
125
+ $ this ->assertEquals ($ expected , $ result );
126
+ }
127
+
48
128
public function testReadEmpty ()
49
129
{
50
130
$ multipart = new TestMultipart ('test-boundary ' );
@@ -211,13 +291,17 @@ public function testContentTypeWithGeneratedBoundary()
211
291
} else {
212
292
$ this ->assertRegExp ($ expected , $ multipart ->getContentType ());
213
293
}
294
+
295
+ $ boundary = $ multipart ->getBoundary ();
296
+ $ this ->assertEquals ('multipart/test; boundary= ' . $ boundary , $ multipart ->getContentType ());
214
297
}
215
298
216
299
public function testContentTypeWithCustomBoundary ()
217
300
{
218
301
$ multipart = new TestMultipart ('test-boundary ' );
219
302
220
303
$ this ->assertEquals ('multipart/test; boundary=test-boundary ' , $ multipart ->getContentType ());
304
+ $ this ->assertEquals ('test-boundary ' , $ multipart ->getBoundary ());
221
305
}
222
306
223
307
public function testContentLength ()
@@ -306,6 +390,44 @@ public function testContentLengthMixedNoContentLengthsGiven()
306
390
fclose ($ resource );
307
391
}
308
392
393
+ public function testBufferNonIntegerLength ()
394
+ {
395
+ $ multipart = new TestMultipart ('test-boundary ' );
396
+ $ multipart ->add ('Hello World ' );
397
+ $ multipart ->finish ();
398
+
399
+ try {
400
+ $ multipart ->buffer ('' );
401
+
402
+ $ this ->fail ('Expected an InvalidArgumentException ' );
403
+ } catch (\InvalidArgumentException $ e ) {
404
+ $ this ->assertEquals ('$bufferSize is incorrectly typed ' , $ e ->getMessage ());
405
+ }
406
+ }
407
+
408
+ public function testBufferNonPositiveLength ()
409
+ {
410
+ $ multipart = new TestMultipart ('test-boundary ' );
411
+ $ multipart ->add ('Hello World ' );
412
+ $ multipart ->finish ();
413
+
414
+ try {
415
+ $ multipart ->buffer (0 );
416
+
417
+ $ this ->fail ('Expected an InvalidArgumentException ' );
418
+ } catch (\InvalidArgumentException $ e ) {
419
+ $ this ->assertEquals ('$bufferSize <= 0 ' , $ e ->getMessage ());
420
+ }
421
+
422
+ try {
423
+ $ multipart ->buffer (-1 );
424
+
425
+ $ this ->fail ('Expected an InvalidArgumentException ' );
426
+ } catch (\InvalidArgumentException $ e ) {
427
+ $ this ->assertEquals ('$bufferSize <= 0 ' , $ e ->getMessage ());
428
+ }
429
+ }
430
+
309
431
public function testBuffer ()
310
432
{
311
433
$ multipart = new TestMultipart ('test-boundary ' );
@@ -315,12 +437,14 @@ public function testBuffer()
315
437
$ multipart ->add ($ s );
316
438
$ expected .= $ s ;
317
439
$ this ->assertEquals ($ i === 0 , $ multipart ->isBuffered ());
440
+ $ this ->assertFalse ($ multipart ->isFinished ());
318
441
}
319
442
320
443
$ resource = fopen (__FILE__ , 'rb ' );
321
444
$ multipart ->add ($ resource );
322
445
$ expected .= file_get_contents (__FILE__ );
323
446
$ this ->assertFalse ($ multipart ->isBuffered ());
447
+ $ this ->assertFalse ($ multipart ->isFinished ());
324
448
325
449
$ resource2 = fopen (__FILE__ , 'rb ' );
326
450
$ callable = function ($ length ) use ($ resource2 ) {
@@ -330,17 +454,20 @@ public function testBuffer()
330
454
$ multipart ->add ($ callable );
331
455
$ expected .= file_get_contents (__FILE__ );
332
456
$ this ->assertFalse ($ multipart ->isBuffered ());
457
+ $ this ->assertFalse ($ multipart ->isFinished ());
333
458
334
459
for ($ i = 0 ; $ i < 100 ; $ i ++) {
335
460
$ s = "This is test line $ i \n" ;
336
461
$ multipart ->add ($ s );
337
462
$ expected .= $ s ;
338
463
$ this ->assertFalse ($ multipart ->isBuffered ());
464
+ $ this ->assertFalse ($ multipart ->isFinished ());
339
465
}
340
466
341
467
$ multipart ->finish ();
342
468
$ expected .= "--test-boundary-- \r\n" ;
343
469
$ this ->assertFalse ($ multipart ->isBuffered ());
470
+ $ this ->assertTrue ($ multipart ->isFinished ());
344
471
345
472
$ result = $ multipart ->buffer ();
346
473
$ this ->assertTrue ($ multipart ->isBuffered ());
@@ -380,12 +507,14 @@ public function testToString()
380
507
$ multipart ->add ($ s );
381
508
$ expected .= $ s ;
382
509
$ this ->assertEquals ($ i === 0 , $ multipart ->isBuffered ());
510
+ $ this ->assertFalse ($ multipart ->isFinished ());
383
511
}
384
512
385
513
$ resource = fopen (__FILE__ , 'rb ' );
386
514
$ multipart ->add ($ resource );
387
515
$ expected .= file_get_contents (__FILE__ );
388
516
$ this ->assertFalse ($ multipart ->isBuffered ());
517
+ $ this ->assertFalse ($ multipart ->isFinished ());
389
518
390
519
$ resource2 = fopen (__FILE__ , 'rb ' );
391
520
$ callable = function ($ length ) use ($ resource2 ) {
@@ -395,17 +524,20 @@ public function testToString()
395
524
$ multipart ->add ($ callable );
396
525
$ expected .= file_get_contents (__FILE__ );
397
526
$ this ->assertFalse ($ multipart ->isBuffered ());
527
+ $ this ->assertFalse ($ multipart ->isFinished ());
398
528
399
529
for ($ i = 0 ; $ i < 100 ; $ i ++) {
400
530
$ s = "This is test line $ i \n" ;
401
531
$ multipart ->add ($ s );
402
532
$ expected .= $ s ;
403
533
$ this ->assertFalse ($ multipart ->isBuffered ());
534
+ $ this ->assertFalse ($ multipart ->isFinished ());
404
535
}
405
536
406
537
$ multipart ->finish ();
407
538
$ expected .= "--test-boundary-- \r\n" ;
408
539
$ this ->assertFalse ($ multipart ->isBuffered ());
540
+ $ this ->assertTrue ($ multipart ->isFinished ());
409
541
410
542
$ result = (string ) $ multipart ;
411
543
$ this ->assertTrue ($ multipart ->isBuffered ());
@@ -445,12 +577,14 @@ public function testToStringNoFinish()
445
577
$ multipart ->add ($ s );
446
578
$ expected .= $ s ;
447
579
$ this ->assertEquals ($ i === 0 , $ multipart ->isBuffered ());
580
+ $ this ->assertFalse ($ multipart ->isFinished ());
448
581
}
449
582
450
583
$ resource = fopen (__FILE__ , 'rb ' );
451
584
$ multipart ->add ($ resource );
452
585
$ expected .= file_get_contents (__FILE__ );
453
586
$ this ->assertFalse ($ multipart ->isBuffered ());
587
+ $ this ->assertFalse ($ multipart ->isFinished ());
454
588
455
589
$ resource2 = fopen (__FILE__ , 'rb ' );
456
590
$ callable = function ($ length ) use ($ resource2 ) {
@@ -460,15 +594,19 @@ public function testToStringNoFinish()
460
594
$ multipart ->add ($ callable );
461
595
$ expected .= file_get_contents (__FILE__ );
462
596
$ this ->assertFalse ($ multipart ->isBuffered ());
597
+ $ this ->assertFalse ($ multipart ->isFinished ());
463
598
464
599
for ($ i = 0 ; $ i < 100 ; $ i ++) {
465
600
$ s = "This is test line $ i \n" ;
466
601
$ multipart ->add ($ s );
467
602
$ expected .= $ s ;
603
+ $ this ->assertFalse ($ multipart ->isBuffered ());
604
+ $ this ->assertFalse ($ multipart ->isFinished ());
468
605
}
469
606
470
607
$ result = (string ) $ multipart ;
471
608
$ this ->assertTrue ($ multipart ->isBuffered ());
609
+ $ this ->assertFalse ($ multipart ->isFinished ());
472
610
473
611
fclose ($ resource );
474
612
fclose ($ resource2 );
@@ -483,9 +621,13 @@ public function testToStringNoFinish()
483
621
484
622
class TestMultipart extends Multipart
485
623
{
486
- public function __construct ($ boundary = '' )
624
+ /**
625
+ * @param string|int $boundary
626
+ * @param string|int $contentType
627
+ */
628
+ public function __construct ($ boundary = '' , $ contentType = 'multipart/test ' )
487
629
{
488
- parent ::__construct ($ boundary , ' multipart/test ' );
630
+ parent ::__construct ($ boundary , $ contentType );
489
631
}
490
632
491
633
public function add ($ content , $ length = -1 )
0 commit comments