closures.php 863 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /**
  3. * Test do_action() and related functions
  4. *
  5. * @group hooks
  6. */
  7. class Tests_Actions_Closures extends WP_UnitTestCase {
  8. /**
  9. * @ticket 10493
  10. */
  11. function test_action_closure() {
  12. $tag = 'test_action_closure';
  13. $closure = function($a, $b) { $GLOBALS[$a] = $b;};
  14. add_action($tag, $closure, 10, 2);
  15. $this->assertSame( 10, has_action($tag, $closure) );
  16. $context = array( rand_str(), rand_str() );
  17. do_action($tag, $context[0], $context[1]);
  18. $this->assertSame($GLOBALS[$context[0]], $context[1]);
  19. $tag2 = 'test_action_closure_2';
  20. $closure2 = function() { $GLOBALS['closure_no_args'] = true;};
  21. add_action($tag2, $closure2);
  22. $this->assertSame( 10, has_action($tag2, $closure2) );
  23. do_action($tag2);
  24. $this->assertTrue($GLOBALS['closure_no_args']);
  25. remove_action( $tag, $closure );
  26. remove_action( $tag2, $closure2 );
  27. }
  28. }