Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Dec 6, 2023
1 parent ed7b15c commit be86f91
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ test-cqrs: phpunit
test-http-factory: PHPUNIT_TESTSUITE=http-factory
test-http-factory: phpunit

test-http-handler: PHPUNIT_TESTSUITE=http-handler
test-http-handler: phpunit

test-link: PHPUNIT_TESTSUITE=link
test-link: phpunit

Expand Down Expand Up @@ -132,6 +135,9 @@ coverage-filesystem:
coverage-http-factory:
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --testsuite http-factory --coverage-html $(COVERAGE_DIR)

coverage-http-handler: PHPUNIT_TESTSUITE=http-handler
coverage-http-handler: coverage

coverage-http-message:
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --testsuite http-message --coverage-html $(COVERAGE_DIR)

Expand Down
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
"sonsofphp/logger-implementation": "0.3.x-dev",
"sonsofphp/pager-implementation": "0.3.x-dev",
"psr/link-implementation": "^1.0 || ^2.0",
"sonsofphp/cookie-implementation": "0.3.x-dev"
"sonsofphp/cookie-implementation": "0.3.x-dev",
"psr/container-implementation": "^1.0 || ^2.0",
"psr/http-server-handler-implementation": "^1.0",
"psr/http-server-middleware-implementation": "^1.0"
},
"require": {
"php": ">=8.1",
Expand Down Expand Up @@ -77,7 +80,9 @@
"doctrine/collections": "^2",
"doctrine/orm": "^2",
"sonsofphp/cookie-contract": "0.3.x-dev",
"psr/container": "^1.0 || ^2.0"
"psr/container": "^1.0 || ^2.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -116,7 +121,8 @@
"sonsofphp/pager-doctrine-orm": "self.version",
"sonsofphp/cookie": "self.version",
"sonsofphp/cookie-contract": "self.version",
"sonsofphp/container": "self.version"
"sonsofphp/container": "self.version",
"sonsofphp/http-handler": "self.version"
},
"autoload": {
"psr-4": {
Expand All @@ -136,6 +142,7 @@
"SonsOfPHP\\Component\\FeatureToggle\\": "src/SonsOfPHP/Component/FeatureToggle",
"SonsOfPHP\\Component\\Filesystem\\": "src/SonsOfPHP/Component/Filesystem",
"SonsOfPHP\\Component\\HttpFactory\\": "src/SonsOfPHP/Component/HttpFactory",
"SonsOfPHP\\Component\\HttpHandler\\": "src/SonsOfPHP/Component/HttpHandler",
"SonsOfPHP\\Component\\HttpMessage\\": "src/SonsOfPHP/Component/HttpMessage",
"SonsOfPHP\\Component\\Json\\": "src/SonsOfPHP/Component/Json",
"SonsOfPHP\\Component\\Link\\": "src/SonsOfPHP/Component/Link",
Expand Down Expand Up @@ -174,6 +181,7 @@
"src/SonsOfPHP/Component/FeatureToggle/Tests",
"src/SonsOfPHP/Component/Filesystem/Tests",
"src/SonsOfPHP/Component/HttpFactory/Tests",
"src/SonsOfPHP/Component/HttpHandler/Tests",
"src/SonsOfPHP/Component/HttpMessage/Tests",
"src/SonsOfPHP/Component/Json/Tests",
"src/SonsOfPHP/Component/Link/Tests",
Expand Down
1 change: 1 addition & 0 deletions docs/components/http-handler/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Usage is pretty simple.
use SonsOfPHP\Component\HttpHandler\HttpHandler;

$middlewares = [];
$middlewares[] = new RouterMiddleware();
$middlewares[] = new CookieMiddleware();
// ...

Expand Down
2 changes: 1 addition & 1 deletion src/SonsOfPHP/Component/Container/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
"url": "https://tidelift.com/subscription/pkg/packagist-sonsofphp-sonsofphp"
}
]
}
}
29 changes: 29 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/HttpHandler.php
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 src/SonsOfPHP/Component/HttpHandler/Tests/HttpHandlerTest.php
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));
}
}
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"sonsofphp/http-message": "0.3.x-dev"
},
"provide": {
"psr/http-server-handler-implementation": "^1.0",
"psr/http-server-middleware-implementation": "^1.0"
Expand Down

0 comments on commit be86f91

Please sign in to comment.