Skip to content
New issue

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

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

Already on GitHub? # to your account

Test with failed expectation on value passed to mocked method is incorrectly considered risky #6138

Closed
sebastianbergmann opened this issue Feb 24, 2025 · 0 comments
Assignees
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/11 Something affects PHPUnit 11 version/12 Something affects PHPUnit 12

Comments

@sebastianbergmann
Copy link
Owner

final class C
{
    private string $foo;

    public function __construct(string $foo)
    {
        $this->foo = $foo;
    }
}
interface I
{
    public function m(C $c): void;
}
final class IssueXXXXTest extends TestCase
{
    public function testOne(): void
    {
        $i = $this->createMock(I::class);

        $i->expects($this->once())->method('m')->with(new C('bar'));

        $i->m(new C('baz'));
    }
}

Running the test shown above yields the output shown below:

PHPUnit 11.5.9-8-g6b2c09626d by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.4
Configuration: /usr/local/src/phpunit/phpunit.xml

F                                                                   1 / 1 (100%)

Time: 00:00.016, Memory: 12.00 MB

There was 1 failure:

1) PHPUnit\TestFixture\IssueXXXX\IssueXXXXTest::testOne
Expectation failed for method name is "m" when invoked 1 time
Parameter 0 for invocation PHPUnit\TestFixture\IssueXXXX\I::m(PHPUnit\TestFixture\IssueXXXX\C Object (...)): void does not match expected value.
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
 PHPUnit\TestFixture\IssueXXXX\C Object (
-    'foo' => 'bar'
+    'foo' => 'baz'
 )

/usr/local/src/phpunit/src/Framework/MockObject/Runtime/Matcher.php:110
/usr/local/src/phpunit/src/Framework/MockObject/Runtime/InvocationHandler.php:109
/usr/local/src/phpunit/tests/end-to-end/regression/XXXX/IssueXXXXTest.php:37
/usr/local/src/phpunit/src/Framework/TestCase.php:1240
/usr/local/src/phpunit/src/Framework/TestCase.php:514
/usr/local/src/phpunit/src/Framework/TestRunner/TestRunner.php:87
/usr/local/src/phpunit/src/Framework/TestCase.php:361
/usr/local/src/phpunit/src/Framework/TestSuite.php:369
/usr/local/src/phpunit/src/TextUI/TestRunner.php:64
/usr/local/src/phpunit/src/TextUI/Application.php:210

--

There was 1 risky test:

1) PHPUnit\TestFixture\IssueXXXX\IssueXXXXTest::testOne
This test did not perform any assertions

/usr/local/src/phpunit/tests/end-to-end/regression/XXXX/IssueXXXXTest.php:31

FAILURES!
Tests: 1, Assertions: 0, Failures: 1, Risky: 1.

The test is correctly considered to have failed. It is incorrectly considered to be risky as it did perform an assertion (expectations on mock objects are assertions).

final class IssueXXXXTest extends TestCase
{
    public function testOne(): void
    {
        $i = $this->createMock(I::class);

        $i->expects($this->once())->method('m')->with(new C('bar'));

        $i->m(new C('bar'));
    }
}

Running the test shown (which calls $i->m(new C('bar')); instead of $i->m(new C('baz'));) above yields the output shown below:

PHPUnit 11.5.9-8-g6b2c09626d by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.4
Configuration: /usr/local/src/phpunit/phpunit.xml

.                                                                   1 / 1 (100%)

Time: 00:00.018, Memory: 12.00 MB

OK (1 test, 1 assertion)

The expectation succeeds, is counted as an assertions, and the test is therefore not considered to be risky.

@sebastianbergmann sebastianbergmann added feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11 version/12 Something affects PHPUnit 12 labels Feb 24, 2025
@sebastianbergmann sebastianbergmann self-assigned this Feb 24, 2025
sebastianbergmann added a commit that referenced this issue Feb 24, 2025
@sebastianbergmann sebastianbergmann removed the version/10 Something affects PHPUnit 10 label Feb 24, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/11 Something affects PHPUnit 11 version/12 Something affects PHPUnit 12
Projects
None yet
Development

No branches or pull requests

1 participant