Skip to content

Commit cc2a34a

Browse files
danopzNyholm
authored andcommitted
update test for ServerRequest::getParsedBody to respect thrown InvalidArgumentException, update test for Stream::_toString to use a better read-only stream which works (#14)
1 parent 6a0d6a2 commit cc2a34a

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/ServerRequestIntegrationTest.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,52 @@ public function testGetUploadedFiles()
9090
$this->assertEquals($file, $files[0]);
9191
}
9292

93-
public function testGetParsedBody()
93+
/**
94+
* @dataProvider validParsedBodyParams
95+
*/
96+
public function testGetParsedBody($value)
9497
{
9598
if (isset($this->skippedTests[__FUNCTION__])) {
9699
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
97100
}
98101

99-
$data = [
100-
4711,
101-
null,
102-
new \stdClass(),
103-
['foo' => 'bar', 'baz'],
102+
$new = $this->serverRequest->withParsedBody($value);
103+
$this->assertNull($this->serverRequest->getParsedBody(), 'withParsedBody MUST be immutable');
104+
$this->assertEquals($value, $new->getParsedBody());
105+
}
106+
107+
public function validParsedBodyParams()
108+
{
109+
return [
110+
[null],
111+
[new \stdClass()],
112+
[['foo' => 'bar', 'baz']],
104113
];
114+
}
105115

106-
foreach ($data as $item) {
107-
$new = $this->serverRequest->withParsedBody($item);
108-
$this->assertNull($this->serverRequest->getParsedBody(), 'withParsedBody MUST be immutable');
109-
$this->assertEquals($item, $new->getParsedBody());
116+
/**
117+
* @dataProvider invalidParsedBodyParams
118+
* @expectedException \InvalidArgumentException
119+
*/
120+
public function testGetParsedBodyInvalid($value)
121+
{
122+
if (isset($this->skippedTests[__FUNCTION__])) {
123+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
110124
}
125+
126+
$new = $this->serverRequest->withParsedBody($value);
127+
$this->assertNull($this->serverRequest->getParsedBody(), 'withParsedBody MUST be immutable');
128+
$this->assertEquals($value, $new->getParsedBody());
129+
}
130+
131+
public function invalidParsedBodyParams()
132+
{
133+
return [
134+
[4711],
135+
[47.11],
136+
['foobar'],
137+
[true],
138+
];
111139
}
112140

113141
public function testGetAttributes()
@@ -117,7 +145,9 @@ public function testGetAttributes()
117145
}
118146

119147
$new = $this->serverRequest->withAttribute('foo', 'bar');
120-
$this->assertNull($this->serverRequest->getAttributes(), 'withAttribute MUST be immutable');
148+
$oldAttributes = $this->serverRequest->getAttributes();
149+
$this->assertInternalType('array', $oldAttributes, 'getAttributes MUST return an array');
150+
$this->assertEmpty($oldAttributes, 'withAttribute MUST be immutable');
121151
$this->assertEquals(['foo' => 'bar'], $new->getAttributes());
122152

123153
$new = $new->withAttribute('baz', 'biz');

src/StreamIntegrationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ public function testToStringReadOnlyStreams()
2727
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
2828
}
2929

30-
$url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png';
31-
$resource = fopen($url, 'r');
30+
$resource = fopen(__FILE__, 'r');
3231
$stream = $this->createStream($resource);
3332

3433
// Make sure this does not throw exception
3534
$content = (string) $stream;
36-
$this->assertTrue(!empty($content), 'You MUST be able to convert a read only stream to string');
35+
$this->assertNotEmpty($content, 'You MUST be able to convert a read only stream to string');
3736
}
3837

3938
public function testToStringRewindStreamBeforeToString()

0 commit comments

Comments
 (0)