Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
PSR-15 support (http-interop/http-server-middleware)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Nov 27, 2017
1 parent d997148 commit 17af450
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"require": {
"php": "^7.1",
"http-interop/http-middleware": "^0.4.1",
"http-interop/http-server-middleware": "^1.0.1",
"psr/container": "^1.0",
"psr/http-message": "^1.0.1"
},
Expand Down
101 changes: 79 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

namespace Zend\Expressive\Authentication;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Interop\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class AuthenticationMiddleware implements ServerMiddlewareInterface
class AuthenticationMiddleware implements MiddlewareInterface
{
/**
* @var AuthenticationInterface
Expand All @@ -26,11 +27,11 @@ public function __construct(AuthenticationInterface $auth)
/**
* {@inheritDoc}
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$user = $this->auth->authenticate($request);
if (null !== $user) {
return $delegate->process($request->withAttribute(UserInterface::class, $user));
return $handler->handle($request->withAttribute(UserInterface::class, $user));
}
return $this->auth->unauthorizedResponse($request);
}
Expand Down
18 changes: 9 additions & 9 deletions test/AuthenticationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
namespace ZendTest\Expressive\Authentication;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Interop\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -25,14 +25,14 @@ public function setUp()
$this->authentication = $this->prophesize(AuthenticationInterface::class);
$this->request = $this->prophesize(ServerRequestInterface::class);
$this->authenticatedUser = $this->prophesize(UserInterface::class);
$this->delegate = $this->prophesize(DelegateInterface::class);
$this->handler = $this->prophesize(RequestHandlerInterface::class);
}

public function testConstructor()
{
$middleware = new AuthenticationMiddleware($this->authentication->reveal());
$this->assertInstanceOf(AuthenticationMiddleware::class, $middleware);
$this->assertInstanceOf(ServerMiddlewareInterface::class, $middleware);
$this->assertInstanceOf(MiddlewareInterface::class, $middleware);
}

public function testProcessWithNoAuthenticatedUser()
Expand All @@ -45,7 +45,7 @@ public function testProcessWithNoAuthenticatedUser()
->willReturn($response->reveal());

$middleware = new AuthenticationMiddleware($this->authentication->reveal());
$result = $middleware->process($this->request->reveal(), $this->delegate->reveal());
$result = $middleware->process($this->request->reveal(), $this->handler->reveal());

$this->assertInstanceOf(ResponseInterface::class, $result);
$this->assertEquals($response->reveal(), $result);
Expand All @@ -60,14 +60,14 @@ public function testProcessWithAuthenticatedUser()
->willReturn($this->request->reveal());
$this->authentication->authenticate($this->request->reveal())
->willReturn($this->authenticatedUser->reveal());
$this->delegate->process($this->request->reveal())
->willReturn($response->reveal());
$this->handler->handle($this->request->reveal())
->willReturn($response->reveal());

$middleware = new AuthenticationMiddleware($this->authentication->reveal());
$result = $middleware->process($this->request->reveal(), $this->delegate->reveal());
$result = $middleware->process($this->request->reveal(), $this->handler->reveal());

$this->assertInstanceOf(ResponseInterface::class, $result);
$this->assertEquals($response->reveal(), $result);
$this->delegate->process($this->request->reveal())->shouldBeCalled();
$this->handler->handle($this->request->reveal())->shouldBeCalled();
}
}

0 comments on commit 17af450

Please sign in to comment.