Skip to content

Commit 9909f88

Browse files
committed
Moved addMultipart body to Multipart. Added more failure tests
1 parent fc20a44 commit 9909f88

File tree

8 files changed

+566
-12
lines changed

8 files changed

+566
-12
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ parameters:
1313
path: tests/Robtimus/Multipart/*
1414
- message: '#Method .* has parameter .* with no type specified.#'
1515
path: tests/Robtimus/Multipart/*
16+
- message: '#Parameter \#[0-9]+ .* of method .* expects .*, .* given.#'
17+
path: tests/Robtimus/Multipart/*

src/Robtimus/Multipart/Multipart.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ final protected function addContent($content, $length = -1)
201201
$this->add($content, $length);
202202
}
203203

204+
/**
205+
* Adds a nested multipart.
206+
*
207+
* @param Multipart $multipart The nested multipart.
208+
*
209+
* @return void
210+
*/
211+
protected function addMultipart(Multipart $multipart)
212+
{
213+
$this->startPart();
214+
$this->addContentType($multipart->getContentType());
215+
$this->endHeaders();
216+
$this->addContent(array($multipart, 'read'), $multipart->getContentLength());
217+
$this->endPart();
218+
}
219+
204220
/**
205221
* Ends the last part.
206222
*

src/Robtimus/Multipart/MultipartAlternative.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ public function __construct($boundary = '')
2929
*/
3030
public function addMultipart(Multipart $multipart)
3131
{
32-
$this->startPart();
33-
$this->addContentType($multipart->getContentType());
34-
$this->endHeaders();
35-
$this->addContent(array($multipart, 'read'), $multipart->getContentLength());
36-
$this->endPart();
37-
32+
parent::addMultipart($multipart);
3833
return $this;
3934
}
4035

src/Robtimus/Multipart/MultipartMixed.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ public function __construct($boundary = '')
2929
*/
3030
public function addMultipart(Multipart $multipart)
3131
{
32-
$this->startPart();
33-
$this->addContentType($multipart->getContentType());
34-
$this->endHeaders();
35-
$this->addContent(array($multipart, 'read'), $multipart->getContentLength());
36-
$this->endPart();
37-
32+
parent::addMultipart($multipart);
3833
return $this;
3934
}
4035

tests/Robtimus/Multipart/MultipartAlternativeTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,71 @@
33

