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

Fix PHP Doc Comments #1410

Open
wants to merge 3 commits into
base: 1.6.x
Choose a base branch
from

Conversation

rolandsusans
Copy link

@rolandsusans rolandsusans commented Mar 22, 2024

Related issue:

Steps to reproduce:

When installing the latest:

  • mockery/mock:1.6.11 in project dependencies, the related issue persists when Closure used.

Example code that generates the phpstan issue:

Mockery::mock(
    LoggerInterface::class,
    static function (MockInterface $mock): void {
       //....
})

phpstan error:

-
	message: "#^Parameter \\#2 \\.\\.\\.\\$args of static method Mockery\\:\\:mock\\(\\) expects array\\|class\\-string\\<Interop\\\\Queue\\\\Consumer\\>\\|Interop\\\\Queue\\\\Consumer, Closure given\\.$#"
	count: 8
	path: tests/Unit/.......php

Description

This pull request includes a change to the mock function in the Mockery.php file. The change expands the type of the $args parameter to also include Closure along with the existing types.

This change allows Closure objects to be passed as arguments to the mock function, which fixes mentioned phpstan error.

@rolandsusans rolandsusans changed the title Fx param types for phpstan Fx Mockery:mock method phpdocs Mar 22, 2024
@rolandsusans rolandsusans changed the title Fx Mockery:mock method phpdocs Fix Mockery:mock method phpdocs Mar 22, 2024
@ghostwriter ghostwriter self-assigned this Mar 22, 2024
@ghostwriter ghostwriter added Patch Backwards compatible bug fixes and improvements Fixed for bug fixes or error corrections labels Mar 22, 2024
@ghostwriter ghostwriter added this to the 1.6.12 milestone Mar 22, 2024
@aldilop
Copy link

aldilop commented Mar 22, 2024

Somehow related, I think the phpdoc in namedMock is also broken.
Phpstan should not be able to guess TNamedMock if for example we:

$mock = Mockery::namedMock('AnotherName', MyClass::class);

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Copy link

codecov bot commented Mar 22, 2024

Codecov Report

Attention: Patch coverage is 80.43478% with 18 lines in your changes are missing coverage. Please review.

Project coverage is 77.65%. Comparing base (44a98de) to head (1c67601).

Additional details and impacted files
@@             Coverage Diff              @@
##              1.6.x    #1410      +/-   ##
============================================
- Coverage     77.85%   77.65%   -0.20%     
- Complexity     1022     1026       +4     
============================================
  Files            76       76              
  Lines          2596     2623      +27     
============================================
+ Hits           2021     2037      +16     
- Misses          575      586      +11     
Files Coverage Δ
library/Mockery.php 89.86% <100.00%> (ø)
...rary/Mockery/Adapter/Phpunit/TestListenerTrait.php 86.95% <ø> (ø)
library/Mockery/Configuration.php 58.13% <ø> (ø)
library/Mockery/Container.php 92.12% <100.00%> (ø)
library/Mockery/CountValidator/AtLeast.php 100.00% <100.00%> (ø)
library/Mockery/CountValidator/AtMost.php 100.00% <100.00%> (ø)
library/Mockery/CountValidator/Exact.php 100.00% <100.00%> (ø)
...brary/Mockery/Exception/BadMethodCallException.php 100.00% <ø> (ø)
library/Mockery/ExpectsHigherOrderMessage.php 100.00% <ø> (ø)
library/Mockery/Generator/CachingGenerator.php 100.00% <100.00%> (ø)
... and 35 more

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter ghostwriter changed the title Fix Mockery:mock method phpdocs Fix PHP Doc Comments Mar 22, 2024
@ghostwriter ghostwriter linked an issue Apr 15, 2024 that may be closed by this pull request
@ghostwriter
Copy link
Member

there are many ways to create a mock, this list may not be complete.

$mock = \Mockery::mock();

$mock = \Mockery::mock(MyClass::class);

$mock = \Mockery::mock(MyClass::class, function ($mock) {
    $mock->shouldAllowMockingProtectedMethods();
});

$mock = \Mockery::mock(array('pi' => 3.1416, 'itWorks' => true));
$this->assertEquals(3.1416, $mock->pi());

$mock = \Mockery::mock('MyClass, MyInterface');

$mock = \Mockery::mock('MyClass', 'MyInterface, OtherInterface');

// <!-- runtime partial test doubles, -->
class Foo {
    function foo() {
       return 123;
    }
    function bar() {
        return $this->foo(); 
    }
}
$foo = mock(Foo::class)->makePartial();
$foo->foo(); // int(123);

// <!-- generated partial test doubles, and -->
$mock = \Mockery::mock('MyClass[method]', [param]);

// <!-- proxied partial test doubles. -->
$mock = \Mockery::mock(new MyClass);

// <!-- aliasing, -->
$mock = \Mockery::mock('alias:MyClass');

// <!-- and overloading. -->
$mock = \Mockery::mock('overload:MyClass');

// <!-- named mocks, -->
$mock = \Mockery::namedMock('MyClassName', 'DateTime');

// <!-- with constructor arguments, -->
$mock = \Mockery::mock('MyClass', [$constructorArg1, $constructorArg2]);
$mock = \Mockery::mock('MyClass', 'MyInterface', [$constructorArg1, $constructorArg2]);

// <!-- There’s also a shorthand way of setting up method call expectations and their return values: -->
$mock = \Mockery::mock('MyClass', ['name_of_method_1' => 'return value 1', 'name_of_method_2' => 'return value 2']);

// <!-- In the next example, the foo() and bar() methods of MyClass will be mocked but no other MyClass methods are touched.
// We will need to define expectations for the foo() and bar() methods to dictate their mocked behaviour. -->
$mock = \Mockery::mock('MyClass[foo,bar]');

// <!-- pass in constructor arguments since unmocked methods may rely on those -->
$mock = \Mockery::mock('MyNamespace\MyClass[foo]', array($arg1, $arg2));

we’ll add tests using each syntax, including spy|namedMock|mock and run static analysis tools, with their plugins.

@aldilop
Copy link

aldilop commented Apr 15, 2024

Everything that can be done with mock can be done with namedMock, it just shifts the first parameter.
There's a lot more magic in mock that I knew of tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed for bug fixes or error corrections Patch Backwards compatible bug fixes and improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mocking final classes reports unresolvable type by PHPStan
3 participants