Skip to content

Commit

Permalink
Changed RequestDtoEvent (#176)
Browse files Browse the repository at this point in the history
* Changed RequestDtoEvent

Co-authored-by: Patrik Foldes <pf@csgo.com>
  • Loading branch information
sspat and Patrik Foldes authored Sep 1, 2022
1 parent 303688c commit 0d9ed5c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"preferred-install": {
"*": "dist"
},
"process-timeout": 14400,
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenerator/NameGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function setRequestNames(RequestDtoDefinition $request, string $operation
public function setResponseNames(ResponseDtoDefinition $response, string $responseNamespace, string $operationName, string $responsePath): void
{
try {
$statusNamespace = $this->httpstatus->getReasonPhrase($response->getStatusCode());
$statusNamespace = $this->httpstatus->getReasonPhrase((int) $response->getStatusCode());
} catch (Throwable $e) {
$statusNamespace = $response->getStatusCode();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ public function handle(Request $request): Response
[$methodName, $inputDtoClass] = $this->getMethodAndInputDtoFQCN($requestHandlerInterface);

$requestDto = null;

if ($inputDtoClass !== null) {
$requestDto = $this->createRequestDto($request, $operation, $inputDtoClass);
$this->eventDispatcher->dispatch(new RequestDtoEvent($requestDto, $operationId, $specification));
}

$this->eventDispatcher->dispatch(new RequestDtoEvent($requestDto, $operationId, $specification, $requestHandler));

$responseDto = $this->executeRequestHandler($requestHandler, $methodName, $requestDto);
$this->eventDispatcher->dispatch(new ResponseDtoEvent($responseDto, $operationId, $specification));

Expand Down
16 changes: 12 additions & 4 deletions src/Event/Server/RequestDtoEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OnMoon\OpenApiServerBundle\Event\Server;

use OnMoon\OpenApiServerBundle\Interfaces\Dto;
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Specification\Definitions\Specification;
use Symfony\Contracts\EventDispatcher\Event;

Expand All @@ -27,12 +28,14 @@ final class RequestDtoEvent extends Event
private ?Dto $requestDto;
private string $operationId;
private Specification $specification;
private RequestHandler $requestHandler;

public function __construct(?Dto $requestDto, string $operationId, Specification $specification)
public function __construct(?Dto $requestDto, string $operationId, Specification $specification, RequestHandler $requestHandler)
{
$this->requestDto = $requestDto;
$this->operationId = $operationId;
$this->specification = $specification;
$this->requestDto = $requestDto;
$this->operationId = $operationId;
$this->specification = $specification;
$this->requestHandler = $requestHandler;
}

public function getRequestDto(): ?Dto
Expand All @@ -49,4 +52,9 @@ public function getSpecification(): Specification
{
return $this->specification;
}

public function getRequestHandler(): RequestHandler
{
return $this->requestHandler;
}
}
28 changes: 24 additions & 4 deletions test/unit/Event/Server/RequestDtoEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use cebe\openapi\spec\OpenApi;
use OnMoon\OpenApiServerBundle\Event\Server\RequestDtoEvent;
use OnMoon\OpenApiServerBundle\Interfaces\Dto;
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Specification\Definitions\Specification;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
Expand All @@ -18,7 +19,7 @@ final class RequestDtoEventTest extends TestCase
{
public function testRequestDtoEventGettersReturnCorrectValues(): void
{
$requestDto = new class () implements Dto {
$requestDto = new class () implements Dto {
/**
* @return mixed[]
*/
Expand All @@ -35,13 +36,32 @@ public static function fromArray(array $data): Dto
return new self();
}
};
$operationId = '12345';
$specification = new Specification([], new OpenApi([]));
$operationId = '12345';
$specification = new Specification([], new OpenApi([]));
$requestHandler = new class () implements RequestHandler{
};

$requestDtoEvent = new RequestDtoEvent($requestDto, $operationId, $specification);
$requestDtoEvent = new RequestDtoEvent($requestDto, $operationId, $specification, $requestHandler);

Assert::assertEquals($requestDto, $requestDtoEvent->getRequestDto());
Assert::assertEquals($operationId, $requestDtoEvent->getOperationId());
Assert::assertEquals($specification, $requestDtoEvent->getSpecification());
Assert::assertEquals($requestHandler, $requestDtoEvent->getRequestHandler());
}

public function testRequestDtoEventGettersWhenRequestDtoNull(): void
{
$requestDto = null;
$operationId = '12345';
$specification = new Specification([], new OpenApi([]));
$requestHandler = new class () implements RequestHandler{
};

$requestDtoEvent = new RequestDtoEvent($requestDto, $operationId, $specification, $requestHandler);

Assert::assertNull($requestDtoEvent->getRequestDto());
Assert::assertEquals($operationId, $requestDtoEvent->getOperationId());
Assert::assertEquals($specification, $requestDtoEvent->getSpecification());
Assert::assertEquals($requestHandler, $requestDtoEvent->getRequestHandler());
}
}

0 comments on commit 0d9ed5c

Please sign in to comment.