Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 4d9c20c

Browse files
events are no longer validated when received, only upon dispatch
1 parent 60ffc38 commit 4d9c20c

File tree

4 files changed

+6
-113
lines changed

4 files changed

+6
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function translate($message);
274274

275275
## Validators
276276

277-
A validator is an optional component used to validate an incoming event.
277+
A validator is an optional component used to validate an event upon dispatch.
278278

279279
### JSONSchemaEventValidator
280280

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 4.0.2 - 2017-07-20
44

55
* Fix events not being validated correctly when attribute injectors are used
6+
* Events are no longer validated when received, only upon dispatch
67

78
## 4.0.1 - 2017-07-18
89

src/EventManager.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function getListenExprFailHandler()
164164
}
165165

166166
/**
167-
* Set the handler which is called when an event is dispatched or received but fails validation.
167+
* Set the handler which is called when an event is dispatched but fails validation.
168168
*
169169
* @param callable $handler
170170
*/
@@ -174,7 +174,7 @@ public function setValidationFailHandler(callable $handler)
174174
}
175175

176176
/**
177-
* Return the handler which is called when an event is dispatched or received but fails validation.
177+
* Return the handler which is called when an event is dispatched but fails validation.
178178
*
179179
* @return callable|null
180180
*/
@@ -225,21 +225,7 @@ public function handleSubscribeCallback($message, $expr, callable $handler)
225225
// we were able to translate the message into an event
226226
if ($event->matches($expr)) {
227227
// the event matches the listen expression
228-
if ($this->validator === null) {
229-
// nothing to validate
230-
call_user_func($handler, $event);
231-
} else {
232-
$result = $this->validator->validate($event);
233-
if ($result->passes()) {
234-
// event validates!
235-
call_user_func($handler, $event);
236-
} else {
237-
// pass to validation fail handler?
238-
if ($this->validationFailHandler) {
239-
call_user_func($this->validationFailHandler, $result);
240-
}
241-
}
242-
}
228+
call_user_func($handler, $event);
243229
} else {
244230
// pass to listen expr fail handler?
245231
if ($this->listenExprFailHandler) {

tests/EventManagerTest.php

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,7 @@ public function testHandleSubscribeCallback()
162162
->with('message payload')
163163
->andReturn($event);
164164

165-
$validator = Mockery::mock(EventValidatorInterface::class);
166-
167-
$validationResult = new ValidationResult($validator, $event, true);
168-
169-
$validator->shouldReceive('validate')
170-
->with($event)
171-
->andReturn($validationResult);
172-
173-
$manager = new EventManager($adapter, $translator, $validator);
165+
$manager = new EventManager($adapter, $translator);
174166

175167
$handler = Mockery::mock(\stdClass::class);
176168
$handler->shouldReceive('handle')
@@ -266,92 +258,6 @@ public function testHandleSubscribeCallbackWhenEventDoesNotMatchListenExpression
266258
$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
267259
}
268260

269-
public function testHandleSubscribeCallbackWithoutValidator()
270-
{
271-
$event = Mockery::mock(EventInterface::class);
272-
$event->shouldReceive('matches')
273-
->with('user/created')
274-
->andReturn(true);
275-
276-
$adapter = Mockery::mock(PubSubAdapterInterface::class);
277-
278-
$translator = Mockery::mock(MessageTranslatorInterface::class);
279-
$translator->shouldReceive('translate')
280-
->with('message payload')
281-
->andReturn($event);
282-
283-
$manager = new EventManager($adapter, $translator);
284-
285-
$handler = Mockery::mock(\stdClass::class);
286-
$handler->shouldReceive('handle')
287-
->with($event);
288-
289-
$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
290-
}
291-
292-
public function testHandleSubscribeCallbackWhenValidationFails()
293-
{
294-
$event = Mockery::mock(EventInterface::class);
295-
$event->shouldReceive('matches')
296-
->with('user/created')
297-
->andReturn(true);
298-
299-
$adapter = Mockery::mock(PubSubAdapterInterface::class);
300-
301-
$translator = Mockery::mock(MessageTranslatorInterface::class);
302-
$translator->shouldReceive('translate')
303-
->with('message payload')
304-
->andReturn($event);
305-
306-
$validator = Mockery::mock(EventValidatorInterface::class);
307-
308-
$validationResult = new ValidationResult($validator, $event, false, ['Required properties missing: ["user"]']);
309-
310-
$validator->shouldReceive('validate')
311-
->with($event)
312-
->andReturn($validationResult);
313-
314-
$manager = new EventManager($adapter, $translator, $validator);
315-
316-
$handler = Mockery::mock(\stdClass::class);
317-
318-
$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
319-
}
320-
321-
public function testHandleSubscribeCallbackWhenValidationFailsAndHandlerIsCalled()
322-
{
323-
$event = Mockery::mock(EventInterface::class);
324-
$event->shouldReceive('matches')
325-
->with('user/created')
326-
->andReturn(true);
327-
328-
$adapter = Mockery::mock(PubSubAdapterInterface::class);
329-
330-
$translator = Mockery::mock(MessageTranslatorInterface::class);
331-
$translator->shouldReceive('translate')
332-
->with('message payload')
333-
->andReturn($event);
334-
335-
$validator = Mockery::mock(EventValidatorInterface::class);
336-
337-
$validationResult = new ValidationResult($validator, $event, false, ['Required properties missing: ["user"]']);
338-
339-
$validator->shouldReceive('validate')
340-
->with($event)
341-
->andReturn($validationResult);
342-
343-
$manager = new EventManager($adapter, $translator, $validator);
344-
345-
$validationFailHandler = Mockery::mock(\stdClass::class);
346-
$validationFailHandler->shouldReceive('handle')
347-
->with($validationResult);
348-
$manager->setValidationFailHandler([$validationFailHandler, 'handle']);
349-
350-
$handler = Mockery::mock(\stdClass::class);
351-
352-
$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
353-
}
354-
355261
public function testDispatch()
356262
{
357263
$adapter = Mockery::mock(PubSubAdapterInterface::class);

0 commit comments

Comments
 (0)