Skip to content

Commit a91e2fe

Browse files
authored
Merge pull request #365 from veewee/events-allow-immutable-messages
Allow immutable changes on event request / response
2 parents b0c9708 + cdd3c39 commit a91e2fe

File tree

9 files changed

+174
-9
lines changed

9 files changed

+174
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"phpro/grumphp-shim": "~1.2.0",
4242
"phpspec/phpspec": "~7.0",
4343
"phpspec/prophecy-phpunit": "^2.0.1",
44-
"phpstan/phpstan": "^0.12.57",
44+
"phpstan/phpstan": "^0.12.87",
4545
"phpunit/phpunit": "~9.4",
4646
"psr/http-factory": "^1.0",
4747
"psr/http-message": "^1.0.1",

src/Phpro/SoapClient/Client.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function debugLastSoapRequest(): array
7676
* For backward compatibility with Symfony 4
7777
*
7878
* @deprecated : We will remove this method in v2.0 in favour of injecting the internal dispatcher directly.
79+
*
80+
* @template T of Event\SoapEvent
81+
* @param T $event
82+
* @return T
7983
*/
8084
private function dispatch(Event\SoapEvent $event, string $name = null): Event\SoapEvent
8185
{
@@ -95,8 +99,8 @@ private function dispatch(Event\SoapEvent $event, string $name = null): Event\So
9599
*/
96100
protected function call(string $method, RequestInterface $request): ResultInterface
97101
{
98-
$requestEvent = new Event\RequestEvent($this, $method, $request);
99-
$this->dispatch($requestEvent, Events::REQUEST);
102+
$requestEvent = $this->dispatch(new Event\RequestEvent($this, $method, $request), Events::REQUEST);
103+
$request = $requestEvent->getRequest();
100104

101105
try {
102106
$arguments = ($request instanceof MultiArgumentRequestInterface) ? $request->getArguments() : [$request];
@@ -115,8 +119,8 @@ protected function call(string $method, RequestInterface $request): ResultInterf
115119
throw $soapException;
116120
}
117121

118-
$this->dispatch(new Event\ResponseEvent($this, $requestEvent, $result), Events::RESPONSE);
122+
$responseEvent = $this->dispatch(new Event\ResponseEvent($this, $requestEvent, $result), Events::RESPONSE);
119123

120-
return $result;
124+
return $responseEvent->getResponse();
121125
}
122126
}

src/Phpro/SoapClient/Event/Dispatcher/EventDispatcherInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
interface EventDispatcherInterface
1515
{
1616
/**
17-
* @param SoapEvent $event
17+
* @template T of SoapEvent
18+
* @param T $event
1819
* @param string|null $name Deprecated : will be removed in v2.0!
19-
*
20-
* @return SoapEvent
20+
* @return T
2121
*/
2222
public function dispatch(SoapEvent $event, string $name = null): SoapEvent;
2323
}

src/Phpro/SoapClient/Event/Dispatcher/PsrEventDispatcher.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public function __construct(PsrEventDispatcherImplementation $dispatcher)
1919
$this->dispatcher = $dispatcher;
2020
}
2121

22+
/**
23+
* @template T of SoapEvent
24+
* @param T $event
25+
* @param string|null $name Deprecated : will be removed in v2.0!
26+
* @return T
27+
*/
2228
public function dispatch(SoapEvent $event, string $name = null): SoapEvent
2329
{
2430
$this->dispatcher->dispatch($event);

src/Phpro/SoapClient/Event/Dispatcher/SymfonyEventDispatcher.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public function __construct($eventDispatcher)
2525
$this->dispatcher = $eventDispatcher;
2626
}
2727

28+
/**
29+
* @template T of SoapEvent
30+
* @param T $event
31+
* @param string|null $eventName Deprecated : will be removed in v2.0!
32+
* @return T
33+
*/
2834
public function dispatch(SoapEvent $event, string $eventName = null): SoapEvent
2935
{
3036
$interfacesImplemented = class_implements($this->dispatcher);

src/Phpro/SoapClient/Event/RequestEvent.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ public function getClient(): Client
6262
{
6363
return $this->client;
6464
}
65+
66+
public function registerRequest(RequestInterface $request): void
67+
{
68+
$this->request = $request;
69+
}
6570
}

src/Phpro/SoapClient/Event/ResponseEvent.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ResponseEvent extends SoapEvent
1717
protected $requestEvent;
1818

1919
/**
20-
* @var mixed
20+
* @var ResultInterface
2121
*/
2222
protected $response;
2323

@@ -61,4 +61,9 @@ public function getClient(): Client
6161
{
6262
return $this->client;
6363
}
64+
65+
public function registerResponse(ResultInterface $response): void
66+
{
67+
$this->response = $response;
68+
}
6469
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhproTest\SoapClient\Unit;
6+
7+
use Phpro\SoapClient\Client;
8+
use Phpro\SoapClient\Event\RequestEvent;
9+
use Phpro\SoapClient\Type\RequestInterface;
10+
use PHPUnit\Framework\TestCase;
11+
use Prophecy\PhpUnit\ProphecyTrait;
12+
use Prophecy\Prophecy\ObjectProphecy;
13+
14+
class RequestEventTest extends TestCase
15+
{
16+
use ProphecyTrait;
17+
18+
/**
19+
* @var Client & ObjectProphecy
20+
*/
21+
private Client $client;
22+
23+
/**
24+
* @var RequestInterface & ObjectProphecy
25+
*/
26+
private RequestInterface $request;
27+
28+
private RequestEvent $event;
29+
30+
protected function setUp(): void
31+
{
32+
$this->client = $this->prophesize(Client::class)->reveal();
33+
$this->request = $this->prophesize(RequestInterface::class)->reveal();
34+
$this->event = new RequestEvent($this->client, 'method', $this->request);
35+
}
36+
37+
/** @test */
38+
public function it_contains_a_client(): void
39+
{
40+
self::assertSame($this->client, $this->event->getClient());
41+
}
42+
43+
/** @test */
44+
public function it_contains_a_request(): void
45+
{
46+
self::assertSame($this->request, $this->event->getRequest());
47+
}
48+
49+
/** @test */
50+
public function it_contains_a_method(): void
51+
{
52+
self::assertSame('method', $this->event->getMethod());
53+
}
54+
55+
/** @test */
56+
public function it_can_overwrite_request(): void
57+
{
58+
$new = $this->prophesize(RequestInterface::class)->reveal();
59+
$this->event->registerRequest($new);
60+
61+
self::assertSame($new, $this->event->getRequest());
62+
self::assertNotSame($this->request, $this->event->getRequest());
63+
}
64+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhproTest\SoapClient\Unit;
6+
7+
use Phpro\SoapClient\Client;
8+
use Phpro\SoapClient\Event\RequestEvent;
9+
use Phpro\SoapClient\Event\ResponseEvent;
10+
use Phpro\SoapClient\Type\RequestInterface;
11+
use Phpro\SoapClient\Type\ResultInterface;
12+
use PHPUnit\Framework\TestCase;
13+
use Prophecy\PhpUnit\ProphecyTrait;
14+
use Prophecy\Prophecy\ObjectProphecy;
15+
16+
class ResponseEventTest extends TestCase
17+
{
18+
use ProphecyTrait;
19+
20+
/**
21+
* @var Client & ObjectProphecy
22+
*/
23+
private Client $client;
24+
25+
/**
26+
* @var RequestInterface & ObjectProphecy
27+
*/
28+
private RequestInterface $request;
29+
30+
/**
31+
* @var ResultInterface & ObjectProphecy
32+
*/
33+
private ResultInterface $response;
34+
35+
private RequestEvent $requestEvent;
36+
private ResponseEvent $event;
37+
38+
protected function setUp(): void
39+
{
40+
$this->client = $this->prophesize(Client::class)->reveal();
41+
$this->request = $this->prophesize(RequestInterface::class)->reveal();
42+
$this->response = $this->prophesize(ResultInterface::class)->reveal();
43+
$this->requestEvent = new RequestEvent($this->client, 'method', $this->request);
44+
45+
$this->event = new ResponseEvent($this->client, $this->requestEvent, $this->response);
46+
}
47+
48+
/** @test */
49+
public function it_contains_a_client(): void
50+
{
51+
self::assertSame($this->client, $this->event->getClient());
52+
}
53+
54+
/** @test */
55+
public function it_contains_a_request_event(): void
56+
{
57+
self::assertSame($this->requestEvent, $this->event->getRequestEvent());
58+
}
59+
60+
/** @test */
61+
public function it_contains_a_response(): void
62+
{
63+
self::assertSame($this->response, $this->event->getResponse());
64+
}
65+
66+
/** @test */
67+
public function it_can_overwrite_response(): void
68+
{
69+
$new = $this->prophesize(ResultInterface::class)->reveal();
70+
$this->event->registerResponse($new);
71+
72+
self::assertSame($new, $this->event->getResponse());
73+
self::assertNotSame($this->response, $this->event->getResponse());
74+
}
75+
}

0 commit comments

Comments
 (0)