Skip to content

Commit

Permalink
Merge pull request #365 from veewee/events-allow-immutable-messages
Browse files Browse the repository at this point in the history
Allow immutable changes on event request / response
  • Loading branch information
veewee authored May 14, 2021
2 parents b0c9708 + cdd3c39 commit a91e2fe
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions src/Phpro/SoapClient/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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];
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions src/Phpro/SoapClient/Event/Dispatcher/PsrEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/Phpro/SoapClient/Event/RequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public function getClient(): Client
{
return $this->client;
}

public function registerRequest(RequestInterface $request): void
{
$this->request = $request;
}
}
7 changes: 6 additions & 1 deletion src/Phpro/SoapClient/Event/ResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ResponseEvent extends SoapEvent
protected $requestEvent;

/**
* @var mixed
* @var ResultInterface
*/
protected $response;

Expand Down Expand Up @@ -61,4 +61,9 @@ public function getClient(): Client
{
return $this->client;
}

public function registerResponse(ResultInterface $response): void
{
$this->response = $response;
}
}
64 changes: 64 additions & 0 deletions test/PhproTest/SoapClient/Unit/Event/RequestEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace PhproTest\SoapClient\Unit;

use Phpro\SoapClient\Client;
use Phpro\SoapClient\Event\RequestEvent;
use Phpro\SoapClient\Type\RequestInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

class RequestEventTest extends TestCase
{
use ProphecyTrait;

/**
* @var Client & ObjectProphecy
*/
private Client $client;

/**
* @var RequestInterface & ObjectProphecy
*/
private RequestInterface $request;

private RequestEvent $event;

protected function setUp(): void
{
$this->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());
}
}
75 changes: 75 additions & 0 deletions test/PhproTest/SoapClient/Unit/Event/ResponseEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace PhproTest\SoapClient\Unit;

use Phpro\SoapClient\Client;
use Phpro\SoapClient\Event\RequestEvent;
use Phpro\SoapClient\Event\ResponseEvent;
use Phpro\SoapClient\Type\RequestInterface;
use Phpro\SoapClient\Type\ResultInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

class ResponseEventTest extends TestCase
{
use ProphecyTrait;

/**
* @var Client & ObjectProphecy
*/
private Client $client;

/**
* @var RequestInterface & ObjectProphecy
*/
private RequestInterface $request;

/**
* @var ResultInterface & ObjectProphecy
*/
private ResultInterface $response;

private RequestEvent $requestEvent;
private ResponseEvent $event;

protected function setUp(): void
{
$this->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());
}
}

0 comments on commit a91e2fe

Please sign in to comment.