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

Stubbed global function can not be re-stubbed after finish_spying #27

Closed
mehamasum opened this issue Feb 8, 2022 · 0 comments · Fixed by #28
Closed

Stubbed global function can not be re-stubbed after finish_spying #27

mehamasum opened this issue Feb 8, 2022 · 0 comments · Fixed by #28

Comments

@mehamasum
Copy link
Contributor

If a global function is stubbed once and then the original function is restored through finish_spying, the same global function can not be stubbed again.

For example,

<?php

require_once '/vendor/antecedent/patchwork/Patchwork.php';

function bar(): int {
	return 0;
}


class Test_Foo extends PHPUnit\Framework\TestCase {
	function tearDown() {
		\Spies\finish_spying();
	}


	public function test_foo_one() {
		\Spies\stub_function( 'bar' )->that_returns( 1 );
		$this->assertEquals( 1, bar() );
	}

	public function test_foo_two() {
		\Spies\stub_function( 'bar' )->that_returns( 2 );
		$this->assertEquals( 2, bar() );
	}

}

In test_foo_one, I have stubbed bar to return 1.
In test_foo_two, I have stubbed bar to return 2.
If both tests are run one after another, the first test passes and the second doesn't.

There was 1 failure:

1) Test_Foo::test_foo_two
Failed asserting that 0 matches expected 2.

The second one's bar is not stubbed and returns the original return value.

So a stubbed global function can not be re-stubbed properly.

Note: if the tests are run individually, both tests pass.

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