`expectActionAdded( array( Functions::type( class-string ), 'method' ) )` fails when same method name used twice

Open
#268 2 comments 1 reaction 0 assignees View on GitHub

@faisalahammad is already working on this.

Since Jul 19, 2026.

  • #270 by @faisalahammad — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
50/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
php
Domain
testing

Research direction

Run phpunit --filter=testMultipleActions and start at WP_Mock::expectActionAdded() and the Functions::type() handling used by the two replication tests. The fix is done when both same-method and distinct-method cases pass, while the existing AnyInstance case remains passing.

Written by the indexing model from the issue text.

Description

Bug report

I've started migrating code to use \WP_Mock\Functions::type() #254 but have run into an unusual failure.

When using \WP_Mock\Functions::type() in an array in \WP_Mock::expectActionAdded(), if the method name is used twice, it fails.

Replication steps

protected function wpMockSetStrictModeForTest(): void {
    $property = new \ReflectionProperty( 'WP_Mock', '__strict_mode' );
    $property->setAccessible( true );
    $property->setValue( true );
}

// Fails.
public function testMultipleActionsTypeSameMethod(): void
{
    $this->wpMockSetStrictModeForTest();

     \WP_Mock::expectActionAdded(
       'init',
       array( \WP_Mock\Functions::type( \WP_Mock\Tests\Mocks\SampleClass::class ), 'action' )
     );

     \WP_Mock::expectActionAdded(
       'init',
       array( \WP_Mock\Functions::type( \WP_Mock\Tests\Mocks\SampleSubClass::class ), 'action' )
     );

    add_action( 'init', array( new \WP_Mock\Tests\Mocks\SampleClass(), 'action' ) );
    add_action( 'init', array( new \WP_Mock\Tests\Mocks\SampleSubClass(), 'action' ) );

    $this->assertConditionsMet();
}

// Passes.
public function testMultipleActionsTypeDistinctMethod(): void
{
    $this->wpMockSetStrictModeForTest();

     \WP_Mock::expectActionAdded(
       'init',
       array( \WP_Mock\Functions::type( \WP_Mock\Tests\Mocks\SampleClass::class ), 'action' )
     );

     \WP_Mock::expectActionAdded(
       'init',
       array( \WP_Mock\Functions::type( \WP_Mock\Tests\Mocks\SampleSubClass::class ), 'action2' )
     );

    add_action( 'init', array( new \WP_Mock\Tests\Mocks\SampleClass(), 'action' ) );
    add_action( 'init', array( new \WP_Mock\Tests\Mocks\SampleSubClass(), 'action2' ) );

    $this->assertConditionsMet();
}

phpunit --filter=testMultipleActions

Expected behavior

The method name should have no effect on the assertion.


// Passes.
public function testMultipleActionsAnyInstance(): void
{
    $this->wpMockSetStrictModeForTest();

    \WP_Mock::expectActionAdded(
        'init',
        array( new AnyInstance( \WP_Mock\Tests\Mocks\SampleClass::class ), 'action' )
    );

    \WP_Mock::expectActionAdded(
        'init',
        array( new AnyInstance( \WP_Mock\Tests\Mocks\SampleSubClass::class ), 'action' ),
    );

    add_action( 'init', array( new \WP_Mock\Tests\Mocks\SampleClass(), 'action' ) );
    add_action( 'init', array( new \WP_Mock\Tests\Mocks\SampleSubClass(), 'action' ) );

    $this->assertConditionsMet();
}

I haven't taken time to try fix this yet. Maybe @badasswp might know quickly what's up.

Dominant language
PHP
Stars
714
Forks
72
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 10up/wp_mock

All issues in 10up/wp_mock

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.