Skip to content

Commit

Permalink
added RequestEnricher (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored Apr 24, 2018
1 parent 9e0043f commit 385c17c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/SAREhub/Microt/Util/Request/MultiRequestEnricher.php
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;
}
}
12 changes: 12 additions & 0 deletions src/SAREhub/Microt/Util/Request/RequestEnricher.php
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 src/SAREhub/Microt/Util/Request/RequestEnricherMiddleware.php
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);
}
}
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);
}
}
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()");
}
}

0 comments on commit 385c17c

Please sign in to comment.