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

Commit

Permalink
events are no longer validated when received, only upon dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgoslett committed Jul 20, 2017
1 parent 60ffc38 commit 4d9c20c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function translate($message);

## Validators

A validator is an optional component used to validate an incoming event.
A validator is an optional component used to validate an event upon dispatch.

### JSONSchemaEventValidator

Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 4.0.2 - 2017-07-20

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

## 4.0.1 - 2017-07-18

Expand Down
20 changes: 3 additions & 17 deletions src/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getListenExprFailHandler()
}

/**
* Set the handler which is called when an event is dispatched or received but fails validation.
* Set the handler which is called when an event is dispatched but fails validation.
*
* @param callable $handler
*/
Expand All @@ -174,7 +174,7 @@ public function setValidationFailHandler(callable $handler)
}

/**
* Return the handler which is called when an event is dispatched or received but fails validation.
* Return the handler which is called when an event is dispatched but fails validation.
*
* @return callable|null
*/
Expand Down Expand Up @@ -225,21 +225,7 @@ public function handleSubscribeCallback($message, $expr, callable $handler)
// we were able to translate the message into an event
if ($event->matches($expr)) {
// the event matches the listen expression
if ($this->validator === null) {
// nothing to validate
call_user_func($handler, $event);
} else {
$result = $this->validator->validate($event);
if ($result->passes()) {
// event validates!
call_user_func($handler, $event);
} else {
// pass to validation fail handler?
if ($this->validationFailHandler) {
call_user_func($this->validationFailHandler, $result);
}
}
}
call_user_func($handler, $event);
} else {
// pass to listen expr fail handler?
if ($this->listenExprFailHandler) {
Expand Down
96 changes: 1 addition & 95 deletions tests/EventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,7 @@ public function testHandleSubscribeCallback()
->with('message payload')
->andReturn($event);

$validator = Mockery::mock(EventValidatorInterface::class);

$validationResult = new ValidationResult($validator, $event, true);

$validator->shouldReceive('validate')
->with($event)
->andReturn($validationResult);

$manager = new EventManager($adapter, $translator, $validator);
$manager = new EventManager($adapter, $translator);

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

public function testHandleSubscribeCallbackWithoutValidator()
{
$event = Mockery::mock(EventInterface::class);
$event->shouldReceive('matches')
->with('user/created')
->andReturn(true);

$adapter = Mockery::mock(PubSubAdapterInterface::class);

$translator = Mockery::mock(MessageTranslatorInterface::class);
$translator->shouldReceive('translate')
->with('message payload')
->andReturn($event);

$manager = new EventManager($adapter, $translator);

$handler = Mockery::mock(\stdClass::class);
$handler->shouldReceive('handle')
->with($event);

$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
}

public function testHandleSubscribeCallbackWhenValidationFails()
{
$event = Mockery::mock(EventInterface::class);
$event->shouldReceive('matches')
->with('user/created')
->andReturn(true);

$adapter = Mockery::mock(PubSubAdapterInterface::class);

$translator = Mockery::mock(MessageTranslatorInterface::class);
$translator->shouldReceive('translate')
->with('message payload')
->andReturn($event);

$validator = Mockery::mock(EventValidatorInterface::class);

$validationResult = new ValidationResult($validator, $event, false, ['Required properties missing: ["user"]']);

$validator->shouldReceive('validate')
->with($event)
->andReturn($validationResult);

$manager = new EventManager($adapter, $translator, $validator);

$handler = Mockery::mock(\stdClass::class);

$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
}

public function testHandleSubscribeCallbackWhenValidationFailsAndHandlerIsCalled()
{
$event = Mockery::mock(EventInterface::class);
$event->shouldReceive('matches')
->with('user/created')
->andReturn(true);

$adapter = Mockery::mock(PubSubAdapterInterface::class);

$translator = Mockery::mock(MessageTranslatorInterface::class);
$translator->shouldReceive('translate')
->with('message payload')
->andReturn($event);

$validator = Mockery::mock(EventValidatorInterface::class);

$validationResult = new ValidationResult($validator, $event, false, ['Required properties missing: ["user"]']);

$validator->shouldReceive('validate')
->with($event)
->andReturn($validationResult);

$manager = new EventManager($adapter, $translator, $validator);

$validationFailHandler = Mockery::mock(\stdClass::class);
$validationFailHandler->shouldReceive('handle')
->with($validationResult);
$manager->setValidationFailHandler([$validationFailHandler, 'handle']);

$handler = Mockery::mock(\stdClass::class);

$manager->handleSubscribeCallback('message payload', 'user/created', [$handler, 'handle']);
}

public function testDispatch()
{
$adapter = Mockery::mock(PubSubAdapterInterface::class);
Expand Down

0 comments on commit 4d9c20c

Please sign in to comment.