Skip to content

Commit

Permalink
Added request object to request dto event (#180)
Browse files Browse the repository at this point in the history
* Added request object to request dto event

* Fix phpstan error

Co-authored-by: Patrik Foldes <pf@csgo.com>
  • Loading branch information
sspat and Patrik Foldes authored Nov 16, 2022
1 parent 785e572 commit 8df21f3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"cs": "phpcs --config-set php_version 7040 && phpcs",
"csfix": "phpcs --config-set php_version 7040 && phpcbf",
"psalm": "psalm",
"stan": "phpstan analyse --memory-limit=-1",
"stan": "phpstan analyse --memory-limit=-1 --xdebug",
"tests": "phpunit --fail-on-warning",
"mutation": "vendor/bin/roave-infection-static-analysis-plugin --only-covered --test-framework-options=\"--testsuite=unit\"",
"all": "composer psalm && composer stan && composer tests && composer mutation && composer cs && composer security",
Expand Down
8 changes: 0 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ parameters:
message: '#Instanceof between cebe\\openapi\\spec\\MediaType and cebe\\openapi\\spec\\MediaType will always evaluate to true\.#'
paths:
- %currentWorkingDirectory%/src/Specification/SpecificationParser.php
-
message: '#SpecificationLoaderTest\.php:55\)\) of method#'
paths:
- %currentWorkingDirectory%/test/functional/Specification/SpecificationLoaderTest.php
-
message: '#TestApiServerCodeGeneratorFactory\.php:55\)\) of method#'
paths:
- %currentWorkingDirectory%/test/generation/TestApiServerCodeGeneratorFactory.php
-
message: '#OnMoon\\OpenApiServerBundle\\Router\\RouteLoader::__construct\(\) does not call parent constructor from Symfony\\Component\\Config\\Loader\\Loader\.#'
paths:
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function handle(Request $request): Response
$requestDto = $this->createRequestDto($request, $operation, $inputDtoClass);
}

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

$responseDto = $this->executeRequestHandler($requestHandler, $methodName, $requestDto);
$this->eventDispatcher->dispatch(new ResponseDtoEvent($responseDto, $operationId, $specification));
Expand Down
10 changes: 9 additions & 1 deletion src/Event/Server/RequestDtoEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OnMoon\OpenApiServerBundle\Interfaces\Dto;
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Specification\Definitions\Specification;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\Event;

/**
Expand All @@ -29,13 +30,15 @@ final class RequestDtoEvent extends Event
private string $operationId;
private Specification $specification;
private RequestHandler $requestHandler;
private Request $request;

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

public function getRequestDto(): ?Dto
Expand All @@ -57,4 +60,9 @@ public function getRequestHandler(): RequestHandler
{
return $this->requestHandler;
}

public function getRequest(): Request
{
return $this->request;
}
}
9 changes: 7 additions & 2 deletions test/unit/Event/Server/RequestDtoEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OnMoon\OpenApiServerBundle\Specification\Definitions\Specification;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

/**
* @covers \OnMoon\OpenApiServerBundle\Event\Server\RequestDtoEvent
Expand All @@ -19,6 +20,7 @@ final class RequestDtoEventTest extends TestCase
{
public function testRequestDtoEventGettersReturnCorrectValues(): void
{
$request = new Request();
$requestDto = new class () implements Dto {
/**
* @return mixed[]
Expand All @@ -41,27 +43,30 @@ public static function fromArray(array $data): Dto
$requestHandler = new class () implements RequestHandler{
};

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

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

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

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

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

0 comments on commit 8df21f3

Please sign in to comment.