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

Expectation not counting mocked methods #5919

Open
dingo-d opened this issue Aug 7, 2024 · 0 comments
Open

Expectation not counting mocked methods #5919

dingo-d opened this issue Aug 7, 2024 · 0 comments
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/9 Something affects PHPUnit 9 version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11

Comments

@dingo-d
Copy link

dingo-d commented Aug 7, 2024

Q A
PHPUnit version 9.6.20 / 10.5.29 / 11.3.0
PHP version 8.3.4
Installation Method Composer

Summary

Expectation of the number of calls for mocked methods is not correct

Current behavior

When setting up expectations on mocked methods on mocks, the call number is always 0.

How to reproduce

I've created a public repo that can be installed using composer: https://github.com/dingo-d/phpunit-expectation-count-issue.

On versions 9 and 10 (I know 9 is out of bugfix support, but that was the one I tested on first) I got the same error:

Expectation failed for method name is "count" when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

On version 11 I got a different error:

Error: Typed property MockObject_C_c176faab::$__phpunit_state must not be accessed before initialization

/Users/dzoljom/Projects/Personal/phpunit-test/src/A.php:15
/Users/dzoljom/Projects/Personal/phpunit-test/tests/ATest.php:36

So I'm not sure what is the problem here.

Expected behavior

When I used xDebug to inspect the code when I ran the test, I could see that when I passed the mocked method as a dependency to the class under test, the mocked services were provided ($cMock and $bMock) so their mocked methods are used and called instead of the actual classes.

So I expect (no pun intended) that the call count to these methods I mocked will increase, and I'll get the info that the methods were called once for each service class.

This is just a POC from another project that has a similar structure, but the call count is different in each service, and this happens there as well (the reported count is 0 when it shouldn't be). As to why I'm doing these mocks, this is a simplified version, in my version the mocked method on the services does an API call, so I need to avoid doing that in my tests ofc (hence mocking this).

I'm not sure if I'm doing something wrong with the expectations, or if this is some edge-case bug 🤷🏼‍♂️

@dingo-d dingo-d added the type/bug Something is broken label Aug 7, 2024
@sebastianbergmann sebastianbergmann added feature/test-doubles Test Stubs and Mock Objects version/9 Something affects PHPUnit 9 version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11 labels Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/9 Something affects PHPUnit 9 version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11
Projects
None yet
Development

No branches or pull requests

2 participants