diff --git a/composer.json b/composer.json index 71d7330..2dfc284 100644 --- a/composer.json +++ b/composer.json @@ -52,5 +52,14 @@ "platform": { "php": "7.0.0" } + }, + "scripts": { + "check": [ + "@cs", + "@test" + ], + "cs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff --dry-run", + "cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff", + "test": "@php -dzend.assertions=1 -dassert.exception=1 ./vendor/bin/phpunit --coverage-text" } } diff --git a/examples/gzip-compress.php b/examples/gzip-compress.php index c45279b..f96feb2 100644 --- a/examples/gzip-compress.php +++ b/examples/gzip-compress.php @@ -1,15 +1,15 @@ getBuffer() to obtain the remaining buffered data. + * + * @param string $prompt Optional prompt to print to console + * + * @return \Amp\Promise Will resolve with the read line + */ +function prompt(string $prompt = ''): Promise +{ + return call(static function () use ($prompt) { + if ($prompt) { + yield getStdout()->write($prompt); + } + return getStdinLineReader()->readLine(); + }); +} + +/** + * Simple wrapper function to asynchronously write a string to the PHP output buffer. + * + * @param string $string + * @return Promise + */ +function bufferEcho($string): Promise +{ + return getOutputBufferStream()->write($string); +} function parseLineDelimitedJson(InputStream $stream, bool $assoc = false, int $depth = 512, int $options = 0): Iterator { return new Producer(static function (callable $emit) use ($stream, $assoc, $depth, $options) { diff --git a/test/ResourceOutputStreamTest.php b/test/ResourceOutputStreamTest.php index e186e12..25a33c5 100644 --- a/test/ResourceOutputStreamTest.php +++ b/test/ResourceOutputStreamTest.php @@ -4,7 +4,9 @@ use Amp\ByteStream\ResourceOutputStream; use Amp\ByteStream\StreamException; +use Amp\Loop; use PHPUnit\Framework\TestCase; +use function Amp\ByteStream\bufferEcho; use function Amp\Promise\wait; class ResourceOutputStreamTest extends TestCase @@ -66,4 +68,22 @@ public function testClosedRemoteSocket() wait($stream->write("foobar")); wait($stream->write("foobar")); } + + public function testEcho() + { + Loop::run(function () { + $data = "\n".\base64_encode(\random_bytes(10))."\n"; + $found = false; + \ob_start(static function ($match) use (&$found, $data) { + if ($match === $data) { + $found = true; + return ''; + } + return $match; + }); + yield bufferEcho($data); + \ob_end_flush(); + $this->assertTrue($found, "Data wasn't sent to the output buffer"); + }); + } }