Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock function to return an empty array. #173

Open
peterwilsoncc opened this issue Oct 17, 2022 · 2 comments
Open

Unable to mock function to return an empty array. #173

peterwilsoncc opened this issue Oct 17, 2022 · 2 comments

Comments

@peterwilsoncc
Copy link

Describe the bug
In Distributor, I am attempting to mock a function to return an empty array:

https://github.com/10up/distributor/blob/74ba9c5110de1dfa3415e76eab7d7ffc87b19e49/tests/php/SubscriptionsTest.php#L321-L325

However, the return value is coming back as NULL.

As an alternative approach, I've tried setting the return attribute as a callable function(){ return []; } but the same issue occurs.

To Reproduce

  1. As a reduced test case, create the test:
     public function test_return_empty_array() {
     	$post_id = 1;
     	\WP_Mock::userFunction(
     		'\Distributor\Utils\peters_test_function', [
     			'return' => [],
     		]
     	);
    
     	$this->assertSame( [], \Distributor\Utils\peters_test_function( $post_id ) );
     }
  2. Run phpunit --filter test_return_empty_array
  3. PHP Unit will throw a failure:
    PHPUnit 8.5.26 #StandWithUkraine
    
    F                                                                   1 / 1 (100%)
    
    Time: 2.11 seconds, Memory: 18.00 MB
    
    There was 1 failure:
    
    1) Distributor\SubscriptionsTest::test_return_empty_array
    Failed asserting that null is identical to Array &0 ().
    
    /vagrant/content/plugins/distributor/tests/php/SubscriptionsTest.php:18
    /vagrant/content/plugins/distributor/vendor/10up/wp_mock/php/WP_Mock/Tools/TestCase.php:307
    phpvfscomposer:///vagrant/content/plugins/distributor/vendor/phpunit/phpunit/phpunit:97
    

Expected behavior
Returned values retain type.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • PHPUnit 8.5.26
  • PHP 7.4.30
  • OS: Ubuntu 22.04 LTS

Additional context

peterwilsoncc added a commit to 10up/distributor that referenced this issue Oct 17, 2022
@BrianHenryIE
Copy link
Contributor

BrianHenryIE commented Jun 27, 2024

This works for a non-namespaced function:

public function test_return_empty_array() {
  $post_id = 1;
  \WP_Mock::userFunction( 'peters_test_function' )
    ->andReturnValues([[]]);

  $this->assertSame( array(), peters_test_function( $post_id ) );
}

This does not work:

\WP_Mock::userFunction( '\Distributor\Utils\peters_test_function' )
    ->andReturnValues([[]]);

but returns

Failed asserting that null is identical to Array &0 ().

rather then

Error : Call to undefined function Distributor\Utils\peters_test_function()

showing the issue rests in the namespace... somehow!

@BrianHenryIE
Copy link
Contributor

BrianHenryIE commented Jun 28, 2024

This works:

public function test_return_empty_array() {
    $post_id = 1;

    \WP_Mock::userFunction( '\\Distributor\\Utils\\peters_test_function' );

    \Patchwork\redefine('\\Distributor\\Utils\\peters_test_function', function($post_id) {
        return [];
    });

    $this->assertSame( [], \Distributor\Utils\peters_test_function( $post_id ) );
}

protected function tearDown(): void
{
    parent::tearDown();
    \Patchwork\restoreAll();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants