diff --git a/composer.json b/composer.json index ba0138b5..3375b2a5 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "phpro/grumphp-shim": "~1.2.0", "phpspec/phpspec": "~7.0", "phpspec/prophecy-phpunit": "^2.0.1", - "phpstan/phpstan": "^0.12.57", + "phpstan/phpstan": "^0.12.87", "phpunit/phpunit": "~9.4", "psr/http-factory": "^1.0", "psr/http-message": "^1.0.1", diff --git a/src/Phpro/SoapClient/Client.php b/src/Phpro/SoapClient/Client.php index 549124ad..5f9be56a 100644 --- a/src/Phpro/SoapClient/Client.php +++ b/src/Phpro/SoapClient/Client.php @@ -76,6 +76,10 @@ public function debugLastSoapRequest(): array * For backward compatibility with Symfony 4 * * @deprecated : We will remove this method in v2.0 in favour of injecting the internal dispatcher directly. + * + * @template T of Event\SoapEvent + * @param T $event + * @return T */ private function dispatch(Event\SoapEvent $event, string $name = null): Event\SoapEvent { @@ -95,8 +99,8 @@ private function dispatch(Event\SoapEvent $event, string $name = null): Event\So */ protected function call(string $method, RequestInterface $request): ResultInterface { - $requestEvent = new Event\RequestEvent($this, $method, $request); - $this->dispatch($requestEvent, Events::REQUEST); + $requestEvent = $this->dispatch(new Event\RequestEvent($this, $method, $request), Events::REQUEST); + $request = $requestEvent->getRequest(); try { $arguments = ($request instanceof MultiArgumentRequestInterface) ? $request->getArguments() : [$request]; @@ -115,8 +119,8 @@ protected function call(string $method, RequestInterface $request): ResultInterf throw $soapException; } - $this->dispatch(new Event\ResponseEvent($this, $requestEvent, $result), Events::RESPONSE); + $responseEvent = $this->dispatch(new Event\ResponseEvent($this, $requestEvent, $result), Events::RESPONSE); - return $result; + return $responseEvent->getResponse(); } } diff --git a/src/Phpro/SoapClient/Event/Dispatcher/EventDispatcherInterface.php b/src/Phpro/SoapClient/Event/Dispatcher/EventDispatcherInterface.php index f8e572d9..a324fc7f 100644 --- a/src/Phpro/SoapClient/Event/Dispatcher/EventDispatcherInterface.php +++ b/src/Phpro/SoapClient/Event/Dispatcher/EventDispatcherInterface.php @@ -14,10 +14,10 @@ interface EventDispatcherInterface { /** - * @param SoapEvent $event + * @template T of SoapEvent + * @param T $event * @param string|null $name Deprecated : will be removed in v2.0! - * - * @return SoapEvent + * @return T */ public function dispatch(SoapEvent $event, string $name = null): SoapEvent; } diff --git a/src/Phpro/SoapClient/Event/Dispatcher/PsrEventDispatcher.php b/src/Phpro/SoapClient/Event/Dispatcher/PsrEventDispatcher.php index 8390dbb6..c8da9b95 100644 --- a/src/Phpro/SoapClient/Event/Dispatcher/PsrEventDispatcher.php +++ b/src/Phpro/SoapClient/Event/Dispatcher/PsrEventDispatcher.php @@ -19,6 +19,12 @@ public function __construct(PsrEventDispatcherImplementation $dispatcher) $this->dispatcher = $dispatcher; } + /** + * @template T of SoapEvent + * @param T $event + * @param string|null $name Deprecated : will be removed in v2.0! + * @return T + */ public function dispatch(SoapEvent $event, string $name = null): SoapEvent { $this->dispatcher->dispatch($event); diff --git a/src/Phpro/SoapClient/Event/Dispatcher/SymfonyEventDispatcher.php b/src/Phpro/SoapClient/Event/Dispatcher/SymfonyEventDispatcher.php index 220b3012..d6ef0be4 100644 --- a/src/Phpro/SoapClient/Event/Dispatcher/SymfonyEventDispatcher.php +++ b/src/Phpro/SoapClient/Event/Dispatcher/SymfonyEventDispatcher.php @@ -25,6 +25,12 @@ public function __construct($eventDispatcher) $this->dispatcher = $eventDispatcher; } + /** + * @template T of SoapEvent + * @param T $event + * @param string|null $eventName Deprecated : will be removed in v2.0! + * @return T + */ public function dispatch(SoapEvent $event, string $eventName = null): SoapEvent { $interfacesImplemented = class_implements($this->dispatcher); diff --git a/src/Phpro/SoapClient/Event/RequestEvent.php b/src/Phpro/SoapClient/Event/RequestEvent.php index b896c6c5..fb313453 100644 --- a/src/Phpro/SoapClient/Event/RequestEvent.php +++ b/src/Phpro/SoapClient/Event/RequestEvent.php @@ -62,4 +62,9 @@ public function getClient(): Client { return $this->client; } + + public function registerRequest(RequestInterface $request): void + { + $this->request = $request; + } } diff --git a/src/Phpro/SoapClient/Event/ResponseEvent.php b/src/Phpro/SoapClient/Event/ResponseEvent.php index 884a505f..2fea06d7 100644 --- a/src/Phpro/SoapClient/Event/ResponseEvent.php +++ b/src/Phpro/SoapClient/Event/ResponseEvent.php @@ -17,7 +17,7 @@ class ResponseEvent extends SoapEvent protected $requestEvent; /** - * @var mixed + * @var ResultInterface */ protected $response; @@ -61,4 +61,9 @@ public function getClient(): Client { return $this->client; } + + public function registerResponse(ResultInterface $response): void + { + $this->response = $response; + } } diff --git a/test/PhproTest/SoapClient/Unit/Event/RequestEventTest.php b/test/PhproTest/SoapClient/Unit/Event/RequestEventTest.php new file mode 100644 index 00000000..3128ba73 --- /dev/null +++ b/test/PhproTest/SoapClient/Unit/Event/RequestEventTest.php @@ -0,0 +1,64 @@ +client = $this->prophesize(Client::class)->reveal(); + $this->request = $this->prophesize(RequestInterface::class)->reveal(); + $this->event = new RequestEvent($this->client, 'method', $this->request); + } + + /** @test */ + public function it_contains_a_client(): void + { + self::assertSame($this->client, $this->event->getClient()); + } + + /** @test */ + public function it_contains_a_request(): void + { + self::assertSame($this->request, $this->event->getRequest()); + } + + /** @test */ + public function it_contains_a_method(): void + { + self::assertSame('method', $this->event->getMethod()); + } + + /** @test */ + public function it_can_overwrite_request(): void + { + $new = $this->prophesize(RequestInterface::class)->reveal(); + $this->event->registerRequest($new); + + self::assertSame($new, $this->event->getRequest()); + self::assertNotSame($this->request, $this->event->getRequest()); + } +} diff --git a/test/PhproTest/SoapClient/Unit/Event/ResponseEventTest.php b/test/PhproTest/SoapClient/Unit/Event/ResponseEventTest.php new file mode 100644 index 00000000..d1f24547 --- /dev/null +++ b/test/PhproTest/SoapClient/Unit/Event/ResponseEventTest.php @@ -0,0 +1,75 @@ +client = $this->prophesize(Client::class)->reveal(); + $this->request = $this->prophesize(RequestInterface::class)->reveal(); + $this->response = $this->prophesize(ResultInterface::class)->reveal(); + $this->requestEvent = new RequestEvent($this->client, 'method', $this->request); + + $this->event = new ResponseEvent($this->client, $this->requestEvent, $this->response); + } + + /** @test */ + public function it_contains_a_client(): void + { + self::assertSame($this->client, $this->event->getClient()); + } + + /** @test */ + public function it_contains_a_request_event(): void + { + self::assertSame($this->requestEvent, $this->event->getRequestEvent()); + } + + /** @test */ + public function it_contains_a_response(): void + { + self::assertSame($this->response, $this->event->getResponse()); + } + + /** @test */ + public function it_can_overwrite_response(): void + { + $new = $this->prophesize(ResultInterface::class)->reveal(); + $this->event->registerResponse($new); + + self::assertSame($new, $this->event->getResponse()); + self::assertNotSame($this->response, $this->event->getResponse()); + } +}