Skip to content

Commit fc20a44

Browse files
committed
Fixed naming of existing curl_read method. Added more unit tests to get more code coverage
1 parent ea4be75 commit fc20a44

File tree

2 files changed

+147
-5
lines changed

2 files changed

+147
-5
lines changed

src/Robtimus/Multipart/Multipart.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ abstract class Multipart
6060
protected function __construct($boundary, $contentType)
6161
{
6262
Util::validateString($boundary, '$boundary');
63-
Util::validateNonEmptyString($contentType, $contentType);
63+
Util::validateNonEmptyString($contentType, '$contentType');
6464

6565
$this->boundary = $boundary !== '' ? $boundary : $this->generateBoundary();
6666
$this->contentType = $contentType . '; boundary=' . $this->boundary;
@@ -265,7 +265,7 @@ private function add($part, $length = -1)
265265
$this->contentLength += $length;
266266
}
267267
} else {
268-
throw new \UnexpectedValueException('non-supported part type: ' . gettype($part));
268+
throw new \InvalidArgumentException('non-supported part type: ' . gettype($part));
269269
}
270270
}
271271

@@ -352,7 +352,7 @@ private function doReadFromPart($length)
352352
* @return string a portion of this multipart object not larger than the given length,
353353
* or an empty string if nothing remains to be read.
354354
*/
355-
final public function curldoRead($ch, $fd, $length)
355+
final public function curl_read($ch, $fd, $length)
356356
{
357357
return $this->read($length);
358358
}

tests/Robtimus/Multipart/MultipartTest.php

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,39 @@
33

44
class MultipartTest extends MultipartTestBase
55
{
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+
639
public function testAddWhenFinished()
740
{
841
$multipart = new TestMultipart();
@@ -17,6 +50,19 @@ public function testAddWhenFinished()
1750
}
1851
}
1952

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+
2066
public function testReadBeforeFinish()
2167
{
2268
$multipart = new TestMultipart();
@@ -45,6 +91,40 @@ public function testBufferBeforeFinish()
4591
}
4692
}
4793

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+
48128
public function testReadEmpty()
49129
{
50130
$multipart = new TestMultipart('test-boundary');
@@ -211,13 +291,17 @@ public function testContentTypeWithGeneratedBoundary()
211291
} else {
212292
$this->assertRegExp($expected, $multipart->getContentType());
213293
}
294+
295+
$boundary = $multipart->getBoundary();
296+
$this->assertEquals('multipart/test; boundary=' . $boundary, $multipart->getContentType());
214297
}
215298

216299
public function testContentTypeWithCustomBoundary()
217300
{
218301
$multipart = new TestMultipart('test-boundary');
219302

220303
$this->assertEquals('multipart/test; boundary=test-boundary', $multipart->getContentType());
304+
$this->assertEquals('test-boundary', $multipart->getBoundary());
221305
}
222306

223307
public function testContentLength()
@@ -306,6 +390,44 @@ public function testContentLengthMixedNoContentLengthsGiven()
306390
fclose($resource);
307391
}
308392

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+
309431
public function testBuffer()
310432
{
311433
$multipart = new TestMultipart('test-boundary');
@@ -315,12 +437,14 @@ public function testBuffer()
315437
$multipart->add($s);
316438
$expected .= $s;
317439
$this->assertEquals($i === 0, $multipart->isBuffered());
440+
$this->assertFalse($multipart->isFinished());
318441
}
319442

320443
$resource = fopen(__FILE__, 'rb');
321444
$multipart->add($resource);
322445
$expected .= file_get_contents(__FILE__);
323446
$this->assertFalse($multipart->isBuffered());
447+
$this->assertFalse($multipart->isFinished());
324448

