From 4d9c20ca5c0f063df1f3bb7a817b80d429c6b669 Mon Sep 17 00:00:00 2001 From: Matthew Goslett Date: Thu, 20 Jul 2017 08:22:03 +0200 Subject: [PATCH] events are no longer validated when received, only upon dispatch --- README.md | 2 +- changelog.md | 1 + src/EventManager.php | 20 ++------ tests/EventManagerTest.php | 96 +------------------------------------- 4 files changed, 6 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index e45456e..5de2a78 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/changelog.md b/changelog.md index f08aaf0..7205fa5 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/EventManager.php b/src/EventManager.php index 6c143e7..cd138af 100644 --- a/src/EventManager.php +++ b/src/EventManager.php @@ -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 */ @@ -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 */ @@ -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) { diff --git a/tests/EventManagerTest.php b/tests/EventManagerTest.php index 075701b..f8b3304 100644 --- a/tests/EventManagerTest.php +++ b/tests/EventManagerTest.php @@ -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') @@ -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);