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

Raise an error in PHPunit when calling createMock where createStub is enough #210

Open
ChinaskiJr opened this issue Sep 24, 2024 · 0 comments

Comments

@ChinaskiJr
Copy link

ChinaskiJr commented Sep 24, 2024

Feature request

Hi !

In order to enforce and clarify the usage of test doubles for everyone, I would like to know if you think it would be beneficial to implement a rule that raises an error when a mock is created without any assertions on being called (essentially making it a stub).

This rule would only be about using the correct terminology for test doubles. It should help to better understand what is going on in our test, and what is our intention.

https://docs.phpunit.de/en/10.5/test-doubles.html

In example :

<?php
// This will raise an error : 
public function testSomething(): void 
{
    $stub = $this->createMock(Foo::class);
    $stub->method('foo')->willReturn('bar');
   
    self::assertSame('bar', $stub->foo());
}

// This won't raise an error : 
public function testSomething(): void 
{
    $stub = $this->createStub(Foo::class);
    $stub->method('foo')->willReturn('bar');
   
    self::assertSame('bar', $stub->foo());
}

// This won't raise an error : 
public function testSomething(): void 
{
    $mock = $this->createMock(Foo::class);
    $mock->expects($this->once())->method('foo')->willReturn('bar');
   
    self::assertSame('bar', $mock->foo());
}

I can try to make a PR for this, but before working on it, I prefer to ask if you think it would be a good improvement.

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan Sep 24, 2024
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

1 participant