From 8548073d23b0bbfcd0b1bdcd038f2febf6340a23 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 17 Mar 2016 08:49:18 -0500 Subject: [PATCH] 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. --- src/CallbackStream.php | 3 +-- test/CallbackStreamTest.php | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/CallbackStream.php b/src/CallbackStream.php index f1371202..c8e6bc6f 100644 --- a/src/CallbackStream.php +++ b/src/CallbackStream.php @@ -154,8 +154,7 @@ public function read($length) public function getContents() { $callback = $this->detach(); - - return $callback ? call_user_fanc($callback) : ''; + return $callback ? $callback() : ''; } /** diff --git a/test/CallbackStreamTest.php b/test/CallbackStreamTest.php index 38a82171..0ae7b57c 100644 --- a/test/CallbackStreamTest.php +++ b/test/CallbackStreamTest.php @@ -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 () { @@ -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); + } }