-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed7b15c
commit be86f91
Showing
7 changed files
with
109 additions
and
4 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
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 |
---|---|---|
|
@@ -53,4 +53,4 @@ | |
"url": "https://tidelift.com/subscription/pkg/packagist-sonsofphp-sonsofphp" | ||
} | ||
] | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\HttpHandler; | ||
|
||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
// RequestHandler? | ||
class HttpHandler implements RequestHandlerInterface | ||
{ | ||
public function __construct(private array $queue = []) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
if (0 === count($this->queue)) { | ||
throw new \Exception('No Middleware in the queue.'); | ||
} | ||
|
||
$middleware = array_shift($this->queue); | ||
|
||
return $middleware->process($request, $this); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/SonsOfPHP/Component/HttpHandler/Tests/HttpHandlerTest.php
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\HttpHandler\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SonsOfPHP\Component\HttpHandler\HttpHandler; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use SonsOfPHP\Component\HttpMessage\Response; | ||
|
||
/** | ||
* @coversDefaultClass \SonsOfPHP\Component\HttpHandler\HttpHandler | ||
* | ||
* @uses \SonsOfPHP\Component\HttpHandler\HttpHandler | ||
*/ | ||
final class HttpHandlerTest extends TestCase | ||
{ | ||
private $request; | ||
private $response; | ||
private array $middlewares = []; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->request = $this->createMock(ServerRequestInterface::class); | ||
$this->response = $this->createMock(ResponseInterface::class); | ||
|
||
$this->middlewares[] = new class implements MiddlewareInterface { | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
return new Response(); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @coversNothing | ||
*/ | ||
public function testItHasTheCorrectInterface(): void | ||
{ | ||
$handler = new HttpHandler(); | ||
|
||
$this->assertInstanceOf(RequestHandlerInterface::class, $handler); | ||
} | ||
|
||
/** | ||
* @covers ::handle | ||
*/ | ||
public function testHandle(): void | ||
{ | ||
$handler = new HttpHandler($this->middlewares); | ||
|
||
$this->assertNotNull($handler->handle($this->request)); | ||
} | ||
} |
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