Skip to content

Commit

Permalink
Added tests to validate getContents() works for arbitrary callbacks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weierophinney committed Mar 17, 2016
1 parent c9d1598 commit 8548073
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/CallbackStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ public function read($length)
public function getContents()
{
$callback = $this->detach();

return $callback ? call_user_fanc($callback) : '';
return $callback ? $callback() : '';
}

/**
Expand Down
42 changes: 42 additions & 0 deletions test/CallbackStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
*/
class CallbackStreamTest extends TestCase
{
/**
* Sample callback to use with testing.
*
* @return string
*/
public function sampleCallback()
{
return __METHOD__;
}

/**
* Sample static callback to use with testing.
*
* @return string
*/
public static function sampleStaticCallback()
{
return __METHOD__;
}

public function testToString()
{
$stream = new CallbackStream(function () {
Expand Down Expand Up @@ -177,4 +197,26 @@ public function testGetMetadata()
$notExists = $stream->getMetadata('boo');
$this->assertNull($notExists);
}

public function phpCallbacksForStreams()
{
$class = 'ZendTest\Diactoros\CallbackStreamTest';

// @codingStandardsIgnoreStart
return [
'instance-method' => [[new self(), 'sampleCallback'], $class . '::sampleCallback'],
'static-method' => [[$class, 'sampleStaticCallback'], $class . '::sampleStaticCallback'],
];
// @codingStandardsIgnoreEnd
}

/**
* @dataProvider phpCallbacksForStreams
*/
public function testAllowsArbitraryPhpCallbacks($callback, $expected)
{
$stream = new CallbackStream($callback);
$contents = $stream->getContents();
$this->assertEquals($expected, $contents);
}
}

0 comments on commit 8548073

Please sign in to comment.