-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Spree::Event's test interface to run only selected listeners #4204
Merged
waiting-for-dev
merged 1 commit into
solidusio:master
from
nebulab:waiting-for-dev/events_testability
Dec 13, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spree/event' | ||
|
||
module Spree | ||
module Event | ||
# Test helpers for {Spree::Event} | ||
# | ||
# This module defines test helpers methods for {Spree::Event}. They can be | ||
# made available to {Spree::Event} when {Spree::Event.enable_test_interface} | ||
# is called. | ||
# | ||
# If you prefer, you can directly call them from | ||
# `Spree::Event::TestInterface}. | ||
module TestInterface | ||
# @see {Spree::Event::TestInterface} | ||
module Methods | ||
# Perform only given listeners for the duration of the block | ||
# | ||
# Temporarily deactivate all subscribed listeners and listen only to the | ||
# provided ones for the duration of the block. | ||
# | ||
# @example | ||
# Spree::Event.enable_test_interface | ||
# | ||
# listener1 = Spree::Event.subscribe('foo') { do_something } | ||
# listener2 = Spree::Event.subscribe('foo') { do_something_else } | ||
# | ||
# Spree::Event.performing_only(listener1) do | ||
# Spree::Event.fire('foo') # This will run only `listener1` | ||
# end | ||
# | ||
# Spree::Event.fire('foo') # This will run both `listener1` & `listener2` | ||
# | ||
# {Spree::Event::Subscriber} modules can also be given to unsubscribe from | ||
# all listeners generated from it: | ||
# | ||
# @example | ||
# Spree::Event.performing_only(EmailSubscriber) {} | ||
# | ||
# You can gain more fine-grained control thanks to | ||
# {Spree::Event::Subscribe#listeners}: | ||
# | ||
# @example | ||
# Spree::Event.performing_only(EmailSubscriber.listeners('order_finalized')) {} | ||
# | ||
# You can mix different ways of specifying listeners without problems: | ||
# | ||
# @example | ||
# Spree::Event.performing_only(EmailSubscriber, listener1) {} | ||
# | ||
# @param listeners_and_subscribers [Spree::Event::Listener, | ||
# Array<Spree::Event::Listener>, Spree::Event::Subscriber] | ||
# @yield While the block executes only provided listeners will run | ||
def performing_only(*listeners_and_subscribers) | ||
adapter_in_use = Spree::Event.default_adapter | ||
listeners = listeners_and_subscribers.flatten.map(&:listeners) | ||
Spree::Config.events.adapter = adapter_in_use.with_listeners(listeners.flatten) | ||
yield | ||
ensure | ||
Spree::Config.events.adapter = adapter_in_use | ||
end | ||
|
||
# Perform no listeners for the duration of the block | ||
# | ||
# It's a specialized version of {#performing_only} that provides no | ||
# listeners. | ||
# | ||
# @yield While the block executes no listeners will run | ||
# | ||
# @see Spree::Event::TestInterface#performing_only | ||
def performing_nothing(&block) | ||
performing_only(&block) | ||
end | ||
end | ||
|
||
extend Methods | ||
end | ||
|
||
# Adds test methods to {Spree::Event} | ||
# | ||
# @raise [RuntimeError] when {Spree::Event::Configuration#adapter} is set to | ||
# the legacy adapter {Spree::Event::Adapters::ActiveSupportNotifications}. | ||
def enable_test_interface | ||
raise <<~MSG if deprecation_handler.legacy_adapter?(default_adapter) | ||
Spree::Event's test interface is not supported when using the deprecated | ||
adapter 'Spree::Event::Adapters::ActiveSupportNotifications'. | ||
MSG | ||
|
||
extend(TestInterface::Methods) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is needed in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR adds other caller locations that have no direct access to the used adapter. It's easier to default it to the global adapter, instead of making the same operation from the caller each time. E.g.:
solidus/core/lib/spree/event/subscriber_registry.rb
Line 56 in 5881d86
solidus/core/spec/lib/spree/event/subscriber_spec.rb
Line 95 in 5881d86
solidus/core/spec/lib/spree/event/test_interface_spec.rb
Line 37 in 5881d86
solidus/core/spec/lib/spree/event/subscriber_registry_spec.rb
Line 120 in 5881d86