-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
128 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Microt\Util\Request; | ||
|
||
|
||
use Slim\Http\Request; | ||
|
||
class MultiRequestEnricher implements RequestEnricher | ||
{ | ||
/** | ||
* @var RequestEnricher[] | ||
*/ | ||
private $enrichers; | ||
|
||
public function __construct(array $enrichers) | ||
{ | ||
$this->enrichers = $enrichers; | ||
} | ||
|
||
public function enrich(Request $request): Request | ||
{ | ||
$enrichedRequest = $request; | ||
|
||
foreach ($this->enrichers as $enricher) { | ||
$enrichedRequest = $enricher->enrich($enrichedRequest); | ||
} | ||
|
||
return $enrichedRequest; | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Microt\Util\Request; | ||
|
||
|
||
use Slim\Http\Request; | ||
|
||
interface RequestEnricher | ||
{ | ||
public function enrich(Request $request): Request; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/SAREhub/Microt/Util/Request/RequestEnricherMiddleware.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,27 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Microt\Util\Request; | ||
|
||
|
||
use Slim\Http\Request; | ||
use Slim\Http\Response; | ||
|
||
class RequestEnricherMiddleware | ||
{ | ||
/** | ||
* @var RequestEnricher | ||
*/ | ||
private $enricher; | ||
|
||
public function __construct(RequestEnricher $enricher) | ||
{ | ||
$this->enricher = $enricher; | ||
} | ||
|
||
public function __invoke(Request $request, Response $response, callable $next): Response | ||
{ | ||
$request = $this->enricher->enrich($request); | ||
return $next($request, $response); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/unit/SAREhub/Microt/Util/Request/MultiRequestEnricherTest.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,27 @@ | ||
<?php | ||
|
||
namespace SAREhub\Microt\Util\Request; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Microt\Test\App\HttpHelper; | ||
|
||
class MultiRequestEnricherTest extends TestCase | ||
{ | ||
|
||
public function testEnrich() | ||
{ | ||
$enricher1 = \Mockery::mock(RequestEnricher::class); | ||
$enricher2 = \Mockery::mock(RequestEnricher::class); | ||
$multiEnricher = new MultiRequestEnricher([$enricher1, $enricher2]); | ||
$inRequest = HttpHelper::request(); | ||
|
||
$enrichedRequest1 = HttpHelper::request(); | ||
$enricher1->expects("enrich")->withArgs([$inRequest])->andReturn($enrichedRequest1); | ||
$enrichedRequest2 = HttpHelper::request(); | ||
$enricher2->expects("enrich")->withArgs([$enrichedRequest1])->andReturn($enrichedRequest2); | ||
|
||
$current = $multiEnricher->enrich($inRequest); | ||
|
||
$this->assertSame($enrichedRequest2, $current); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/unit/SAREhub/Microt/Util/Request/RequestEnricherMiddlewareTest.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,31 @@ | ||
<?php | ||
|
||
namespace SAREhub\Microt\Util\Request; | ||
|
||
|
||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Microt\Test\App\HttpHelper; | ||
use SAREhub\Microt\Test\CallableMock; | ||
|
||
|
||
class RequestEnricherMiddlewareTest extends TestCase | ||
{ | ||
public function testCall() | ||
{ | ||
$enricher = \Mockery::mock(RequestEnricher::class); | ||
$middleware = new RequestEnricherMiddleware($enricher); | ||
$inRequest = HttpHelper::request(); | ||
$inResponse = HttpHelper::response(); | ||
$next = CallableMock::create(); | ||
|
||
$enrichedRequest = HttpHelper::request(); | ||
$enricher->expects("enrich")->withArgs([$inRequest])->andReturn($enrichedRequest); | ||
|
||
$nextResponse = HttpHelper::response(); | ||
$next->expects("__invoke")->withArgs([$enrichedRequest, $inResponse])->andReturn($nextResponse); | ||
|
||
$currentResponse = $middleware($inRequest, $inResponse, $next); | ||
|
||
$this->assertSame($nextResponse, $currentResponse, "middleware response === next()"); | ||
} | ||
} |