mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-29 21:40:15 +00:00
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches. > > The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used. > > When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`. > > The snake_case methods will automatically be called by PHPUnit. > > > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`. Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case. Follow-up to [51559-51567]. Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov. See #46149. git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
178 lines
5.5 KiB
PHP
178 lines
5.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Test the do_action method of WP_Hook
|
|
*
|
|
* @group hooks
|
|
* @covers WP_Hook::do_action
|
|
*/
|
|
class Tests_Hooks_DoAction extends WP_UnitTestCase {
|
|
private $events = array();
|
|
private $action_output = '';
|
|
private $hook;
|
|
|
|
public function set_up() {
|
|
parent::set_up();
|
|
$this->events = array();
|
|
}
|
|
|
|
public function test_do_action_with_callback() {
|
|
$a = new MockAction();
|
|
$callback = array( $a, 'action' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = rand( 1, 100 );
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_multiple_calls() {
|
|
$a = new MockAction();
|
|
$callback = array( $a, 'filter' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = rand( 1, 100 );
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 2, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_multiple_callbacks_on_same_priority() {
|
|
$a = new MockAction();
|
|
$b = new MockAction();
|
|
$callback_one = array( $a, 'filter' );
|
|
$callback_two = array( $b, 'filter' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = rand( 1, 100 );
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
|
|
$hook->add_filter( $tag, $callback_two, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_multiple_callbacks_on_different_priorities() {
|
|
$a = new MockAction();
|
|
$b = new MockAction();
|
|
$callback_one = array( $a, 'filter' );
|
|
$callback_two = array( $b, 'filter' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = rand( 1, 100 );
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
|
|
$hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
$this->assertSame( 1, $a->get_call_count() );
|
|
}
|
|
|
|
public function test_do_action_with_no_accepted_args() {
|
|
$callback = array( $this, '_action_callback' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = 0;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertEmpty( $this->events[0]['args'] );
|
|
}
|
|
|
|
public function test_do_action_with_one_accepted_arg() {
|
|
$callback = array( $this, '_action_callback' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = 1;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertCount( 1, $this->events[0]['args'] );
|
|
}
|
|
|
|
public function test_do_action_with_more_accepted_args() {
|
|
$callback = array( $this, '_action_callback' );
|
|
$hook = new WP_Hook();
|
|
$tag = __FUNCTION__;
|
|
$priority = rand( 1, 100 );
|
|
$accepted_args = 1000;
|
|
$arg = __FUNCTION__ . '_arg';
|
|
|
|
$hook->add_filter( $tag, $callback, $priority, $accepted_args );
|
|
$hook->do_action( array( $arg ) );
|
|
|
|
$this->assertCount( 1, $this->events[0]['args'] );
|
|
}
|
|
|
|
public function test_do_action_doesnt_change_value() {
|
|
$this->hook = new WP_Hook();
|
|
$this->action_output = '';
|
|
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value1' ), 10, 1 );
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value3' ), 11, 1 );
|
|
|
|
$this->hook->do_action( array( 'a' ) );
|
|
|
|
$this->assertSame( 'a1-b1b3-a2a3', $this->action_output );
|
|
}
|
|
|
|
public function _filter_do_action_doesnt_change_value1( $value ) {
|
|
$this->action_output .= $value . 1;
|
|
return 'x1';
|
|
}
|
|
public function _filter_do_action_doesnt_change_value2( $value ) {
|
|
$this->hook->remove_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10 );
|
|
|
|
$this->action_output .= '-';
|
|
$this->hook->do_action( array( 'b' ) );
|
|
$this->action_output .= '-';
|
|
|
|
$this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );
|
|
|
|
$this->action_output .= $value . 2;
|
|
|
|
return 'x2';
|
|
}
|
|
|
|
public function _filter_do_action_doesnt_change_value3( $value ) {
|
|
$this->action_output .= $value . 3;
|
|
return 'x3';
|
|
}
|
|
|
|
/**
|
|
* Use this rather than MockAction so we can test callbacks with no args
|
|
*
|
|
* @param mixed ...$args Optional arguments passed to the action.
|
|
*/
|
|
public function _action_callback( ...$args ) {
|
|
$this->events[] = array(
|
|
'action' => __FUNCTION__,
|
|
'args' => $args,
|
|
);
|
|
}
|
|
}
|