325449
$resource2 = fopen(__FILE__, 'rb');
326450
$callable = function ($length) use ($resource2) {
@@ -330,17 +454,20 @@ public function testBuffer()
330454
$multipart->add($callable);
331455
$expected .= file_get_contents(__FILE__);
332456
$this->assertFalse($multipart->isBuffered());
457+
$this->assertFalse($multipart->isFinished());
333458

334459
for ($i = 0; $i < 100; $i++) {
335460
$s = "This is test line $i\n";
336461
$multipart->add($s);
337462
$expected .= $s;
338463
$this->assertFalse($multipart->isBuffered());
464+
$this->assertFalse($multipart->isFinished());
339465
}
340466

341467
$multipart->finish();
342468
$expected .= "--test-boundary--\r\n";
343469
$this->assertFalse($multipart->isBuffered());
470+
$this->assertTrue($multipart->isFinished());
344471

345472
$result = $multipart->buffer();
346473
$this->assertTrue($multipart->isBuffered());
@@ -380,12 +507,14 @@ public function testToString()
380507
$multipart->add($s);
381508
$expected .= $s;
382509
$this->assertEquals($i === 0, $multipart->isBuffered());
510+
$this->assertFalse($multipart->isFinished());
383511
}
384512

385513
$resource = fopen(__FILE__, 'rb');
386514
$multipart->add($resource);
387515
$expected .= file_get_contents(__FILE__);
388516
$this->assertFalse($multipart->isBuffered());
517+
$this->assertFalse($multipart->isFinished());
389518

390519
$resource2 = fopen(__FILE__, 'rb');
391520
$callable = function ($length) use ($resource2) {
@@ -395,17 +524,20 @@ public function testToString()
395524
$multipart->add($callable);
396525
$expected .= file_get_contents(__FILE__);
397526
$this->assertFalse($multipart->isBuffered());
527+
$this->assertFalse($multipart->isFinished());
398528

399529
for ($i = 0; $i < 100; $i++) {
400530
$s = "This is test line $i\n";
401531
$multipart->add($s);
402532
$expected .= $s;
403533
$this->assertFalse($multipart->isBuffered());
534+
$this->assertFalse($multipart->isFinished());
404535
}
405536

406537
$multipart->finish();
407538
$expected .= "--test-boundary--\r\n";
408539
$this->assertFalse($multipart->isBuffered());
540+
$this->assertTrue($multipart->isFinished());
409541

410542
$result = (string) $multipart;
411543
$this->assertTrue($multipart->isBuffered());
@@ -445,12 +577,14 @@ public function testToStringNoFinish()
445577
$multipart->add($s);
446578
$expected .= $s;
447579
$this->assertEquals($i === 0, $multipart->isBuffered());
580+
$this->assertFalse($multipart->isFinished());
448581
}
449582

450583
$resource = fopen(__FILE__, 'rb');
451584
$multipart->add($resource);
452585
$expected .= file_get_contents(__FILE__);
453586
$this->assertFalse($multipart->isBuffered());
587+
$this->assertFalse($multipart->isFinished());
454588

455589
$resource2 = fopen(__FILE__, 'rb');
456590
$callable = function ($length) use ($resource2) {
@@ -460,15 +594,19 @@ public function testToStringNoFinish()
460594
$multipart->add($callable);
461595
$expected .= file_get_contents(__FILE__);
462596
$this->assertFalse($multipart->isBuffered());
597+
$this->assertFalse($multipart->isFinished());
463598

464599
for ($i = 0; $i < 100; $i++) {
465600
$s = "This is test line $i\n";
466601
$multipart->add($s);
467602
$expected .= $s;
603+
$this->assertFalse($multipart->isBuffered());
604+
$this->assertFalse($multipart->isFinished());
468605
}
469606

470607
$result = (string) $multipart;
471608
$this->assertTrue($multipart->isBuffered());
609+
$this->assertFalse($multipart->isFinished());
472610

473611
fclose($resource);
474612
fclose($resource2);
@@ -483,9 +621,13 @@ public function testToStringNoFinish()
483621

484622
class TestMultipart extends Multipart
485623
{
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')
487629
{
488-
parent::__construct($boundary, 'multipart/test');
630+
parent::__construct($boundary, $contentType);
489631
}
490632

491633
public function add($content, $length = -1)

0 commit comments

Comments
 (0)