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 features
Brain\Monkey::tearDownWp()-> after each test that use functions redefinition and WP features
Brain\Monkey\setUp()-> before each test that use functions redefinition and WP features
Brain\Monkey\tearDown()-> after each test, no matter if for functions redefinition or for also
Brain\Monkey\tearDown()have to be called after each test, and nothing before each test.
Brain\Monkey\setUp()have also to called before each test.
expectFired()does not really pair with
do_action(): this is why in Brain Monkey v2 the method
expectFired()has been replaced by the function
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.
expectAddedone 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_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:
staticclosures VS normal closures
callablelike, for example, second argument to
add_filter(), checked the received argument to be an actual callable PHP entity, using
callableand it accepts anything that looks like a callable.
[SomeClass::class, 'aMethod']would be accepted even if
SomeClassis not loaded at all, because it looks like a callable. Same goes for
[SomeClass::class, 'aMethod', 1]or even
Some\Name\Space\a Functionwill 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
SomeClassis 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
$someObjectis checked to have an accessible method named
apply_filters()is defined by Brain Monkey and it returns the first argument passed to it, just like WordPress:
apply_filtersbehavior, requiring the return value to be added to expectation to make the test pass again.
expectAppliedon applied filters does not break the default behavior of
apply_filtersbehavior, 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.
whenHappento 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_filter()didn't work: they were not provided at all with Brain Monkey v1 and required to be mocked "manually" .
andReturnFirstArg()to make the Mockery expectations return first argument received.
getMock()allows to do it without leaving "fluent interface chain", for example:
getMock()is not available for Brain Monkey expectations.
\InvalidArgumentException, and so on.