Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addded additional data to response event #184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading