Brain\Monkey::setUp()
-> before each test that use only functions redefinition (no WP features)Brain\Monkey::tearDown()
-> after each test that use only functions redefinition (no WP features)Brain\Monkey::setUpWp()
-> before each test that use functions redefinition and WP featuresBrain\Monkey::tearDownWp()
-> after each test that use functions redefinition and WP featuresBrain\Monkey\setUp()
-> before each test that use functions redefinition and WP featuresBrain\Monkey\tearDown()
-> after each test, no matter if for functions redefinition or for alsoBrain\Monkey\tearDown()
have to be called after each test, and nothing before each test.Brain\Monkey\setUp()
have also to called before each test.Brain\Monkey
classBrain\Monkey\Functions
,Brain\Monkey\WP\Actions
or Brain\Monkey\WP\Filters
Filters::expectAdded()
Filters::expectApplied()
add_filter()
/ apply_filters()
Actions::expectAdded()
Actions::expectFired()
expectAdded()
pairs with add_action()
, but expectFired()
does not really pair with do_action()
: this is why in Brain Monkey v2 the method expectFired()
has been replaced by the function expectDone()
.Brain\Monkey\Functions\expect()
Brain\Monkey\Actions\expectAdded()
Brain\Monkey\Actions\expectDone()
Brain\Monkey\Filters\expectAdded()
Brain\Monkey\Filters\expectApplied()
Actions::expectAdded()
was used, the test does not fail unless something like Actions::expectAdded('init')->once()
was used, which made the test pass only if add_action( 'init' )
was called once.->zeroOrMoreTimes()
as expectation on number of times a method is called, so when the expectation is called zero times, that's a valid outcome.expectAdded
one could expect the test to fail if that thing did not happened), and also made tests unnecessarily verbose.->atLeast()->once()
so, for example, the test above fails in Brain Monkey v2 if MyClass::doSomething()
does not call add_action('init')
at least once.has_action()
/ has_filter()
, functions, to test if some portion of code have added some hooks."function()"
, in Brain Monkey v2 closure string representations also contain the parameters used in the closure signature:static
closures VS normal closurescallable
checkcallable
like, for example, second argument to add_action()
/ add_filter()
, checked the received argument to be an actual callable PHP entity, using is_callable
:callable
and it accepts anything that looks like a callable.[SomeClass::class, 'aMethod']
would be accepted even if SomeClass
is not loaded at all, because it looks like a callable. Same goes for 'Some\Name\Space\aFunction'
.[SomeClass::class, 'a-Method']
or [SomeClass::class, 'aMethod', 1]
or even Some\Name\Space\a Function
will throw an exception because method and function names can't contain hyphens or spaces and when a callback is made of an array, it must have exactly two arguments.[SomeClass::class, 'aMethod']
if the class SomeClass
is available, Brain Monkey checks it to have an accessible method named aMethod
, and raise an exception if not, but will not do any check if the class is not available.[$someObject, 'aMethod']
, the instance of $someObject
is checked to have an accessible method named aMethod
.apply_filters
Default Behaviorapply_filters()
is defined by Brain Monkey and it returns the first argument passed to it, just like WordPress:apply_filters
behavior, requiring the return value to be added to expectation to make the test pass again.expectApplied
on applied filters does not break the default behavior of apply_filters
behavior, if no return expectations are added.andReturnFirstArg()
expectation method (more on this below).andReturnFirstArg()
used in combination with Mockery methods zeroOrMoreTimes()->withAnyArgs()
allows to create a "catch all" behavior for filters when a return expectation has been added, without having to create specific expectations for each of the possible arguments a filter might receive.__return_true
__return_false
__return_null
__return_empty_array
__return_empty_string
__return_zero
trailingslashit
untrailingslashit
doing_action()
and doing_filter()
whenHappen
to respond to actions, inside the expectation callback, the function current_filter()
in Brain Monkey v1 used to correctly resolve to the action / filter being executed.doing_action()
and doing_filter()
didn't work: they were not provided at all with Brain Monkey v1 and required to be mocked "manually" .andReturnFirstArg()
andReturnFirstArg()
to make the Mockery expectations return first argument received.andAlsoExpectIt()
getMock()
allows to do it without leaving "fluent interface chain", for example:getMock()
is not available for Brain Monkey expectations.andAlsoExpectIt()
:\RuntimeException
or \InvalidArgumentException
, and so on.Brain\Monkey\Exception
.