44
class MultipartAlternativeTest extends MultipartTestBase
55
{
6+
public function testAddPartInvalidTypeOfContent()
7+
{
8+
$multipart = new MultipartAlternative();
9+
10+
try {
11+
$multipart->addPart(0, 'text/plain');
12+
13+
$this->fail('Expected an InvalidArgumentException');
14+
} catch (\InvalidArgumentException $e) {
15+
$this->assertEquals('$content is incorrectly typed', $e->getMessage());
16+
}
17+
}
18+
19+
public function testAddPartInvalidTypeOfContentType()
20+
{
21+
$multipart = new MultipartAlternative();
22+
23+
try {
24+
$multipart->addPart('Hello World', 0);
25+
26+
$this->fail('Expected an InvalidArgumentException');
27+
} catch (\InvalidArgumentException $e) {
28+
$this->assertEquals('$contentType is incorrectly typed', $e->getMessage());
29+
}
30+
}
31+
32+
public function testAddPartEmptyContentType()
33+
{
34+
$multipart = new MultipartAlternative();
35+
36+
try {
37+
$multipart->addPart('Hello World', '');
38+
39+
$this->fail('Expected an InvalidArgumentException');
40+
} catch (\InvalidArgumentException $e) {
41+
$this->assertEquals('$contentType must be non-empty', $e->getMessage());
42+
}
43+
}
44+
45+
public function testAddPartInvalidTypeOfContentLength()
46+
{
47+
$multipart = new MultipartAlternative();
48+
49+
try {
50+
$multipart->addPart('Hello World', 'text/plain', '');
51+
52+
$this->fail('Expected an InvalidArgumentException');
53+
} catch (\InvalidArgumentException $e) {
54+
$this->assertEquals('$contentLength is incorrectly typed', $e->getMessage());
55+
}
56+
}
57+
58+
public function testAddPartInvalidTypeOfContentTransferEncoding()
59+
{
60+
$multipart = new MultipartAlternative();
61+
62+
try {
63+
$multipart->addPart('Hello World', 'text/plain', -1, 0);
64+
65+
$this->fail('Expected an InvalidArgumentException');
66+
} catch (\InvalidArgumentException $e) {
67+
$this->assertEquals('$contentTransferEncoding is incorrectly typed', $e->getMessage());
68+
}
69+
}
70+
671
public function testRead()
772
{
873
$multipart = new MultipartAlternative('test-boundary');

tests/Robtimus/Multipart/MultipartFormDataTest.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,149 @@
33

44
class MultipartFormDataTest extends MultipartTestBase
55
{
6+
public function testAddValueInvalidTypeOfName()
7+
{
8+
$multipart = new MultipartFormData();
9+
10+
try {
11+
$multipart->addValue(0, 'value');
12+
13+
$this->fail('Expected an InvalidArgumentException');
14+
} catch (\InvalidArgumentException $e) {
15+
$this->assertEquals('$name is incorrectly typed', $e->getMessage());
16+
}
17+
}
18+
19+
public function testAddValueEmptyName()
20+
{
21+
$multipart = new MultipartFormData();
22+
23+
try {
24+
$multipart->addValue('', 'value');
25+
26+
$this->fail('Expected an InvalidArgumentException');
27+
} catch (\InvalidArgumentException $e) {
28+
$this->assertEquals('$name must be non-empty', $e->getMessage());
29+
}
30+
}
31+
32+
public function testAddValueInvalidTypeOfValue()
33+
{
34+
$multipart = new MultipartFormData();
35+
36+
try {
37+
$multipart->addValue('name', 0);
38+
39+
$this->fail('Expected an InvalidArgumentException');
40+
} catch (\InvalidArgumentException $e) {
41+
$this->assertEquals('$value is incorrectly typed', $e->getMessage());
42+
}
43+
}
44+
45+
public function testAddFileInvalidTypeOfName()
46+
{
47+
$multipart = new MultipartFormData();
48+
49+
try {
50+
$multipart->addFile(0, 'file.txt', 'Hello World', 'text/plain');
51+
52+
$this->fail('Expected an InvalidArgumentException');
53+
} catch (\InvalidArgumentException $e) {
54+
$this->assertEquals('$name is incorrectly typed', $e->getMessage());
55+
}
56+
}
57+
58+
public function testAddFileEmptyName()
59+
{
60+
$multipart = new MultipartFormData();
61+
62+
try {
63+
$multipart->addFile('', 'file.txt', 'Hello World', 'text/plain');
64+
65+
$this->fail('Expected an InvalidArgumentException');
66+
} catch (\InvalidArgumentException $e) {
67+
$this->assertEquals('$name must be non-empty', $e->getMessage());
68+
}
69+
}
70+
71+
public function testAddFileInvalidTypeOfFilename()
72+
{
73+
$multipart = new MultipartFormData();
74+
75+
try {
76+
$multipart->addFile('name', 0, 'Hello World', 'text/plain');
77+
78+
$this->fail('Expected an InvalidArgumentException');
79+
} catch (\InvalidArgumentException $e) {
80+
$this->assertEquals('$filename is incorrectly typed', $e->getMessage());
81+
}
82+
}
83+
84+
public function testAddFileEmptyFilename()
85+
{
86+
$multipart = new MultipartFormData();
87+
88+
try {
89+
$multipart->addFile('name', '', 'Hello World', 'text/plain');
90+
91+
$this->fail('Expected an InvalidArgumentException');
92+
} catch (\InvalidArgumentException $e) {
93+
$this->assertEquals('$filename must be non-empty', $e->getMessage());
94+
}
95+
}
96+
97+
public function testAddFileInvalidTypeOfContent()
98+
{
99+
$multipart = new MultipartFormData();
100+
101+
try {
102+
$multipart->addFile('name', 'file.txt', 0, 'text/plain');
103+
104+
$this->fail('Expected an InvalidArgumentException');
105+
} catch (\InvalidArgumentException $e) {
106+
$this->assertEquals('$content is incorrectly typed', $e->getMessage());
107+
}
108+
}
109+
110+
public function testAddFileInvalidTypeOfContentType()
111+
{
112+
$multipart = new MultipartFormData();
113+
114+
try {
115+
$multipart->addFile('name', 'file.txt', 'Hello World', 0);
116+
117+
$this->fail('Expected an InvalidArgumentException');
118+
} catch (\InvalidArgumentException $e) {
119+
$this->assertEquals('$contentType is incorrectly typed', $e->getMessage());
120+
}
121+
}
122+
123+
public function testAddFileEmptyContentType()
124+
{
125+
$multipart = new MultipartFormData();
126+
127+
try {
128+
$multipart->addFile('name', 'file.txt', 'Hello World', '');
129+
130+
$this->fail('Expected an InvalidArgumentException');
131+
} catch (\InvalidArgumentException $e) {
132+
$this->assertEquals('$contentType must be non-empty', $e->getMessage());
133+
}
134+
}
135+
136+
public function testAddFileInvalidTypeOfContentLength()
137+
{
138+
$multipart = new MultipartFormData();
139+
140+
try {
141+
$multipart->addFile('name', 'file.txt', 'Hello World', 'text/plain', '');
142+
143+
$this->fail('Expected an InvalidArgumentException');
144+
} catch (\InvalidArgumentException $e) {
145+
$this->assertEquals('$contentLength is incorrectly typed', $e->getMessage());
146+
}
147+
}
148+
6149
public function testReadStringsOnly()
7150
{
8151
$multipart = new MultipartFormData('test-boundary');

0 commit comments

Comments
 (0)