Skip to content

Commit

Permalink
addded additional data to response event (#184)
Browse files Browse the repository at this point in the history
Co-authored-by: samoylenko <stas@csgo.com>
  • Loading branch information
SamoylenkoSU and samoylenko authored May 6, 2024
1 parent ac5c881 commit 75a4422
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/composer.lock
vendor/
local-php-security-checker
/.idea
2 changes: 1 addition & 1 deletion src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function handle(Request $request): Response
$this->eventDispatcher->dispatch(new ResponseDtoEvent($responseDto, $operationId, $specification));

$response = $this->createResponse($requestHandler, $operation, $requestHandlerInterface, $responseDto);
$this->eventDispatcher->dispatch(new ResponseEvent($response, $operationId, $specification));
$this->eventDispatcher->dispatch(new ResponseEvent($response, $operationId, $specification, $requestHandler, $request));

return $response;
}
Expand Down
24 changes: 20 additions & 4 deletions src/Event/Server/ResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace OnMoon\OpenApiServerBundle\Event\Server;

use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Specification\Definitions\Specification;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\Event;

Expand All @@ -20,12 +22,16 @@ final class ResponseEvent extends Event
private Response $response;
private string $operationId;
private Specification $specification;
private RequestHandler $requestHandler;
private Request $request;

public function __construct(Response $response, string $operationId, Specification $specification)
public function __construct(Response $response, string $operationId, Specification $specification, RequestHandler $requestHandler, Request $request)
{
$this->response = $response;
$this->operationId = $operationId;
$this->specification = $specification;
$this->response = $response;
$this->operationId = $operationId;
$this->specification = $specification;
$this->requestHandler = $requestHandler;
$this->request = $request;
}

public function getResponse(): Response
Expand All @@ -42,4 +48,14 @@ public function getSpecification(): Specification
{
return $this->specification;
}

public function getRequestHandler(): RequestHandler
{
return $this->requestHandler;
}

public function getRequest(): Request
{
return $this->request;
}
}
15 changes: 11 additions & 4 deletions test/unit/Event/Server/ResponseEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use cebe\openapi\spec\OpenApi;
use OnMoon\OpenApiServerBundle\Event\Server\ResponseEvent;
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Specification\Definitions\Specification;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -18,14 +20,19 @@ final class ResponseEventTest extends TestCase
{
public function testResponseEventGettersReturnCorrectValues(): void
{
$response = new Response();
$operationId = '12345';
$specification = new Specification([], [], new OpenApi([]));
$response = new Response();
$operationId = '12345';
$specification = new Specification([], [], new OpenApi([]));
$requestHandler = new class () implements RequestHandler{
};
$request = new Request();

$responseEvent = new ResponseEvent($response, $operationId, $specification);
$responseEvent = new ResponseEvent($response, $operationId, $specification, $requestHandler, $request);

Assert::assertEquals($response, $responseEvent->getResponse());
Assert::assertEquals($operationId, $responseEvent->getOperationId());
Assert::assertEquals($specification, $responseEvent->getSpecification());
Assert::assertEquals($requestHandler, $responseEvent->getRequestHandler());
Assert::assertEquals($request, $responseEvent->getRequest());
}
}

0 comments on commit 75a4422

Please sign in to comment.