Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 8548073

Browse files
committed
Added tests to validate getContents() works for arbitrary callbacks
Starting with PHP 5.4, you can call arbitrary callbacks using the `$callback()` notation (`call_user_func()` is no longer necessary). As such, also reverted the changes from 1b43e4d.
1 parent c9d1598 commit 8548073

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/CallbackStream.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ public function read($length)
154154
public function getContents()
155155
{
156156
$callback = $this->detach();
157-
158-
return $callback ? call_user_fanc($callback) : '';
157+
return $callback ? $callback() : '';
159158
}
160159

161160
/**

test/CallbackStreamTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818
*/
1919
class CallbackStreamTest extends TestCase
2020
{
21+
/**
22+
* Sample callback to use with testing.
23+
*
24+
* @return string
25+
*/
26+
public function sampleCallback()
27+
{
28+
return __METHOD__;
29+
}
30+
31+
/**
32+
* Sample static callback to use with testing.
33+
*
34+
* @return string
35+
*/
36+
public static function sampleStaticCallback()
37+
{
38+
return __METHOD__;
39+
}
40+
2141
public function testToString()
2242
{
2343
$stream = new CallbackStream(function () {
@@ -177,4 +197,26 @@ public function testGetMetadata()
177197
$notExists = $stream->getMetadata('boo');
178198
$this->assertNull($notExists);
179199
}
200+
201+
public function phpCallbacksForStreams()
202+
{
203+
$class = 'ZendTest\Diactoros\CallbackStreamTest';
204+
205+
// @codingStandardsIgnoreStart
206+
return [
207+
'instance-method' => [[new self(), 'sampleCallback'], $class . '::sampleCallback'],
208+
'static-method' => [[$class, 'sampleStaticCallback'], $class . '::sampleStaticCallback'],
209+
];
210+
// @codingStandardsIgnoreEnd
211+
}
212+
213+
/**
214+
* @dataProvider phpCallbacksForStreams
215+
*/
216+
public function testAllowsArbitraryPhpCallbacks($callback, $expected)
217+
{
218+
$stream = new CallbackStream($callback);
219+
$contents = $stream->getContents();
220+
$this->assertEquals($expected, $contents);
221+
}
180222
}

0 commit comments

Comments
 (0)