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

Use of mocks swallows output #5338

Closed
Crell opened this issue Apr 18, 2023 · 3 comments
Closed

Use of mocks swallows output #5338

Crell opened this issue Apr 18, 2023 · 3 comments
Labels
feature/test-doubles Test Stubs and Mock Objects status/waiting-for-feedback Waiting for feedback from original reporter type/bug Something is broken version/10 Something affects PHPUnit 10

Comments

@Crell
Copy link

Crell commented Apr 18, 2023

Q A
PHPUnit version 10.1.1
PHP version 8.2
Installation Method Composer

Summary

Similar to #5278, I'm getting the same problem still but only in test methods that make use of the mocking library and assertions.

Current behavior

I was making this test method for a tutorial (So it's not best practice-y):

    public function testWithMockBuilder(): void
    {
        print __METHOD__;
        $mock_repository = $this
            ->getMockBuilder(ProductRepository::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->assertTrue(true);

        $obj1 = new Product('one', 50, 'blue');

        $obj2 = $this->getMockBuilder(Product::class)
            ->disableOriginalConstructor()
            ->getMock();
        $obj2->method('availableIn')->willReturn(false);
        $obj2->method('numberAvailable')->willReturn(70);

        $map = [ [1, $obj1], [2, $obj2] ];

        $mock_repository
            ->method('load')->will($this->returnValueMap($map));

        $subject = new ProductOperator($mock_repository);
        $result = $subject->find(40, 'green');

        //$this->assertCount(1, $result);
    }

If I uncomment the assertCount() statement, the output from print __METHOD__ disappears. If I comment that line out, print/var_dump/etc. generate the expected output.

Of note, in another test method in the same class I can generate output and run assertions at the same time, no problem:

    public function testStuff(): void
    {
        print __METHOD__;
        $this->assertTrue(true);
    }

How to reproduce

See above.

Expected behavior

Output should be printed to the screen regardless.

@Crell Crell added type/bug Something is broken version/10 Something affects PHPUnit 10 labels Apr 18, 2023
@sebastianbergmann
Copy link
Owner

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@sebastianbergmann sebastianbergmann added status/waiting-for-feedback Waiting for feedback from original reporter feature/test-doubles Test Stubs and Mock Objects labels Apr 19, 2023
@sebastianbergmann
Copy link
Owner

I cannot reproduce this:

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

interface I
{
    public function m(): bool;
}

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

        $i->method('m')->willReturn(true);

        $this->assertTrue($i->m());
        
        print __METHOD__;
    }
}
$ phpunit Test.php
PHPUnit 10.1.1-10-g05e360c789 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.5

Test::testOne.                                                                   1 / 1 (100%)

Time: 00:00.017, Memory: 4.00 MB

OK (1 test, 1 assertion)

@Crell
Copy link
Author

Crell commented Apr 21, 2023

Huh. Well, now I can't reproduce it either. I swear it was happening. 😄

If I get this behavior again I'll try to submit a more contained example. But for now I guess I'll close this.

@Crell Crell closed this as completed Apr 21, 2023
@Crell Crell closed this as not planned Won't fix, can't repro, duplicate, stale Apr 21, 2023
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 status/waiting-for-feedback Waiting for feedback from original reporter type/bug Something is broken version/10 Something affects PHPUnit 10
Projects
None yet
Development

No branches or pull requests

2 participants