Skip to content

Instantly share code, notes, and snippets.

@dkraczkowski
Created January 19, 2017 15:34
Show Gist options
  • Save dkraczkowski/4f2708085b4ebc28a88fa31485bdf449 to your computer and use it in GitHub Desktop.
Save dkraczkowski/4f2708085b4ebc28a88fa31485bdf449 to your computer and use it in GitHub Desktop.
Middleware Composer Test
<?php namespace OctopusTest;
use OctopusTest\Fixtures\DI\A;
use OctopusTest\Fixtures\DI\B;
use RuntimeException;
use Octopus\MiddlewareComposer;
use OctopusTest\TestCase;
class MiddlewareComposerTest extends TestCase
{
public function testBasicUsage()
{
$a = function($next) {
echo "I";
yield $next();
echo " so much";
};
$b = function($next) {
echo " like testing";
yield $next();
};
$runner = new MiddlewareComposer([$a, $b]);
ob_start();
$runner();
$contents = ob_get_contents();
ob_end_clean();
self::assertEquals('I like testing so much', $contents);
}
public function testFinallyHandlerForSuccess()
{
$a = function($next) {
echo 'I';
yield $next();
echo 'as much ';
};
$b = function() {
echo ' like testing ';
yield;
};
$finally = function() {
echo 'as beacon';
};
ob_start();
$runner = new MiddlewareComposer([$a, $b]);
$runner->finally($finally);
$runner();
$contents = ob_get_contents();
ob_end_clean();
self::assertEquals('I like testing as much as beacon', $contents);
}
public function testFinallyHandlerForError()
{
$a = function($next) {
echo 'I';
yield $next();
echo 'as much ';
};
$b = function() {
throw new RuntimeException(' hate when tests are failing');
yield;
};
$finally = function(RuntimeException $exception) {
echo $exception->getMessage();
};
ob_start();
$runner = new MiddlewareComposer([$a, $b]);
$runner->finally($finally);
try {
$runner();
} catch (RuntimeException $exception) {}
$contents = ob_get_contents();
ob_end_clean();
self::assertEquals('I hate when tests are failing', $contents);
}
public function testErrorHandler()
{
$a = function($next) {
echo 'I';
yield $next();
echo 'as much ';
};
$b = function() {
throw new RuntimeException(' hate when tests are failing');
yield;
};
$error = function(RuntimeException $exception) {
echo $exception->getMessage();
};
$finally = function(RuntimeException $exception) {
echo ' so much';
};
ob_start();
$runner = new MiddlewareComposer([$a, $b]);
$runner->error($error)
->finally($finally);
$runner();
$contents = ob_get_contents();
ob_end_clean();
self::assertEquals('I hate when tests are failing so much', $contents);
}
public function testSuccessHandler()
{
$a = function($next) {
echo 'I';
yield $next();
echo ' mostly because ';
};
$b = function($next) {
echo ' love testing';
yield $next();
};
$success = function() {
echo 'it helps';
};
$e = function() {
throw new RuntimeException(' hate failures');
};
ob_start();
$runner = new MiddlewareComposer([$a, $b]);
$runner->success($success);
$runner();
$contents = ob_get_contents();
ob_end_clean();
self::assertEquals('I love testing mostly because it helps', $contents);
ob_start();
$runner = new MiddlewareComposer([$a, $e]);
$runner->success($success);
try {
$runner();
} catch (RuntimeException $e) {}
$contents = ob_get_contents();
ob_end_clean();
self::assertEquals('I', $contents);
}
public function testPassingContext()
{
$context = new \stdClass();
$context->a = 1;
$context->b = 10;
$a = function() {
$this->a += 1;
yield;
};
$b = function() {
$this->b += 10;
yield;
};
$runner = new MiddlewareComposer([$a, $b], $context);
$runner();
self::assertEquals(20, $context->b);
self::assertEquals(2, $context->a);
}
public function testPassingParameters()
{
$result = '';
$a = function(A $a, B $b, $next) use (&$result) {
$result .= $a->getA() . $b->getB();
yield $next($b, $a);
};
$b = function(B $b, A $a, $next = null) use (&$result) {
$result .= $a->getA() . $b->getB();
yield;
};
$runner = new MiddlewareComposer([$a, $b]);
$runner(new A(1), new B(2));
self::assertEquals('1212', $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment