-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kodus/1.0.0
add PSR-15 middleware and test: upgrades minimum PHP version to 7
- Loading branch information
Showing
5 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
language: php | ||
|
||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
|
||
before_script: | ||
- 'composer install --prefer-source' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Kodus\Logging; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* Add this to the *top* of your middleware stack - it will delegate to the rest of the | ||
* middleware stack unconditionally, then decorates the Response with ChromeLogger headers. | ||
*/ | ||
class ChromeLoggerMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var ChromeLogger | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param ChromeLogger $logger | ||
*/ | ||
public function __construct(ChromeLogger $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Process an incoming server request and return a response, optionally delegating | ||
* response creation to a handler. | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$response = $handler->handle($request); | ||
|
||
return $this->logger->writeToResponse($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Kodus\Logging\Test\Unit; | ||
|
||
use Kodus\Logging\ChromeLogger; | ||
use Kodus\Logging\ChromeLoggerMiddleware; | ||
use mindplay\middleman\Dispatcher; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use UnitTester; | ||
use Zend\Diactoros\Response\TextResponse; | ||
use Zend\Diactoros\ServerRequest; | ||
|
||
class MockMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var ChromeLogger | ||
*/ | ||
private $logger; | ||
|
||
public function __construct(ChromeLogger $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$this->logger->notice("running mock middleware"); | ||
|
||
return new TextResponse("Hello"); | ||
} | ||
} | ||
|
||
class ChromeLoggerMiddlewareCest | ||
{ | ||
public function logDuringMiddlewareDispatch(UnitTester $I) | ||
{ | ||
$logger = new ChromeLogger(); | ||
|
||
$logger_middleware = new ChromeLoggerMiddleware($logger); | ||
|
||
$mock_middleware = new MockMiddleware($logger); | ||
|
||
$dispatcher = new Dispatcher([ | ||
$logger_middleware, | ||
$mock_middleware | ||
]); | ||
|
||
$response = $dispatcher->dispatch(new ServerRequest()); | ||
|
||
$header = $response->getHeaderLine(ChromeLogger::HEADER_NAME); | ||
|
||
$data = json_decode(base64_decode($header), true); | ||
|
||
$I->assertSame([[["running mock middleware"], "info"]], $data["rows"]); | ||
} | ||
} |