Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TerminableInterface #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/Stack/CallableHttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,44 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;

class CallableHttpKernel implements HttpKernelInterface
class CallableHttpKernel implements HttpKernelInterface, TerminableInterface
{
private $callable;
private $handleCallable;
private $terminateCallable;

public function __construct($callable)
public function __construct($handleCallable, $terminateCallable = null)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException('Invalid callable passed to CallableHttpKernel::__construct().');
if (!is_callable($handleCallable)) {
throw new \InvalidArgumentException('Invalid handleCallable passed to CallableHttpKernel::__construct().');
}

$this->callable = $callable;
if ($terminateCallable && !is_callable($terminateCallable)) {
throw new \InvalidArgumentException('Invalid terminateCallable passed to CallableHttpKernel::__construct().');
}

$this->handleCallable = $handleCallable;
$this->terminateCallable = $terminateCallable;
}

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
$response = call_user_func($this->callable, $request, $type, $catch);
$response = call_user_func($this->handleCallable, $request, $type, $catch);

if (!$response instanceof Response) {
throw new \UnexpectedValueException('Kernel function did not return an object of type Response');
}

return $response;
}

public function terminate(Request $request, Response $response)
{
if (!$this->terminateCallable) {
return;
}

call_user_func($this->terminateCallable, $request, $response);
}
}
27 changes: 18 additions & 9 deletions tests/unit/Stack/CallableHttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,45 @@
class CallableHttpKernelTest extends \PHPUnit_Framework_TestCase
{
/** @test */
public function handleShouldDelegateToCallable()
public function handleAndTerminateShouldDelegateToCallable()
{
$spy = new SpyCallable(new Response('ok'));
$kernel = new CallableHttpKernel($spy);
$handleSpy = new SpyCallable(new Response('ok'));
$terminateSpy = new SpyCallable(null);
$kernel = new CallableHttpKernel($handleSpy, $terminateSpy);

$request = Request::create('/');
$response = $kernel->handle($request);

$this->assertSame('ok', $response->getContent());
$this->assertSame(1, $spy->getCount());
$this->assertSame(1, $handleSpy->getCount());
$this->assertSame(
array($request, HttpKernelInterface::MASTER_REQUEST, true),
$spy->getCall(0)
$handleSpy->getCall(0)
);

$kernel->terminate($request, $response);

$this->assertSame(1, $terminateSpy->getCount());
$this->assertSame(
array($request, $response),
$terminateSpy->getCall(0)
);
}

/** @test */
public function handleShouldDelegateAllArgs()
{
$spy = new SpyCallable(new Response('ok'));
$kernel = new CallableHttpKernel($spy);
$handleSpy = new SpyCallable(new Response('ok'));
$kernel = new CallableHttpKernel($handleSpy);

$request = Request::create('/');
$response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false);

$this->assertSame('ok', $response->getContent());
$this->assertSame(1, $spy->getCount());
$this->assertSame(1, $handleSpy->getCount());
$this->assertSame(
array($request, HttpKernelInterface::SUB_REQUEST, false),
$spy->getCall(0)
$handleSpy->getCall(0)
);
}
}
Expand Down